我正在编写一个简单的绘画程序。我已经创建了一个JPanel
,并且已经覆盖了“public void paintComponent(Graphics g)
”,我还创建了相应的Listeners
. 问题是每次我绘制一个新形状时,我以前的形状都会消失,有谁知道我怎样才能将以前的形状保持在原来的位置?我可以把它super.paintComponent(g)
带走,但随后Jpanel's
layout
意志就会被扭曲。任何建议都受到高度赞赏。:) 这是我的paintComponent 方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = xend-xstart;
int height = yend - ystart;
if(width<0)
width *= -1;
if(height <0)
height *= -1;
if(color!= null && shape !=null){
if(fill.isSelected())
{
g.setColor(color);
if(shape.equals("Rectangle"))
g.fillRect(xstart, ystart, width, height);
if(shape.equals("Square"))
g.fillRect(xstart, ystart, width, width);
if(shape.equals("Circle"))
g.fillOval(xstart,ystart,width ,width);
}
}
}