Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odd graphical bug: a copy of component A is painted on component B. HELP! (java)

I have made a simple paint program in which you can use a brush-tool to paint some different colors and erase (simply paint in white).

It works fine, but I have a very strange graphical bug which causes the toolpanel and the last mouseovered color/tool-icon to be painted on top of the drawpanel.

Implementation: The frame holds two extentions of JPanel: a ToolPanel and a DrawPanel. ToolPanel holds two JPanels which hold the color- and toolbuttons. The Buttons are extensions of JComponent.

link to screenshot (I'm not allowed to post images appearently):

enter image description here

note: The buttons in the second "fake" toolpanel are not actual buttons that can be clicked and I can paint on top of it. If I paint on the "fake" color button in the left corner it will be repainted again when I mouseover a new color and enter the drawpanel with the mouse.

note 2: I used to have a JMenuBar which was also painted in the drawpanel. It was repainted each time the drawpanel is mouseovered after the window (not just the panel) lost focus once.

Some code: (I know the tool selection implementation isn't the best :P)

DrawPanel's paintComponent method:

    public void paintComponent(Graphics g) { 
    if(isMousePressed) {
        if(tool == "BRUSH") {
            g.setColor(color);
            g.fillOval(currentEvent.getX(), currentEvent.getY(), 30, 30);
        } else if(tool == "ERASER") {
            g.setColor(getBackground());
            g.fillOval(currentEvent.getX(), currentEvent.getY(), 30, 30);

        }
    }
}

Let me know if there is any relevant information or code that I left out.

Hypothesis: I didn't call super.paintComponent in DrawPanels paintComponent-method, maybe that causes some problems? The reason that I didn't is that if I do, it will repaint the background all the time so only the dot I painted last will be visible. Not sure if the supercall actually solves the problem or if the fake-panel is just covered by the background as well. Maybe I need to work around that some other way? Or is it something else?

Thanks!

like image 665
tobes Avatar asked Aug 01 '11 16:08

tobes


People also ask

What does paint component do in Java?

The paintComponent() method can also be called explicitly by the repaint() method defined in Component class. The effect of calling repaint() is that Swing automatically clears the graphic on the panel and executes the paintComponent method to redraw the graphics on this panel.

What is paint Graphics g in Java?

The method to be overridden is in java.awt.Component : public void paint(Graphics g) When AWT invokes this method, the Graphics object parameter is pre-configured with the appropriate state for drawing on this particular component: The Graphics object's color is set to the component's foreground property.

What is a paint component?

Most paints consist of the same basic components: pigments, binders, liquids, and additives. Each component serves a role in determining the quality of the paint as well as its performance both during and after application.

What does JPanel repaint do?

Ideally if you want to draw in an area then you would extend JPanel with a custom paint and place it below the button. To explain repaint further - the point of 'repaint' is to tell the window manager that you have changed something that requires the component to be redrawn.


1 Answers

It's been a long time since I worked with Swing but your basic problem is the fact the background is not being repainted. This does mean a proper working component should paint the entire area every single time, so you will have to save and repaint any previous drawing.

You could try setting your component to transparent (check for a setTransparent or setOpaque method) but as it's been several years I'm not sure what the exact result will be.

like image 73
Ken Blair Avatar answered Oct 21 '22 10:10

Ken Blair