Note that there are some explanatory texts on larger screens.

plurals
  1. POJava graphics repaint problem
    primarykey
    data
    text
    <p>Having trouble with a simple paint pad in java. Issues with getting my clear button to repaint. The array is clearing but not repainting. Can anyone spot my problem or is there any different way of generating a clear button for this code. </p> <pre><code>public class DrawingPanel extends JPanel { private double x1=0; private double x2=0; private double y1=0; private double y2=0; private ArrayList&lt;Shape&gt; myArr = new ArrayList&lt;Shape&gt;(); //private ArrayList&lt;Shape&gt; clearMyArr = new ArrayList&lt;Shape&gt;(); 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 &lt;= 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()); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload