你則很難通過不提供任何代碼,但這裏的二取的想法...
使用多邊形
這基本上採用的是Polygon
和不斷加分,直到按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
對象。
您可能希望顯示一些代碼以符合您的描述。否則,我們怎麼知道你可能做錯了什麼? –
一個「Point」類型的數組,更多的是面向對象的,然後是平行的x-y數組。 – Mordechai
我自己我會使用'ArrayList',然後調用'clear()'在需要時重置它。 –