Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pascal Triangle

Status
Not open for further replies.

Zigen

IS-IT--Management
Jan 20, 2003
6
0
0
US
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(&quot;Created by: Kevin Leong&quot;, 50, 550);
g.drawString(&quot;Date: January 29, 2003&quot;, 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;
}
}

 
It looks like the lowest number you're loading into the pascal array is 1. This array is intended to hold indexes into the color array. When you specify the number 1 when running the program, the color array is allocated with a size of 1, which means the largest index into it you can use is 0 (arrays are 0-based in java).

You need to adjust the values in the pascal array to reflect indexes that go from 0 to array.length-1. You should also review the program to make sure all the arrays are being properly addressed with 0-based indexes.

What's a Pascal triangle?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top