Skip to content
Advertisement

Why using an if statement inside a paintComponent(Graphics g) method invalidates all code inside the method?

I’m trying to create a JFrame object which contains a JPanel object. Inside the JPanel object there are 3 JButtons that when clicked, are intended to change the background color of the JPanel.

I also want to draw an image which size equals the one of the JPanel object to give the impression of a background image but as you may imagine I want it to be drown only the first time, when the user hasn’t clicked any buttons yet. Once a button is clicked I intend to call the repaint() method inherited from the Component class which in my understanding should make a call to the paintComponent(Graphics g).

Given the fact I want the image to be drawn only when the user hasn’t clicked any buttons, inside the paintComponent(Graphics g) I’m trying to use an if statement so when the paintComponent(Graphics g) method is called the second time by the repaint() method, it will execute inside an else statement and simply call the super.paintComponent(Graphics g) method that in my understanding, should paint it without the image. The problem is that as soon as I put the if statement inside the paintComponent method it seems to invalidate the entire code inside the method.

Any suggestions or explanation on why is this happening would be appreciated.

The code is below:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;   
import javax.swing.*;

public class PruebaEventosSelf {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MarcoBotonSelf marco=new MarcoBotonSelf();
        marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class MarcoBotonSelf extends JFrame{

    public MarcoBotonSelf() {
        setExtendedState(MarcoBotonSelf.MAXIMIZED_BOTH);
        setTitle("National Aeronautics and Space Administration NASA");
        Image image=Toolkit.getDefaultToolkit().getImage("C:\Users\wagne\OneDrive\Desktop\Nasa.png");
        setIconImage(image);
        LaminaBoton lamina=new LaminaBoton();
        add(lamina);
        setVisible(true);
    }
}

class LaminaBoton extends JPanel implements ActionListener {

    JButton botonAzul=new JButton("Blue");
    JButton botonNegro=new JButton("Black");
    JButton botonGris=new JButton("Gris");
    boolean repaint=false;

    public LaminaBoton() {
        botonAzul.addActionListener(this);
        add(botonAzul, Container.CENTER_ALIGNMENT);
        botonNegro.addActionListener(this);
        add(botonNegro, Container.LEFT_ALIGNMENT);
        botonGris.addActionListener(this);
        add(botonGris, Container.CENTER_ALIGNMENT);
    }

    public void paintComponent(Graphics g) {
        if(repaint) {
            super.paintComponent(g);
        }else {
            Image imagen=Toolkit.getDefaultToolkit().getImage("C:\Users\wagne\OneDrive\Desktop\NASA.jpg");
            g.drawImage(imagen, 0, 0, this);
        }
    }

    public void actionPerformed(ActionEvent e) {
        Object pulsado=e.getSource();
        if (pulsado==botonAzul){
            repaint=true;
            repaint();
            this.setBackground(Color.blue);
            System.out.println("Blue is working!");
        }else if(pulsado==botonNegro) {
            System.out.println("Black is working!");
            setBackground(Color.BLACK);
        }else {
            System.out.println("Gray is working!");
            setBackground(Color.DARK_GRAY);
        }
    }

}

HERE’S ANOTHER WAY I TRIED:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;  
import javax.swing.*;

public class PruebaEventosSelf {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MarcoBotonSelf marco=new MarcoBotonSelf();
        marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class MarcoBotonSelf extends JFrame{

    public MarcoBotonSelf() {
        setExtendedState(MarcoBotonSelf.MAXIMIZED_BOTH);
        setTitle("National Aeronautics and Space Administration NASA");
        Image image=Toolkit.getDefaultToolkit().getImage("C:\Users\wagne\OneDrive\Desktop\Nasa.png");
        setIconImage(image);
        LaminaBoton lamina=new LaminaBoton();
        add(lamina);
        setVisible(true);
    }
}

class LaminaBoton extends JPanel implements ActionListener {

    JButton botonAzul=new JButton("Blue");
    JButton botonNegro=new JButton("Black");
    JButton botonGris=new JButton("Gris");
    boolean repaint=false;

    public LaminaBoton() {
        botonAzul.addActionListener(this);
        add(botonAzul, Container.CENTER_ALIGNMENT);
        botonNegro.addActionListener(this);
        add(botonNegro, Container.LEFT_ALIGNMENT);
        botonGris.addActionListener(this);
        add(botonGris, Container.CENTER_ALIGNMENT);
    }

    public void paintComponent(Graphics g) {
        Image imagen=Toolkit.getDefaultToolkit().getImage("C:\Users\wagne\OneDrive\Desktop\NASA.jpg");
        g.drawImage(imagen, 0, 0, this);
        if (repaint) super.paintComponent(g);
    }

    public void actionPerformed(ActionEvent e) {
        Object pulsado=e.getSource();
        if (pulsado==botonAzul){
            repaint=true;
            repaint();
            this.setBackground(Color.blue);
            System.out.println("Blue is working!");
        }else if(pulsado==botonNegro) {
            System.out.println("Black is working!");
            setBackground(Color.BLACK);
        }else {
            System.out.println("Gray is working!");
            setBackground(Color.DARK_GRAY);
        }
    }

}

I’ve tried another 4 different ways but they all seem to lead to the same result of the image not being drown not even if the use hasn’t clicked any buttons.

Advertisement

Answer

Your paintComponent() method should ALWAYS call super.paintCompnent(g); as the first statement in the method. Then it should draw the image only if the repaint variable is false.

It would be better – and more logically readable – to call that variable paintImage and set it initially to true, then the button listener sets it to false, and the paintComponent() method draws the image only if paintImage is true.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement