所以我正在创建一个程序,允许用户在JPanel上绘制多个形状,类似于绘图程序。问题是,当我尝试向存储所有形状的ArrayList添加一行,然后在JPanel上重绘它们时,该行将被存储但不会被重绘。
因此,例如,假设我从顶部的JMenu中选择了线条工具。然后我单击并拖动,从我点击鼠标当前位置的位置出现该行。我释放鼠标,线仍然存在。当我去绘制另一条线,点击并拖动时,第一条线在我释放鼠标时消失。我怎样才能让这条线保留在JPanel上?
import ...
public class GraphicsTest {
public GraphicsTest() {
JFrame frame = new JFrame("");
Painter painter = new Painter();
frame.setLayout(new BorderLayout());
// frame.setJMenuBar(painter.createMenuBar());
frame.add(painter,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(750,488);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GraphicsTest();
}
});
}
public class Painter extends JPanel implements MouseListener, MouseMotionListener {
private java.awt.geom.Line2D.Double line = new java.awt.geom.Line2D.Double();
private List<Shape> shapeList = new ArrayList<Shape>();
private List<Integer> opNumList = new ArrayList<Integer>();
// Initial color
private Color color = Color.black;
// int variables that determine the position of various on-screen objects
private int dragX = 0, dragY = 0, downX = 0, downY = 0, upX = 0, upY = 0;
// Fields that determine which tool should be used
private int PEN = 0, LINE = 1, RECTANGLE = 2, OVAL = 3;
// Initial tool used
private int toolNum = LINE;
private Point clickPoint, releasePoint;
/**
* Sole and default constructor of this class
**/
public Painter() {
// Adding listeners so that the mouse may be used to draw various objects
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int index = 0;
Graphics2D g2 = (Graphics2D) g.create();
g2.setColor(color);
if (toolNum == LINE) {
g2.draw(new java.awt.geom.Line2D.Double(downX,downY,dragX,dragY));
}
System.out.println("Size: " + shapeList.size());
if (!opNumList.isEmpty()) {
for (Shape s : shapeList) {
// System.out.println("Index: " + index);
// System.out.println("Element: " + opNumList.get(index));
// System.out.println(s);
System.out.println();
switch (opNumList.get(index)) {
case 41: g2.setColor(Color.black); g2.draw(s); break;
default: return;
}
index++;
}
}
}
/**
* MouseListener interface methods
**/
@Override
public void mouseClicked(MouseEvent ev) {}
@Override
public void mouseEntered(MouseEvent ev) {}
@Override
public void mouseExited(MouseEvent ev) {}
@Override
public void mousePressed(MouseEvent ev) {
clickPoint = new Point(ev.getPoint());
upX = ev.getX();
upY = ev.getY();
downX = ev.getX();
downY = ev.getY();
}
@Override
public void mouseReleased(MouseEvent ev) {
upX = ev.getX();
upY = ev.getY();
releasePoint = new Point(ev.getPoint());
line.x1 = clickPoint.x;
line.y1 = clickPoint.y;
line.x2 = releasePoint.x;
line.y2 = releasePoint.y;
if (toolNum == LINE)
shapeList.add(line);
if (color == Color.black) {
if (toolNum == LINE)
opNumList.add(41);
}
repaint();
}
/**
* MouseMotionListener interface methods
**/
@Override
public void mouseMoved(MouseEvent ev) {}
@Override
public void mouseDragged(MouseEvent ev) {
dragX = ev.getX();
dragY = ev.getY();
repaint();
}
}
}
答案 0 :(得分:1)
您只创建了line
...
private java.awt.geom.Line2D.Double line = new java.awt.geom.Line2D.Double();
然后,您更新Line
line.x1 = clickPoint.x;
line.y1 = clickPoint.y;
line.x2 = releasePoint.x;
line.y2 = releasePoint.y;
这意味着shapeList
中的每个条目都是相同的对象,具有相同的属性。所以,是的,你的代码在shapeList
中绘制了每一行,它只是多次绘制SAME行...
相反,每次要添加新行时,都要创建一个新的实例...
Line2D line = new java.awt.geom.Line2D.Double();
line.x1 = clickPoint.x;
line.y1 = clickPoint.y;
line.x2 = releasePoint.x;
line.y2 = releasePoint.y;
然后将其添加到shapeList
...