问题
I have a functional program that gathers coordinates with each mouse click and then draws a polygon using those coordinates. I added a feature so that when you are done drawing your polygon and it is filled in, you can then erase the screen and start again with a new shape. What I am having trouble with is figuring out a way to reset the coordinate values.
What I have now is within my actionPerformed method I Zero out my two arrays (XCoordinates and YCoordinates). Now the user can start fresh with new coordinates but now the first coordinate is (0,0). Every time I draw a shape it starts in the upper left corner haha.
I want to set the values of the array to the values it had when I originally had when I initialized the two arrays. I tried to RE-initialize the arrays withing actionPerformed but it didn't work AND I'm sure that is very bad programming practice.
Any ideas?
回答1:
It's tempting to manipulate the Polygon
coordinate array fields directly, but you should do so only through the public API. In particular, look at these methods:
invalidate()
, which "should be called after any direct manipulation of the coordinates."reset()
, which makes a polygon empty.addPoint()
, which maintains an internally consistent state.
There's a related example here.
回答2:
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.
来源:https://stackoverflow.com/questions/13187872/java-applet-polygon-array