im writing a program that draws stuff, rite now im only trying to draw lines, and storing their values in an arraylist, and then reading them back and drawing.
heres the code
Code:
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class DrawPanel extends JPanel
{
private Point point1 = null, point2 = null;
private ArrayList line = new ArrayList();
public DrawPanel()
{
DrawListener listener = new DrawListener();
addMouseListener (listener);
addMouseMotionListener(listener);
setBackground (Color.white);
setPreferredSize (new Dimension(400, 300));
}
public void paintComponent (Graphics page)
{
super.paintComponent (page);
page.setColor (Color.black);
for(int x = 0; x < line.size(); ++x)
{
if(line.get(x) != null & line.get(x) != null)
{
page.drawLine (((Point)line.get(x)).x, ((Point)line.get(x)).y, ((Point)line.get(x+1)).x, ((Point)line.get(x+1)).y);
}
}
}
private class DrawListener implements MouseListener, MouseMotionListener
{
public void mousePressed (MouseEvent event)
{
point1 = event.getPoint();
line.add(point1);
}
public void mouseDragged(MouseEvent event)
{
point2 = event.getPoint();
line.add(point2);
repaint();
}
public void mouseClicked (MouseEvent event) {}
public void mouseReleased (MouseEvent event) {}
public void mouseEntered (MouseEvent event) {}
public void mouseExited (MouseEvent event) {}
public void mouseMoved (MouseEvent event) {}
}
}