I am trying to make an applet that when done will draw a series of bars with varying heights, then sort through them and rearrange by height.
My immediate goal is to figure out if I can get coordinants to draw a rectangle from an array.
So far I have created three classes, a Bar class, an ArrayCollection Class, then the applet itself. At this point all I want to do is have the bars drawn to the screen. I think the code I have does what I intend it to since I have had no compiler issues until it comes to the paint method in the applet.
After writing this post, my instructor sent me an email and informed me that I created an array of arrays, resulting in an array of 2500 objects. I'm going to try to amend that problem before editing the code I listed below in case someone can give me a few hints.
// the following class is to create a Bar object
public Bar{
int x, y, width, height;
public Bar (int xPos, int yPos, int nwidth, int
nheight){
x = xPos;
y = yPos;
width = nwidth;
height = nheight;
}
}
// This class is to create an array of objects
import Bar;
public class ArrayCollection{
private Bar[] arrayOfBars;
private int count;
private int size;
public ArrayCollection(){
size = 50;
arrayOfBars = new Bar[size];
count = 0;
}
public void addBar (int xPos, int yPos, int width, int
height){
arrayofBars[count] = new Bar (xPos, y Pos, width,
height);
}
}
import ArrayCollection;
import java.awt.*;
import java.applet.Applet;
public class SortsDemo extends Applet{
private int x = 1;
private int y = 250;
private int width = 5;
private int height = 5;
private int gap = 3;
private ArrayCollection[] sortsBars = new
ArrayCollection[50];
public void init(){
// I am starting off the array with a bar of predetermined size
sortsBars[o].addBar(x, y, width, height);
// and now I am looping through the array and
// creating bars of larger sizes each time
for (int i = 1; i < sortsBars.legnth - 1; i++){
x+= width + gap;
height +=5;
sortsBars[i].addBar(x, y, width, height);
}
}
public void paint (Graphics g){
for (int index = o; index < sortsBars.length;
index++){
// At this point I don't know how to access what
// is in the array and have it print out to screen
g.drawRect();
}
}
Any help would be appreciated.
Regards, Michael
[ April 11, 2002: Message edited by: michael bradly ]
[ April 11, 2002: Message edited by: michael bradly ]
My immediate goal is to figure out if I can get coordinants to draw a rectangle from an array.
So far I have created three classes, a Bar class, an ArrayCollection Class, then the applet itself. At this point all I want to do is have the bars drawn to the screen. I think the code I have does what I intend it to since I have had no compiler issues until it comes to the paint method in the applet.
After writing this post, my instructor sent me an email and informed me that I created an array of arrays, resulting in an array of 2500 objects. I'm going to try to amend that problem before editing the code I listed below in case someone can give me a few hints.
// the following class is to create a Bar object
public Bar{
int x, y, width, height;
public Bar (int xPos, int yPos, int nwidth, int
nheight){
x = xPos;
y = yPos;
width = nwidth;
height = nheight;
}
}
// This class is to create an array of objects
import Bar;
public class ArrayCollection{
private Bar[] arrayOfBars;
private int count;
private int size;
public ArrayCollection(){
size = 50;
arrayOfBars = new Bar[size];
count = 0;
}
public void addBar (int xPos, int yPos, int width, int
height){
arrayofBars[count] = new Bar (xPos, y Pos, width,
height);
}
}
import ArrayCollection;
import java.awt.*;
import java.applet.Applet;
public class SortsDemo extends Applet{
private int x = 1;
private int y = 250;
private int width = 5;
private int height = 5;
private int gap = 3;
private ArrayCollection[] sortsBars = new
ArrayCollection[50];
public void init(){
// I am starting off the array with a bar of predetermined size
sortsBars[o].addBar(x, y, width, height);
// and now I am looping through the array and
// creating bars of larger sizes each time
for (int i = 1; i < sortsBars.legnth - 1; i++){
x+= width + gap;
height +=5;
sortsBars[i].addBar(x, y, width, height);
}
}
public void paint (Graphics g){
for (int index = o; index < sortsBars.length;
index++){
// At this point I don't know how to access what
// is in the array and have it print out to screen
g.drawRect();
}
}
Any help would be appreciated.
Regards, Michael
[ April 11, 2002: Message edited by: michael bradly ]
[ April 11, 2002: Message edited by: michael bradly ]