Skip to content
Advertisement

Painting method paints things from other components

I am trying to make a simple Java program with GUI using Java Swing.

I have painting panel (gPanel) in the center of the screen, panel with buttons (buttonSet) in the west and panel with labels (labelPanel) in the east. To paint over gPanel I use paintComponent method and since I have two buttons, which are supposed to draw different things (and change label on the right of the screen), I decided to put switch case in paintComponent method for it to choose the correct actual painting method.

When I run the program everything looks fine – program uses the first method to paint and there is a sampletext.png image shown in the middle of the screen with yellow background, as it should be. Button number 1 also uses this method to draw over gPanel, so pressing it draws the same thing.

Now Button number 2 uses the second painting method and this is where things go wrong. It draws sampleimage.png over the gPanel, but also parts of left and right panels (i.e. buttons from left buttonSet panel and orange colour that is background colour of side panels) are drawn, though it shouldn’t happen. Also the whole gPanel becomes gray (I think it happens because of label on the right that becomes very long after pressing Button number 2, because when the label was shorter gPanel didn’t turn gray and left the previously drawn things instead).

Pressing Button number 1 paints things from first method properly, so pressing it after pressing Button number 2 “reverts” the changes.

What do I have to do to make my second painting method work properly? Also why adding border to buttonSet and labelPanel works but adding it to gPanel doesn’t?

JavaScript

Tree of my project:

  • DrawingProject

    -JRE System Library

    -src

    –com.inferjus.drawingproject

    —DrawingProject.java

    –resources

    —sampleicon.png

    —sampleimage.png

    —sampletext.png

what shows after running the program by default or after pressing Button One

what shows after pressing Button Two one time

what shows after pressing Button Two a few times

Advertisement

Answer

Introduction

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

I went ahead and created the following GUI. I created two BufferedImages for the text image and the plain image so I wouldn’t have to read any external files.

enter image description here

enter image description here

Explanation

When I create a Swing GUI, I use the model-view-controller pattern. This pattern allows me to separate my concerns and focus on one part of the application at a time.

Model

I created a model class to hold the button flag and the two BufferedImages. This is the class where you would read the resources.

You can add the JFrame icon back to this class.

Model classes are plain Java getter/setter classes.

View

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

Class names are written in camel case and start with an upper case character. Method names are written in camel case and start with a lower case character. Field names follow the same rules as method names.

I separated the creation of the JFrame from the creation of the JPanels. This helps me to separate my concerns and makes it much easier to visually verify whether or not the code is correct. Aim to write short methods that do one thing and do it well.

You have to manually draw a border on a graphic JPanel. I added the code to your paintComponent method to paint a partial border.

Your paintComponent method should paint. Period. Nothing else. It must also start with a call to the super.paintComponent method to maintain the Swing paint chain.

I changed your JLabel in the right JPanel to a JTextArea. A JTextArea allows for longer messages to word wrap on multiple lines and not make your JFrame change size.

Controller

Your JButton controller classes were fine, except for the class names.

Code

Here’s the complete runnable code. I made all the additional classes inner classes so I could post the code in one block.

JavaScript

Update

In order to paint multiple images, you have to save the origin of the images in a List. I’ve modified the application model to hold a List of origin Point instances. I also corrected the code to create a random point.

Here’s the GUI with multiple images.

enter image description here

Here’s the modified code

JavaScript
Advertisement