Drawing shapes in loops - java

So Im basically just trying to Draw a whole bunch of random triangles to the screen in a loop while changing the colors which seemed not very difficult but i cannot find where my problem lies... it wont loop it just displays one image here's what i have
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JFrame;
public class ManyTriangles extends Canvas {
public void paint(Graphics g) {
Random r = new Random();
int x1 = r.nextInt(350);
int x2 = r.nextInt(400);
int x3 = r.nextInt(300);
int y1 = r.nextInt(800);
int y2 = r.nextInt(200);
int y3 = r.nextInt(600);
int[] xpts = { x1, x2, x3 };
int[] ypts = { y1, y2, y3 };
int randomColor = r.nextInt(3);
for (int x = 0; x <= 500; x++) {
if (randomColor == 3) {
g.setColor(Color.green);
} else if (randomColor == 2) {
g.setColor(Color.red);
} else if (randomColor == 1) {
g.setColor(Color.blue);
}
g.fillPolygon(xpts, ypts, 3);
}
}
public static void main(String[] args) {
ManyTriangles canvas = new ManyTriangles();
JFrame frame = new JFrame("Lots of Triangle's");
frame.setSize(1024, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(canvas);
frame.setVisible(true);
}
}

Your xpts and yppts never change within the loop, so you are painting the same thing over and over again in different colors
You've broken the paint chain by not calling super.paint
You're mixing heavy and light weight components (Canvas on a JFrame), this is not really a good idea...
Instead...
Move the creation of the xpts and yppts into the loop
Call super.paint before doing any custom painting or event better
Use a JPanel instead of a Canvas and override it's paintComponent method instead, making sure you call super.paintComponent before doing any custom painting...
See Painting in AWT and Swing, Performing Custom Painting for more details
Other issues...
Because you're re-generating the output each time paint is called, your output could change at random times (as the repaint manager schedules new repaint requests). If you don't want this, generate the shapes in the constructor or other method, adding them to some kind of List or array and iterate over this within your paintComponent method...

Related

How can I remove the white background and how can i add a stick?

I am trying to create a Billiards game for my project and I am having difficulty in adding the stick and the balls at the same time. Also, when I add the balls the background of the JFrame goes white, it is actually supposed to be green (the color of the table).
Your help will be much appreciated.
I tried using Graphics. For example g2d.setBackground(Color.green). Which did not work
public class Practice_Window implements MouseListener, MouseMotionListener, KeyListener {
JFrame Practice_Mode = new JFrame();
Balls myBalls = new Balls();
Stick myStick = new Stick();
public void PracticeWindow()
{
Practice_Mode.setSize(1000, 500);
Practice_Mode.setVisible(true);
Practice_Mode.setResizable(false);
Practice_Mode.setTitle("Practice Mode");
Practice_Mode.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Practice_Mode.getContentPane().setBackground(new Color(0, 1, 0, 0.5f)); //Not visible after i add my balls
Practice_Mode.getRootPane().setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, Color.GREEN));
Practice_Mode.add(myBalls);
//Practice_Mode.add(myStick);
//JPanel p = new JPanel();
//Timer t= new Timer(10, myBalls);
}
//BALL CLASS
public class Balls extends JPanel implements ActionListener
{
double Size = 35;
double REDxSpeed = 5;
double REDySpeed = 5;
double REDSpeed = 0;
double YELLOWxSpeed = 2;
double YELLOWySpeed = 3;
double YELLOWSpeed = 0;
double WHITExSpeed = 4;
double WHITEySpeed = 2;
double WHITESpeed = 0;
double friction = 0.2;
boolean Collision = false;
Ellipse2D.Double red = new Ellipse2D.Double(200, 200, Size, Size);
Ellipse2D.Double yellow = new Ellipse2D.Double(300, 300, Size, Size);
Ellipse2D.Double white = new Ellipse2D.Double(150, 400, Size, Size);
Timer t = new Timer(10, this);
Balls()
{
t.start();
}
#Override
public void actionPerformed(ActionEvent e) //Things are moving here
{
//RED BALL
red.x += REDxSpeed;
red.y += REDySpeed;
REDSpeed = Math.sqrt(REDxSpeed*REDxSpeed + REDySpeed*REDySpeed);
repaint();
if(red.x < 0 || red.x > getWidth() - red.width)
{
REDxSpeed = -REDxSpeed;
}
if(red.y < 0 || red.y > getHeight() - red.height)
{
REDySpeed = -REDySpeed;
}
//YELLOW BALL
yellow.x += YELLOWxSpeed;
yellow.y += YELLOWySpeed;
YELLOWSpeed = Math.sqrt(YELLOWxSpeed*YELLOWxSpeed + YELLOWySpeed*YELLOWySpeed);
repaint();
if(yellow.x < 0 || yellow.x > getWidth() - yellow.width)
{
YELLOWxSpeed = -YELLOWxSpeed;
}
if(yellow.y < 0 || yellow.y > getHeight() - yellow.height)
{
YELLOWySpeed = -YELLOWySpeed;
}
//WHITE BALL
white.x += WHITExSpeed;
white.y += WHITEySpeed;
WHITESpeed = Math.sqrt(WHITExSpeed*WHITExSpeed + WHITESpeed*WHITEySpeed);
repaint();
if(white.x < 0 || white.x > getWidth() - white.width)
{
WHITExSpeed = -WHITExSpeed;
}
if(white.y < 0 || white.y > getHeight() - white.height)
{
WHITEySpeed = -WHITEySpeed;
}
Collision_Detection();
}
public void paintComponent(Graphics g) //DRAWING MY BALLS
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
//g2d.setBackground(Color.green);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.red);
g2d.fill(red);
g2d.setColor(Color.yellow);
g2d.fill(yellow);
g2d.setColor(Color.black);
g2d.fill(white);
}
//STICK CLASS
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class Stick extends JPanel implements MouseListener, MouseMotionListener, ActionListener {
int xLocation = 50;
int yLocation = 50;
int Width = 50;
int Height = 15;
Rectangle2D.Double stick = new Rectangle2D.Double(200, 200, Width, Height);
Timer t = new Timer(10, this);
Stick()
{
t.start();
}
public void PaintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.ORANGE);
g2d.fill(stick);
}
I am having difficulty in adding the stick and the balls at the same time.
So why are you trying to write your entire application at once without doing any testing along the way? Why do you have Timers in the code? Why do you have KeyListeners?
Learn to develop applications one step at a time. Write a little code do some testing. When it works add more functionality. Get the basic right first before adding more complicated logic.
Practice_Mode.add(myBalls);
//Practice_Mode.add(myStick);
The default layout manager of a JFrame is the BorderLayout. By default, when you add a component to the frame (and don't specify a constraint) the component goes to the CENTER. Only a single component can be added to the CENTER so only the last component added will be visible.
Your basic design is wrong. You don't want to have separate panels for the balls and stick. You want to have a single "BilliardsGameBoard" panel that will paint multiple objects. So this panel will paint all the balls and the stick. This way all the objects are managed by the same class.
You should also not be creating individual objects with variable names. This limits the number of object you can control. Instead, keep the objects you want to paint in an ArrayList, then the painting method iterates through the ArrayList and paints each object.
See: get width and height of JPanel outside of the class for a working example of this approach.
I add the balls the background of the JFrame goes white,
Practice_Mode.getContentPane().setBackground(new Color(0, 1, 0, 0.5f));
Don't use alpha values when you specify your color. For green you can just use:
practiceMode.getContentPane().setBackground( Color.GREEN );
Your stick and balls are extending JPanel, which is typically used as a container to group a bunch of JComponents, which are buttons and UI elements. The graphical errors you see is likely Java trying to line up your panels side by side using the default BorderLayout, because it thinks you want panels of buttons and stuff when you are just trying to achieve freeform shapes.
A better approach would be to to render your stick and balls as primitive shapes on a JPanel, rather than as JPanels themselves. This is accomplished by making them implement Shape or at least giving them draw methods; the Primitives tutorial would be of use. This question also has somebody in a somewhat similar situation as yourself, and has answers relevant to you.

How to fill histogram with array data?

I am having trouble getting my histogram to fill correctly.
I was given a large data file full of doubles that represented GPAs, about 5500 of them. I created a method to calculate the count, mean, and standard deviation of the data, however my last problem is to graph the data.
I am to make a histogram for each of the possible grades (12 of them) and graph them about the total of each grade.
I think I have coded the total for each grade correctly, but when it comes to actually drawing the histogram I cannot figure out the 4 arguments needed for fillRect.
I've been playing around with different variables, but nothing seems to get me close.
Any help is appreciated.
private static int[] gradeCounts(double[] stats) throws Exception{
double stdv = 0;
double sum = 0;
double sum2 = 0;
double variance = 0;
Scanner fsc = new Scanner(new File("introProgGrades.txt"));
while (!fsc.hasNextDouble())
fsc.nextLine();
int[] binCounts = new int[NUM_OF_GRADE_CATEGORIES];
double x = 0;
while (fsc.hasNextDouble()){
stats[2]++;
x = fsc.nextDouble();
sum += x;
sum2 += x * x;
if (x == 0.0)
binCounts[0]++;
else if (x == 0.6666667)
binCounts[1]++;
else if (x == 1.0)
binCounts[2]++;
else if (x == 1.3333333)
binCounts[3]++;
else if (x == 1.6666667)
binCounts[4]++;
else if (x == 2.0)
binCounts[5]++;
else if (x == 2.3333333)
binCounts[6]++;
else if (x == 2.6666667)
binCounts[7]++;
else if (x == 3.0)
binCounts[8]++;
else if (x == 3.3333333)
binCounts[9]++;
else if (x == 3.6666667)
binCounts[10]++;
else
binCounts[11]++;
}
stats[0] = sum/stats[2];
variance = (stats[2] * sum2 - sum * sum) / (stats[2]*(stats[2]-1));
stdv = Math.sqrt(variance);
stats[1] = stdv;
return binCounts;
}
What I am having trouble with:
private static void plotHistogram(int[] binCounts){
int max = Arrays.stream(binCounts).max().getAsInt();
DrawingPanel panel = new DrawingPanel (800,800);
Graphics2D g = panel.getGraphics();
g.fillRect(0, 0, 800/binCounts.length,max);
}
I think I have to iterate through the data with a for loop, but it's the parameters of fillRect that I am clueless on.
but when it comes to actually drawing the histogram I cannot figure out the 4 arguments needed for fillRect.
The JavaDocs are quite explicit in the properties and their meanings, it's just a box, with a position (x/y) and size (width/height)
public abstract void fillRect​(int x,
int y,
int width,
int height)
Fills the specified rectangle. The left and right edges of the rectangle are at x and x +
width - 1. The top and bottom edges are at y and y + height - 1. The
resulting rectangle covers an area width pixels wide by height pixels
tall. The rectangle is filled using the graphics context's current
color. Parameters: x - the x coordinate of the rectangle to be filled.
y - the y coordinate of the rectangle to be filled. width - the width
of the rectangle to be filled. height - the height of the rectangle to
be filled.
I've been playing around with different variables, but nothing seems to get me close.
So, two things come to mind, the first comes from the JavaDocs above...
The rectangle is filled using the graphics context's current
color
This is something that's easy to forget
The second is it seems that you misunderstand how painting works in Swing.
Painting in Swing has a very specific and well documented workflow. The first thing you should do is go read Performing Custom Painting and Painting in AWT and Swing to get a better understanding of how painting works and how you should work with it.
There is never a good reason to call JComponent#getGraphics, apart from been able to return null, this is just a snapshot of the last paint cycle and will be wiped clean on the next paint pass (which may occur at any time for any number of reasons).
Instead, you will need a custom component and override it's paintComponent method instead.
You should then have a read through the 2D Graphics trail to get a better understanding of how the API works and what features/functionality it can provide.
For example....
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Random rnd = new Random();
int[] binCounts = new int[10];
for (int index = 0; index < binCounts.length; index++) {
binCounts[index] = rnd.nextInt(100);
}
JFrame frame = new JFrame();
frame.add(new DrawingPanel(binCounts));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DrawingPanel extends JPanel {
private int[] binCounts;
private int max;
public DrawingPanel(int[] binCounts) {
this.binCounts = binCounts;
max = Arrays.stream(binCounts).max().getAsInt();
System.out.println(max);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(800, 800);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
int barWidth = 800 / binCounts.length;
for (int i = 0; i < binCounts.length; i++) {
int barHeight = (int)((binCounts[i] / (double)max) * getHeight());
// I personally would cache this until the state of the component
// changes, but for brevity
Rectangle rect = new Rectangle(i * barWidth, getHeight() - barHeight, barWidth, barHeight);
g2d.setColor(Color.BLUE);
g2d.fill(rect);
g2d.setColor(Color.BLACK);
g2d.draw(rect);
}
g2d.dispose();
}
}
}

