0

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");
  }
}
MPelletier
  • 16,256
  • 15
  • 86
  • 137
Tussler
  • 5
  • 1
  • 3
  • 1
    `paintComponent` belongs to the `JPanel` `myJPan` class not the `setup` class – Reimeus Jul 22 '13 at 17:39
  • you're not actually overriding `myJPan`'s `paintComponent` method. You're simply defining a `paintComponent` method in your `setup` class. – DannyMo Jul 22 '13 at 17:39
  • If you had added the `@Override` annotation, the compiler would have told you that you're not actually overriding anything. – DannyMo Jul 22 '13 at 17:43
  • So what is is that I need to do? I have to override myJPan's paintComponent method? I don't think I know how to do that – Tussler Jul 22 '13 at 17:47

1 Answers1

0

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 ActionListeners.

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.

Community
  • 1
  • 1
scottysseus
  • 1,922
  • 3
  • 25
  • 50
  • Anytime. I also struggled with the concept only a few months ago. – scottysseus Jul 22 '13 at 18:34
  • also, you should mark this as the answer if it truly answers your question – scottysseus Jul 22 '13 at 18:56
  • 1
    _recommend that you create your own custom JFrame_ very suboptimal: the general rule (OO 101) is to extend xx only if you enhance its xx-ness. The xx-ness of a JFrame is being top-level container. Mere usage of those services isn't no reason for extension. – kleopatra Jul 23 '13 at 06:15