Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent super.paintComponent from calling clearRect
    text
    copied!<p>In a sub-class of <code>JComponent</code> I am overriding <code>paintComponent</code> method to draw. The first thing the overridden method does is to call <code>super.paintComponent</code> since most texts tell me to do this. Some say that this is necessary so that other <code>JComponents</code> can paint themselves. However, calling the super method clears the image, filling it with the background color. I don't want this happen. If I skip calling the super method, I am free to choose whether or not I want to call</p> <pre><code>g.setColor(getBackground()); g.fillRect(0, 0, getWidth() - 1, getHeight() - 1); </code></pre> <p>in order to clear the image before redrawing.</p> <p>But, I'm worried that by skipping the super method, I will be skipping other important operations the super method does. How can I make sure that those other important operations are executed while allowing myself to choose if I want to clear the image or not?</p> <p>EDIT:</p> <p>Here is my paintComponent:</p> <pre><code>public void paintComponent(Graphics g) { super.paintComponent(g); // necessary so other panels can paint themselves. // g.setColor(getBackground()); // g.fillRect(0, 0, getWidth(), getHeight()); // g.setColor(Color.black); if (down) { g.drawLine(downX, downY, currentX, currentY); } } </code></pre> <p>It just draws a line from where the user presses the mouse button to where the mouse was dragged to. If the mouse drags, the line moves. If the mouse is released it won't draw anything.</p> <p>As it is, the program will erase the line the user is drawing if the mouse is released. I don't want the program to to that so that there will be a trail of lines.</p>
 

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