Java图形重绘问题
在java中使用简单的画板时遇到问题。让清除按钮重新绘制的问题。阵列正在清除,但未重新绘制。有人能发现我的问题吗?或者有没有其他方法可以为这段代码生成一个清除按钮Java图形重绘问题,java,swing,user-interface,repaint,graphics2d,Java,Swing,User Interface,Repaint,Graphics2d,在java中使用简单的画板时遇到问题。让清除按钮重新绘制的问题。阵列正在清除,但未重新绘制。有人能发现我的问题吗?或者有没有其他方法可以为这段代码生成一个清除按钮 public class DrawingPanel extends JPanel { private double x1=0; private double x2=0; private double y1=0; private double y2=0; private ArrayList<Shape>
public class DrawingPanel extends JPanel {
private double x1=0;
private double x2=0;
private double y1=0;
private double y2=0;
private ArrayList<Shape> myArr = new ArrayList<Shape>();
//private ArrayList<Shape> clearMyArr = new ArrayList<Shape>();
ButtonPanel buttonPress;
public void paintComponent(Graphics g)
{
for (Shape i : myArr)
{
Graphics2D g2d = (Graphics2D)g;
g2d.draw(i);
}
/*for (Shape i : clearMyArr)
{
Graphics2D g2d = (Graphics2D)g;
g2d.draw(i);
} */
}
//inner class
class Listener1 extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
System.out.println("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Shape shape = null;
if (buttonPress.buttonType.equals("Rectangle"))
{
// Rectangles cannot have a zero width or height
if (x1 != x2 || y1 != y2)
{
double width = Math.abs(x1 -x2);
double height = Math.abs(y1-y2);
shape = new Rectangle2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);
}
}
if (buttonPress.buttonType.equals("Eclipse"))
{
double width = Math.abs(x1 -x2);
double height = Math.abs(y1-y2);
shape = new Ellipse2D.Double(Math.min(x1,x2),Math.min(y1,y2), width, height);;
}
if (buttonPress.buttonType.equals("Lines"))
{
shape = new Line2D.Double(x1, y1, x2, y2);
}
if (buttonPress.buttonType.equals("Clear"))
{
for( int i = 0;i <= myArr.size(); i++ )
{
System.out.println("ArrayList Size :"+myArr.size());
myArr.clear(); // clear all elements from arraylist
//clearMyArr.addAll(myArr);
System.out.println("ArrayList Size :"+myArr.size());
//myArr.removeAll();
revalidate();
repaint();
}
}
if (shape != null)
{
myArr.add(shape);
}
repaint();
}
}
//end of inner class
public DrawingPanel(ButtonPanel reference)
{
buttonPress = reference;
setBorder(BorderFactory.createLineBorder(Color.black,4));
addMouseListener(new Listener1());
}
公共类DrawingPanel扩展了JPanel{
私有双x1=0;
私人双x2=0;
私人双y1=0;
私人双y2=0;
private ArrayList myArr=new ArrayList();
//private ArrayList clearMyArr=new ArrayList();
按钮按钮按钮;
公共组件(图形g)
{
用于(形状一:myArr)
{
Graphics2D g2d=(Graphics2D)g;
g2d.绘图(i);
}
/*用于(形状一:clearMyArr)
{
Graphics2D g2d=(Graphics2D)g;
g2d.绘图(i);
} */
}
//内部阶级
类Listener1扩展了MouseAdapter
{
公共无效鼠标按下(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
System.out.println(“鼠标按下”);
}
公共无效MouseEvent(MouseEvent e)
{
x2=e.getX();
y2=e.getY();
Shape=null;
if(buttonPress.buttonType.equals(“矩形”))
{
//矩形的宽度或高度不能为零
如果(x1!=x2 | | y1!=y2)
{
双宽度=数学绝对值(x1-x2);
双倍高度=数学绝对值(y1-y2);
形状=新矩形2D.Double(数学最小值(x1,x2),数学最小值(y1,y2),宽度,高度);
}
}
if(buttonPress.buttonType.equals(“Eclipse”))
{
双宽度=数学绝对值(x1-x2);
双倍高度=数学绝对值(y1-y2);
形状=新的椭圆2d.Double(数学最小值(x1,x2),数学最小值(y1,y2),宽度,高度);;
}
if(buttonPress.buttonType.equals(“行”))
{
形状=新线2D.双(x1,y1,x2,y2);
}
if(buttonPress.buttonType.equals(“Clear”))
{
对于JPanel的(int i=0;i尝试调用repaint()。尝试从paintcomponent方法调用super.paintcomponent(g)。如果忘记调用super.paintcomponent(g),还应确保调用的是JPanel的重新验证和重新绘制方法。
背景不会被清除,因此旧图像仍然可见。所有的按钮和您添加的内容都不会被绘制。要解决此问题,请让面板先绘制自身,然后您可以在上面绘制您的内容
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);// <-- let panel draw itself
Graphics2D g2d = (Graphics2D)g;
for (Shape i : myArr)
{
g2d.draw(i);
}
}
在侦听器中,这就足够了
if (buttonPress.buttonType.equals("Clear"))
{
myArr.clear();
repaint();
}
您不必调用revalidate();
我不明白这个问题。清除后旧形状是否仍然可见?或者清除按钮是否未绘制?要更快获得更好的帮助,请发布SSCCE()。按下“清除”按钮时,会清除已绘制的形状数组,但不会将其从jpanel中清除。我曾尝试重新验证(),重新绘制(),但没有效果。
if (buttonPress.buttonType.equals("Clear"))
{
myArr.clear();
repaint();
}