Drawing a line

import java.util.ArrayList;

import javax.swing.JPanel;

import java.awt.*;

import java.awt.event.*;

public class LinePanel extends JPanel

{

private ArrayList<Point> pointList;

private int x, y, x2, y2;

public LinePanel()

{

pointList = new ArrayList<Point>();

addMouseListener (new LineListener());

setBackground (Color.black);

setPreferredSize (new Dimension(300, 300));

}

public void paintComponent (Graphics page)

{

super.paintComponent(page);

page.setColor (Color.yellow);

page.drawLine (x);

}

private class LineListener implements MouseListener

{

public void mousePressed (MouseEvent event)

{

pointList.add(event.getPoint());

repaint();

}

public void mouseClicked (MouseEvent event) {}

public void mouseReleased (MouseEvent event) {}

public void mouseEntered (MouseEvent event) {}

public void mouseExited (MouseEvent event) {}

}

}

this is my program but its obviously incomplete, how do i make the different x and y variables from the array list so that i can draw a line by clicking in two different places on the panel. If my question is too confusing (im bad at wording it right) let me know and ill try again.

[1339 byte] By [Holy_Nemoa] at [2007-10-2 15:59:36]
# 1
Program assignment: Write a graphics program that opens a blank JPanel. Make it so that you click can click anywhere on the panel once and then click somewhere else and the program draws a line from the first point where u clicked to the second.
Holy_Nemoa at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...
# 2
can nobody help me?
Holy_Nemoa at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...
# 3
Yes.
Holy_Nemoa at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...
# 4
so how do i do it?
Holy_Nemoa at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...
# 5
Go to Google. Search for- java swing tutorial- mouselistener- java swing custom painting
CeciNEstPasUnProgrammeura at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...
# 6

> so how do i do it?

- Hold an instance field being an array of two java.awt.Point.

- Keep an int pointer to which index of that array you're gonna change next (init value: 0).

- on mouse click, store the location of the click in your point array, at the index the pointer points to, increase the pointer, mod (%) it by the array's length. Call a repaint.

- in the paintComponent method, if both of the Points in your point array are not null, draw a line from the first point to the second.

da.futta at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...
# 7
As I'm sure yo have figured out by now, you'll use the mouselistener for the coordinates of your line and then simply draw a line from the two coordinates found... Do what CeciNEstPasUnProgrammeur and it should start coming together.
beamer7296a at 2007-7-13 16:26:05 > top of Java-index,Java Essentials,Java Programming...