aspose file tools*
The moose likes Applets and the fly likes obtaining drawing coordinants from an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "obtaining drawing coordinants from an array" Watch "obtaining drawing coordinants from an array" New topic
Author

obtaining drawing coordinants from an array

michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
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 ]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: obtaining drawing coordinants from an array