The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
May 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Well that part's done.

Posted by Steve on May 25, 2000 at 1:55 AM

> Hey guys/gals. I use the code segment below to draw the
> horizontal lines in a grid. Can I make an array of Line2D.Float,
> or should I use collections ?? I was thinking:
> float[20] xOne; //used to hold x1 coords. in Line2D.Float
> float[20] yOne; //y1 coords.
> float[20] xTwo; //x2 coords.
> float[20] yTwo; //y2 coords.
> Then an array used to hold an Hln[20] object. One Hln
> element could hold one element from each of the arrays above.
> If possible, how do I go about this.
> Once this is done, I was thinking to use a for LOOP to
> draw the lines. Thanks, Steve

> ----------------
> Line2D.Float Hln1 = new Line2D.Float(11F, 20F, 350F, 20F);
> Line2D.Float Hln2 = new Line2D.Float(11F, 30F, 350F, 30F);
> Line2D.Float Hln3 = new Line2D.Float(11F, 40F, 350F, 40F);
> Line2D.Float Hln4 = new Line2D.Float(11F, 50F, 350F, 50F);
> Line2D.Float Hln5 = new Line2D.Float(11F, 60F, 350F, 60F);
> Line2D.Float Hln6 = new Line2D.Float(11F, 70F, 350F, 70F);
> Line2D.Float Hln7 = new Line2D.Float(11F, 80F, 350F, 80F);
> Line2D.Float Hln8 = new Line2D.Float(11F, 90F, 350F, 90F);
> Line2D.Float Hln9 = new Line2D.Float(11F, 100F, 350F, 100F);
> Line2D.Float Hln10 = new Line2D.Float(11F, 110F, 350F, 110F);
> Line2D.Float Hln11 = new Line2D.Float(11F, 120F, 350F, 120F);
> Line2D.Float Hln12 = new Line2D.Float(11F, 130F, 350F, 130F);
> Line2D.Float Hln13 = new Line2D.Float(11F, 140F, 350F, 140F);
> Line2D.Float Hln14 = new Line2D.Float(11F, 150F, 350F, 150F);
> Line2D.Float Hln15 = new Line2D.Float(11F, 160F, 350F, 160F);
> Line2D.Float Hln16 = new Line2D.Float(11F, 170F, 350F, 170F);
> Line2D.Float Hln17 = new Line2D.Float(11F, 180F, 350F, 180F);
> Line2D.Float Hln18 = new Line2D.Float(11F, 190F, 350F, 190F);
> Line2D.Float Hln19 = new Line2D.Float(11F, 200F, 350F, 200F);
> Line2D.Float Hln20 = new Line2D.Float(11F, 210F, 350F, 210F);

> screen2D.draw(Hln1);
> screen2D.draw(Hln2);
> screen2D.draw(Hln3);
> screen2D.draw(Hln4);
> screen2D.draw(Hln5);
> screen2D.draw(Hln6);
> screen2D.draw(Hln7);
> screen2D.draw(Hln8);
> screen2D.draw(Hln9);
> screen2D.draw(Hln10);
> screen2D.draw(Hln11);
> screen2D.draw(Hln12);
> screen2D.draw(Hln13);
> screen2D.draw(Hln14);
> screen2D.draw(Hln15);
> screen2D.draw(Hln16);
> screen2D.draw(Hln17);
> screen2D.draw(Hln18);
> screen2D.draw(Hln19);
> screen2D.draw(Hln20);

I got it (Thanks, euyeuy). Here's the code if anyone's curious:

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class Chart extends JApplet {
float height;
float width;

public void init() {
setBackground(Color.white);
}

public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen;
height = (float) getSize().height;
width = (float) getSize().width;
screen2D.setColor(Color.black);
Rectangle2D.Float border = new Rectangle2D.Float(10F, 10F, 340F, 210F);
screen2D.draw(border);

// create and initialize 2D array of coords
// coords1=x1, y1 coords2=x2, y2 - Vertical
// coords3=x1, y1 coords4=x2, y2 - Horizontal
float [] [] coords1 = { {20F, 10F}, {30F, 10F}, {40F, 10F}, {50F, 10F}, {60F, 10F},
{70F, 10F}, {80F, 10F}, {90F, 10F}, {100F, 10F}, {110F, 10F},
{120F, 10F}, {130F, 10F}, {140F, 10F}, {150F, 10F},
{160F, 10F}, {170F, 10F}, {180F, 10F}, {190F, 10F},
{200F, 10F}, {210F, 10F}, {220F, 10F}, {230F, 10F}, {240F, 10F}, {250F, 10F}, {260F, 10F}, {270F, 10F}, {280F, 10F}, {290F, 10F}, {300F, 10F}, {310F, 10F}, {320F, 10F}, {330F, 10F}, {340F, 10F}
};
float [] [] coords2 = { {20F, 220F}, {30F, 220F}, {40F, 220F}, {50F, 220F}, {60F, 220F}, {70F, 220F}, {80F, 220F}, {90F, 220F}, {100F, 220F}, {110F, 220F}, {120F, 220F}, {130F, 220F}, {140F, 220F}, {150F, 220F}, {160F, 220F}, {170F, 220F}, {180F, 220F},
{190F, 220F}, {200F, 220F}, {210F, 220F}, {220F, 220F}, {230F, 220F}, {240F, 220F}, {250F, 220F}, {260F, 220F}, {270F, 220F}, {280F, 220F}, {290F, 220F}, {300F, 220F}, {310F, 220F}, {320F, 220F}, {330F, 220F}, {340F, 220F} };
float [] [] coords3 = { {10F, 20F}, {10F, 30F}, {10F, 40F}, {10F, 50F}, {10F, 60F},
{10F, 70F}, {10F, 80F}, {10F, 90F}, {10F, 100F}, {10F, 110F},
{10F, 120F}, {10F, 130F}, {10F, 140F}, {10F, 150F}, {10F, 160F}

};
float [] [] coords4 = { {350F, 20F}, {350F, 30F}, {350F, 40F}, {350F, 50F}, {350F, 60F}, {350F, 70F}, {350F, 80F}, {350F, 90F}, {350F, 100F}, {350F, 110},
{350F, 120F}, {350F, 130F}, {350F, 140F}, {350F, 150F},
{350F, 160F}
};

// create Point objects array from the coord array
Point2D.Float[] points1 = new Point2D.Float[coords1.length];
Point2D.Float[] points2 = new Point2D.Float[coords2.length];
Point2D.Float[] points3 = new Point2D.Float[coords3.length];
Point2D.Float[] points4 = new Point2D.Float[coords4.length];

// create Line objects array from the Point objects array
Line2D.Float[] VertLine2D = new Line2D.Float[points1.length];
Line2D.Float[] HorizLine2D = new Line2D.Float[points3.length];

// for LOOP for initializing the points/lines
for(int i = 0; i < points1.length; i++) {
points1[i] = new Point2D.Float(coords1[i] [0], coords1[i] [1]);
points2[i] = new Point2D.Float(coords2[i] [0], coords2[i] [1]);
VertLine2D[i] = new Line2D.Float(points1[i], points2[i]);
for(int z = 0; z < points3.length; z++) {
points3[z] = new Point2D.Float(coords3[z] [0], coords3[z] [1]);
points4[z] = new Point2D.Float(coords4[z] [0], coords4[z] [1]);
HorizLine2D[z] = new Line2D.Float(points3[z], points4[z]);

// drawing the lines
screen2D.setColor(Color.gray);
screen2D.draw(VertLine2D[i]);
screen2D.draw(HorizLine2D[z]);
}
}
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us