不提供任何代码会让您变得很困难,但这里有两个想法......
使用多边形
这基本上使用 aPolygon
并不断向其添加点,直到您按 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();
}
}
}
使用点列表
这基本上使用了一个点列表
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();
}
}
}
就个人而言,第一个更有效,因为它不需要在Polygon
每次重绘时都构造一个新对象。