简体   繁体   中英

Basic paintComponent not being called by repaint()?

I'm using the book Headfirst java , and I have put together a program that I thought would compile fine. But when the window is created, the background or oval isn't showing up.

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

public class setup {  
  public static void main(String[] args) {    
    JFrame f = new JFrame();
    System.out.println("Created Frame");
    JPanel myJPan = new JPanel();
    System.out.println("Created Panel");

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    f.setSize(300,300);
    System.out.println("Set Size");
    f.setLocationRelativeTo(null);  
    f.setContentPane(myJPan);  
    f.setVisible(true);
    System.out.println("Made Visible");
    myJPan.repaint();
  }


  // @Override  ???
  //  "protected void" ??
  public void paintComponent(Graphics g) {
      // super.paintComponent(); ???
      g.fillRect(0,0,300,300);
      System.out.println("painted");
      int red = (int) (Math.random()*255);
      int green = (int) (Math.random()*255);
      int blue = (int)(Math.random()*255);
      System.out.println("Got Random Colors");
      Color randomColor = new Color(red, green, blue);
      g.setColor(randomColor);
      System.out.println("Set Random Colors");
      g.fillOval(70,70,100,100);
      System.out.println("Filled Oval");
  }
}

See my answer to this question . It provides an example of the proper way to set up a JPanel.

Like other commenters/answerers have stated, paintComponent belongs to the JPanel class. What this means for you is that you need to create a class (let's call it MyPanel ) that extends JPanel. (Note: you can either make a new .java file for this class if you are in eclipse or make it an inner class, it should work either way).

Once you have done that, simply cut the paintCOmponent method from your setup class and paste it into your new MyPanel class.

And finally, within your setup class, instead of creating a JPanel object, create a MyPanel .

Basically, this MyPanel object is your own custom JPanel object that does whatever you want it to! It's almost like magic!

On a side note (this will hopefully help you better format your code in the future), and hopefully more experience Java programmers will agree with me on this, I would also recommend that you create your own custom JFrame object as well. Only for this one, you won't override any methods other than the constructor . Instead, in your constructor for this custom JFrame , you will make all of the specifications for the window (such as your setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE and setSize(300,300) calls). This constructor is also where you will instantiate your MyPanel object (as well as any other component objects in your window) and maybe give it a few ActionListener s.

Then, in another class (such as your setup class), have a main method that has 1 line: one that instantiates a 'JFrame` object. This will automagically create the window.

Oh and one more vitally import thing: you must call setVisible(true) on your JFrame if you want it to display. I am not sure why it is setup that way.

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