You're making it hard by not providing any code, but here's two takes on the idea...
Use Polygon
This basically uses a Polygon
and keeps adding points to it until you press enter...
public class PolyPainter {
public static void main(String[] args) {
new PolyPainter();
}
public PolyPainter() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PolyPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class PolyPane extends JPanel {
private Polygon poly;
private Point lastPoint;
public PolyPane() {
poly = new Polygon();
InputMap im = getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "clear");
ActionMap am = getActionMap();
am.put("clear", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
poly = new Polygon();
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
lastPoint = e.getPoint();
poly.addPoint(e.getX(), e.getY());
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.draw(poly);
if (lastPoint != null) {
g2d.setColor(Color.RED);
g2d.fillOval(lastPoint.x - 5, lastPoint.y - 5, 10, 10);
}
g2d.dispose();
}
}
}
Use List Of Points
This basically uses a list of points
public class PolyPainter1 {
public static void main(String[] args) {
new PolyPainter1();
}
public PolyPainter1() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new PolyPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected class PolyPane extends JPanel {
private List<Point> poly;
private Point lastPoint;
public PolyPane() {
poly = new ArrayList<Point>(25);
InputMap im = getInputMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "clear");
ActionMap am = getActionMap();
am.put("clear", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
poly.clear();
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
lastPoint = e.getPoint();
poly.add(lastPoint);
repaint();
}
});
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Polygon pg = new Polygon();
for (Point p : poly) {
pg.addPoint(p.x, p.y);
}
g2d.draw(pg);
if (lastPoint != null) {
g2d.setColor(Color.RED);
g2d.fillOval(lastPoint.x - 5, lastPoint.y - 5, 10, 10);
}
g2d.dispose();
}
}
}
Personally, the first one is more efficient, as it doesn't need to construct a new Polygon
object each time it does a repaint.