Forums Register Login

obtaining drawing coordinants from an array

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Hi Michael,
First off, your professor is correct. You don't need the extra ArrayCollection class. Just use an array of Bars.

Now you just do the following inside your init method:

The inside your paint you can just use the array as a variable.

Regards,
Manfred.
+Pie Number of slices to send: Send
Thank you for the response. I actually stumbled upon your suggestion in the wee hours of this morning.
However, I am now getting a null pointer exception when I try to run the applet. I was wondering what type of tweaking I would need to do to my method addBar. I essentially copied the one I had in my ArrayCollection class into my Bar class. But since I am no longer using a second array should I simply copy to code in my Bar constructor to my method.
Regards, Michael

Originally posted by Manfred Leonhardt:
Hi Michael,
First off, your professor is correct. You don't need the extra ArrayCollection class. Just use an array of Bars.

Now you just do the following inside your init method:

The inside your paint you can just use the array as a variable.

Regards,
Manfred.

 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1317 times.
Similar Threads
getting coordinants for a bar graph
Gray box where my applet should be
placing images in applets
Randomizing an array
how to create an image through bufferedimage,image and other helper class using byte array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:37:31.