Hi I am having problems printing out my Pascal Triangle. I created the program so that I can enter a modulus number to print out how many colors I want my pascal triangle to use. When I enter the number '1', I am getting a out of bound errors. Does anyone know how I can fix this? Also does anyone know how I can make the picture look like a upright triangle? Can you give me some tips to make my code work better and cleaner? Thanks in advance!!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pascal extends JApplet implements ActionListener {
// Variables
JLabel numberLabel;
JTextField numberField;
JButton drawButton;
int mod;
int pascal[][] = new int[400][400];
public void init() {
// Obtain content pane
Container container = getContentPane();
container.setLayout(null);
// Label
numberLabel = new JLabel("Input Number:"
;
numberLabel.setFont(new Font("Arial", Font.BOLD, 14));
container.add(numberLabel);
numberLabel.setBounds(150,50,150,20);
// Input Numbers
numberField = new JTextField(5);
numberField.setEditable(true);
container.add(numberField);
numberField.setBounds(260,50,50,20);
// Button
drawButton = new JButton("Draw Pascal"
;
drawButton.setFont(new Font("Verdana", Font.BOLD, 10));
drawButton.setBackground(Color.magenta);
drawButton.addActionListener(this);
container.add(drawButton);
drawButton.setBounds(320,50,130,20);
// Backcolor - Yellow
container.setBackground(Color.yellow);
// Window size
setSize(600,600);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
// Cast mod string to a int
String getMod = numberField.getText();
mod = Integer.parseInt(getMod);
// Color
Color color[] = new Color[mod];
for (int c = 0; c < mod ; c++) {
color[c] = randomColor();
}
// Draws Pascal Traingle
for (int x = 1; x < 400; x++) {
for (int y = 0; y < x; y++) {
// Draws Triangles
g.setColor(color[pascal[x][y]]);
g.fillRect(100+x, 120+y, 1,1);
}
}
// Name and Date
g.drawString("Created by: Kevin Leong", 50, 550);
g.drawString("Date: January 29, 2003", 50, 570);
}
public void actionPerformed (ActionEvent event) {
String getMod = numberField.getText();
mod = Integer.parseInt(getMod);
pascal[0][0] = 1;
// Calculations
for (int x = 1; x < pascal.length; x++) {
for (int y = 0; y < pascal.length; y++) {
if (y == 0 || x == y) {
pascal[x][y] = 1; }
else {
pascal[x][y] = (pascal[x-1][y-1] + pascal[x-1][y]) % mod;}
}
}
repaint();
}
public Color randomColor() {
Color rColor = new Color( Math.abs((int)(Math.random()*256)),
Math.abs((int)(Math.random()*256)),
Math.abs((int)(Math.random()*256)));
return rColor;
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Pascal extends JApplet implements ActionListener {
// Variables
JLabel numberLabel;
JTextField numberField;
JButton drawButton;
int mod;
int pascal[][] = new int[400][400];
public void init() {
// Obtain content pane
Container container = getContentPane();
container.setLayout(null);
// Label
numberLabel = new JLabel("Input Number:"
numberLabel.setFont(new Font("Arial", Font.BOLD, 14));
container.add(numberLabel);
numberLabel.setBounds(150,50,150,20);
// Input Numbers
numberField = new JTextField(5);
numberField.setEditable(true);
container.add(numberField);
numberField.setBounds(260,50,50,20);
// Button
drawButton = new JButton("Draw Pascal"
drawButton.setFont(new Font("Verdana", Font.BOLD, 10));
drawButton.setBackground(Color.magenta);
drawButton.addActionListener(this);
container.add(drawButton);
drawButton.setBounds(320,50,130,20);
// Backcolor - Yellow
container.setBackground(Color.yellow);
// Window size
setSize(600,600);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
// Cast mod string to a int
String getMod = numberField.getText();
mod = Integer.parseInt(getMod);
// Color
Color color[] = new Color[mod];
for (int c = 0; c < mod ; c++) {
color[c] = randomColor();
}
// Draws Pascal Traingle
for (int x = 1; x < 400; x++) {
for (int y = 0; y < x; y++) {
// Draws Triangles
g.setColor(color[pascal[x][y]]);
g.fillRect(100+x, 120+y, 1,1);
}
}
// Name and Date
g.drawString("Created by: Kevin Leong", 50, 550);
g.drawString("Date: January 29, 2003", 50, 570);
}
public void actionPerformed (ActionEvent event) {
String getMod = numberField.getText();
mod = Integer.parseInt(getMod);
pascal[0][0] = 1;
// Calculations
for (int x = 1; x < pascal.length; x++) {
for (int y = 0; y < pascal.length; y++) {
if (y == 0 || x == y) {
pascal[x][y] = 1; }
else {
pascal[x][y] = (pascal[x-1][y-1] + pascal[x-1][y]) % mod;}
}
}
repaint();
}
public Color randomColor() {
Color rColor = new Color( Math.abs((int)(Math.random()*256)),
Math.abs((int)(Math.random()*256)),
Math.abs((int)(Math.random()*256)));
return rColor;
}
}