Graphics2D.drawPolyline() vs Graphics2D.drawLine()

Is there any benefit to calling drawPolyline() versus iterating through each line and calling drawLine() in the Graphics2D class?
For example:
graphics2d.drawPolyline(xPoints, yPoints, nPoints);
versus:
for (MyBean line : myBeans) {
graphics2d.drawLine(line.getX1Point(), line.getY1Point(), line.getX2Point(), line.getY2Point());
}
Is the first a convenience method for the second?
Fairly new to AWT. (I realize that the first one may be more concise.)
Edit: I call BufferedImage.createGraphics() for the implementation of Graphics2D.
There is an important difference: With drawPolyline you are drawing a single polyline. With drawLine you are drawing individual lines. So far, so obvious. But what does it mean?
The difference mainly shows up when assigning a "non-trivial" Stroke to the graphics object - usually, a certain BasicStroke. This receives several parameters in the constructor. The important one regarding your question is the join parameter. It can be JOIN_BEVEL, JOIN_METER and JOIN_ROUND. It determines how two connected lines are joined. And this, obviously, can only be applied when it is known that the lines are connected, which is only the case in the drawPolyline call. It simply can not be applied for individual drawLine calls.
The following is a screenshot showing this difference. It uses a stroke with a witdh of 15 and a join=BasicStroke.JOIN_ROUND. The left part is drawn with drawPolyline, and the right one is drawn as individual lines:
But you should not usually not use drawPolyline anyhow...
... because it is somehow out-dated and has several shortcomings. First of all, it is a hassle to create the arrays that are required for calling it. And importantly, it only accepts int[] arrays.
The whole Java 2D painting infrastructure was originally focussing on int coordinates, like in Graphics#drawLine(int,int,int,int). This has been generalized, and the Graphics2D methods allow a much greater flexibility here. So the usual way to draw a polyline nowadays would be to create a Shape object containing the polyline. In most cases, this will be a Path2D instance:
Path2D path = new Path2D.Double();
path.moveTo(x0,y0);
path.lineTo(x1,y1);
path.lineTo(x2,y2);
...
graphics2D.draw(path);
However, just for reference, here is the code that was used to create the above image:
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawLineVsDrawPolyline
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame("");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(new BorderLayout());
class Line
{
int x1, y1, x2, y2;
Line(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public int getX1Point()
{
return x1;
}
public int getY1Point()
{
return y1;
}
public int getX2Point()
{
return x2;
}
public int getY2Point()
{
return y2;
}
}
int xPoints[] = new int[] { 100, 150, 200 };
int yPoints[] = new int[] { 100, 250, 100 };
int nPoints = xPoints.length;
List<Line> lines = new ArrayList<Line>();
for (int i0=0; i0<nPoints-1; i0++)
{
int i1 = i0+1;
int x1 = xPoints[i0];
int y1 = yPoints[i0];
int x2 = xPoints[i1];
int y2 = yPoints[i1];
lines.add(new Line(x1,y1,x2,y2));
}
JPanel panel = new JPanel()
{
#Override
protected void paintComponent(Graphics gr)
{
super.paintComponent(gr);
Graphics2D g = (Graphics2D)gr;
g.setColor(Color.RED);
g.setStroke(new BasicStroke(20.0f,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));
g.drawPolyline(xPoints, yPoints, nPoints);
g.translate(200, 0);
for (Line line : lines) {
g.drawLine(
line.getX1Point(), line.getY1Point(),
line.getX2Point(), line.getY2Point());
}
}
};
f.getContentPane().add(panel, BorderLayout.CENTER);
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
Bearing in mind that Graphics2D is an abstract class, so you don't know which concrete implementation you are working with, I would use whichever method makes your code cleaner.
I rarely use drawPolyline() because it's usually a pain to get the coordinates into arrays for pass to the method. But you should also consider creating a GeneralPath object which can be passed to the Graphics2D.draw(Shape) method. Drawing a path might be more efficient than drawing individual lines for some implementations of Graphics2D. Occasionally I've noticed also that the output looks slightly better with a path rather than individual lines, particularly if the line color is partially transparent (you can sometimes see darker spots where the end points of the individual lines overlap).

Tileing image on JPanel, java

I have a method to set the 'texture' of a JPanel, however it is throwing a NullPointerException, and i cannot figure out why.
Method:
void setTexutre(Image tileImage) {
Graphics g = panel.getGraphics();
int width = (int) getBounds().getWidth();
int height = (int) getBounds().getHeight();
int imageW = tileImage.getWidth(panel);
int imageH = tileImage.getHeight(panel);
for (int x5 = 0; x5 < width; x5 += imageW) {
for (int y5 = 0; y5 < height; y5 += imageH) {
g.drawImage(tileImage, x5, y5, panel);
}
}
panel.paint(g);
}
The NullPointerException is thrown when i call "g.drawImage(tileImage, x5, y5, panel);"
And yes, the image is a real image, i have checked. In the method above panel is defined as a new JPanel, and intializes normally when I do not call the method.
Thanks for any help!
DON'T use Graphics g = panel.getGraphics();
NEVER call panel.paint(g);
See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing/AWT.
getGraphics may return null (it's even documented as saying so) and you SHOULD never rely on it, it's not how custom painting works. Instead, you should override the components paintComponent method and perform your custom painting within it.
You don't control the paint process and should never call paint directly, Swing uses a passive rendering algorithm, this means that components are update ad-hoc, when ever the RepaintManager decides that they need to be repainted. This means, even if you could get your current code to work, the moment the RepaintManager decides to repaint panel, all you rendering would be lost...
The following is the class I used for anyone else looking at this question.
package i.am.not.posting.the.real.pack.name;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class TiledPanel extends JPanel {
private BufferedImage tileImage;
public TiledPanel(BufferedImage tileImage) {
this.tileImage = tileImage;
}
protected void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
int imageW = tileImage.getWidth();
int imageH = tileImage.getHeight();
// Tile the image to fill our area.
for (int x = 0; x < width; x += imageW) {
for (int y = 0; y < height; y += imageH) {
g.drawImage(tileImage, x, y, this);
}
}
}
}
simply creating a TilePanel object will correctly tile the image.

Freehand painting in Java

I've been writing this little painting program thing, but whenever I release the mouse and move to another point on the screen, the line is drawn over there. I tried clearing the points when the mouse has been released, but that deletes everything on screen.
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
public class PaintingCanvas extends Canvas implements MouseMotionListener, MouseListener {
private ArrayList<Point> points = new ArrayList<Point>();
public PaintingCanvas(int width, int height) {
setBounds(0, 0, width, height);
addMouseMotionListener(this);
addMouseListener(this);
}
public void paint(Graphics g) {
for (int i = 0; i < points.size() - 2; i++) {
Point p1 = points.get(i);
Point p2 = points.get(i + 1);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
#Override
public void mouseDragged(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
}
I suggest:
Call the super.paint(g); method first thing in your paint method.
When the mouse is released, paint the image represented by the points ArrayList to a BufferedImage and then clear the points ArrayList, and then call repaint().
Draw the BufferedImage in the paint method. before drawing your lines (but check that it's not null first). You do this with the Graphics#drawImage(Image image, int x, int y, ...) method.
Better, re-write this to work in Swing by painting in a JPanel's paintComponent method.
Of course this is happening - once you start drawing again, the new points are added after the old ones. Once you paint after that, they are included. You'll need to seperate the different paths from each other.
Have you looked at the Path classes? If you are simply drawing discrete lines to a screen, the GeneralPath class might be a simple solution.
The Drawing Arbitrary Shapes tutorial explains how to use these.
Basically, every time the user pressed the mouse (on a mousePressed event), you would call the path's moveTo(x, y) method. For every segment (replacing what you currently do in the mouseDragged() method), you would call the path's lineTo(x, y) method.
No matter what, you -definitely- need to handle mousePressed or mouseReleased events, or both, as you are looking for some way to indicate the start of a new line/path, rather than using the old one.
private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {
points.add(evt.getPoint());
for (int i = 0; i < points.size() - 2; i++)
{
Point p1 = points.get(i);
Point p2 = points.get(i + 1);
jPanel1.getGraphics().drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {
points.clear();
points.add(evt.getPoint());
}
I would make a 2d arraylist in which the first array is points added to until you lift up your finger then a new array is added for the second line etc...
Insert a garbage point -1,-1 in points inside mouseReleased method and check for it inside paint method and skip that point. Updated code:
#Override
public void mouseReleased(MouseEvent e) {
points.add(new Point(-1, -1));
}
#Override
public void paint(Graphics g) {
for (int i = 0; i < points.size() - 1; i++) {
Point p = points.get(i+1);
int x2 = p.x;
int y2 = p.y;
if ( x2 == -1 || y2 == -1) {
++i;
//continue // Considered bad practice, can play havoc with your system. (source http://xkcd.com/292/ ).
}
else {
p = points.get(i);
int x1 = p.x;
int y1 = p.y;
g.drawLine(x1, y1, x2, y2);
}
}
}
OP, how it feels to see this question after 3 years? :D

Categories