简体   繁体   中英

Paint method not being called

Im trying to simplify some basic code, an I've come across a small problem. I was under the impression that the paint method was called automatically, and i based that off every other basic program i have written. I dont get any erros, just the code doesn't work, and i cant call repaint() either. Code:

public class Dynamic_Bg_Color{

JFrame frame;

public Dynamic_Bg_Color(){
    frame = new JFrame("BG Color Changer");

}

public void paint(Graphics g1){
    Graphics g = (Graphics)g1;

    g.setColor(Color.pink);
    g.fillRect(20,20,frame.getWidth()-20,100);
}

public static void main(String[] args){
    Dynamic_Bg_Color d = new Dynamic_Bg_Color();
    Dimension size = new Dimension(500,400);
    d.frame.setPreferredSize(new Dimension(size));
    d.frame.setMinimumSize(new Dimension(size));
    d.frame.setMaximumSize(new Dimension(size));
    d.frame.setLocationRelativeTo(null);

    d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    d.frame.setLayout(new FlowLayout());
    d.frame.setVisible(true);
    d.frame.getContentPane().setBackground(Color.cyan);
}

}

The paint method only gets called if it is an override, and the class extends another class where the paint method has meaning. Your class does not do this, and so your paint method is meaningless.

Having said that, I'm going to suggest that you don't overload paint but rather paintComponent(...) in a class that extends JComponent or one of its children. And most importantly, read the painting with Swing tutorial. Please start here .

As an aside, casting a Graphics object to a Graphics object achieves nothing. Perhaps you copied your code incorrectly and were meaning to cast it to a Graphics2D type?

I made the class extend JComponent, and added the override, but nothing has changed.

  1. I don't see where you add the component to the frame.
  2. Even if you did add the component to the frame is wouldn't paint because the default size of your component is (0, 0) so there is nothing to paint.

You also need to restructure your code. A JFrame variable does not belong to the component where you do custom painting. I suggest you read the section from the Swing tutorial on Custom Painting . It will show you:

  1. How to do custom painting including how you specify a preferred size for your component
  2. How to better structure your program, included executing your code on the Event Dispatch Thread

With minimal changes made the working code:

import javax.swing.*;
import java.awt.*;

public class Dynamic_Bg_Color extends JPanel{

JFrame frame;

public Dynamic_Bg_Color(){
frame = new JFrame("BG Color Changer");
}

public void paint(Graphics g1){
Graphics g = (Graphics2D)g1;

g.setColor(Color.pink);
g.fillRect(20,20,frame.getWidth()-20,100);
}

public static void main(String[] args){
Dynamic_Bg_Color d = new Dynamic_Bg_Color();
Dimension size = new Dimension(500,400);
d.setPreferredSize(new Dimension(size));
d.setMinimumSize(new Dimension(size));
d.setMaximumSize(new Dimension(size));

d.frame.add(d);
d.frame.setLocationRelativeTo(null);
d.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.frame.setLayout(new FlowLayout());
d.frame.pack();
d.frame.setVisible(true);
d.frame.getContentPane().setBackground(Color.cyan);
}

}

But, as others said, I don't suggest you use this cause it is very very bad structure of the program. And read the tutorial on painting, even if I think that Oracles tutorial on this is bad (frame.pack() just sets the size of the JFrame window as big as to hold the components it contains).

Heres the final working code. A few changes being it extended JPanel, setting the size of the JPanel, and adding the panel to the JFrame. Yes i know as everybody stated, this is not an optimal way of doing this, but for right now, it works.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Dynamic_Bg_Color extends JPanel{

private static final long serialVersionUID = 1L;

static Dimension size = new Dimension(500,400);

static JFrame frame;

public Dynamic_Bg_Color(){

    setPreferredSize(size);
    setBackground(Color.cyan);
    addMouseListener(new Handler());

}

@Override
public void paintComponent(Graphics g){

    System.out.println("Click");

    super.paintComponent(g);

    g.setColor(Color.blue);
    g.fillRect(20,20,getWidth()-40,100);

    g.setColor(Color.green);
    g.fillRect(20,140,getWidth()-40,100);

    g.setColor(Color.orange);
    g.fillRect(20,260,getWidth()-40,100);
}

public static void main(String[] args){     


    Dynamic_Bg_Color d = new Dynamic_Bg_Color();

    frame = new JFrame("BG Color Changer");

    frame.setPreferredSize(new Dimension(size));
    frame.setMinimumSize(new Dimension(size));
    frame.setMaximumSize(new Dimension(size));
    frame.setLayout(new FlowLayout());
    frame.setLocationRelativeTo(null);
    frame.add(d);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.cyan);
    frame.setVisible(true);

    }

public class Handler extends MouseAdapter{

    public void mousePressed(MouseEvent e) {

        int x = e.getX();
        int y = e.getY();

        if(x>= 20 && x<=getWidth()-40 && y>=20 && y<= 120){
            frame.getContentPane().setBackground(Color.blue);
            setBackground(Color.blue);
            frame.setTitle("Blue");
        }
        if(x>= 20 && x<=getWidth()-40 && y>=140 && y<= 240){
            frame.getContentPane().setBackground(Color.green);
            setBackground(Color.green);
            frame.setTitle("Green");    
            }
        if(x>= 20 && x<=getWidth()-40 && y>=260 && y<= 360){
            frame.getContentPane().setBackground(Color.orange);
            setBackground(Color.orange);
            frame.setTitle("Orange");
            }
    }



}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM