2

For my assignment, I am given this piece of code:

// This class/method uses a  global variable that MUST be set before calling/using
// note: You can not call the paint routine directly, it is called when frame/window is shown
// look up the repaint() routine in the book
// Review Listings 8.5 and 8.6
//
public static class MyPanel extends JPanel {
 public void paintComponent (Graphics g) {
    int xpos,ypos;
    super.paintComponent(g);
    // set the xpos and ypos before you display the image
    xpos = 10; // you pick the position
    ypos = 10; // you pick the position
    if (theimage != null) {
        g.drawImage(theimage,xpos,ypos,this);
        // note: theimage global variable must be set BEFORE paint is called
    }
 }
}

My professor also says: You will also need to to look up how to create AND add a JPanel to a JFrame. If you can create AND add a JPanel, then all you need to do is substitute 'MyPanel' for the class name 'JPanel' and this code will display an image on the window frame.

What does he mean by "then all you need to do is substitute 'MyPanel' for the class name 'JPanel' and this code will display an image on the window frame"? I'm confused as to where I'm supposed to substitute MyPanel. Here's my code:

public static class MyPanel extends JPanel {
 public void paintComponent (Graphics g) {
    int xpos,ypos;
    super.paintComponent(g);
    JPanel panel= new JPanel();
    JFrame frame= new JFrame();
    frame.setSize(500,400);
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // set the xpos and ypos before you display the image
    xpos = 600; // you pick the position
    ypos = 600; // you pick the position
    if (theimage != null) {
        g.drawImage(theimage,xpos,ypos,this);
        // note: theimage global variable must be set BEFORE paint is called
    }
 }
}
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
stepup.stepout
  • 61
  • 1
  • 1
  • 9
  • 1
    You must have previous listings of code using `JPanel` as the primary container. Your professor is suggesting you should take those listing (8.5 & 8.6) and try and make them use `MyPanel` instead – MadProgrammer Oct 16 '12 at 06:04
  • 2
    paintComponent is protected not public – Aubin Oct 16 '12 at 06:18
  • This [example](http://stackoverflow.com/a/11372350/1057230) might be of interest to you :-) – nIcE cOw Oct 16 '12 at 06:53
  • Creating a new instance of a frame or panel is ***not*** something that should be done within, or invoked from, the `paintComponent()` method! – Andrew Thompson Oct 16 '12 at 07:19
  • Neither of the posted codes would compile, since `theimage` is not declared. – Andrew Thompson Oct 16 '12 at 07:21
  • @Aubin you can escalate the accessibility of a method to a weaker state (I think that's what it's called), so you can make `protected` `public`, but not the other way round – MadProgrammer Oct 16 '12 at 07:42

2 Answers2

5

If I understand what you're asking right... In your assignment, you're being asked to extend JPanel for your own needs. Note how you would add a JPanel if it were not being extended:

JFrame myFrame = new JFrame();
JPanel myPanel = new JPanel();
myFrame.add(myPanel);
myFrame.pack();
myFrame.setVisible(true);

This adds the JPanel to the JFrame, packs it and sets it to be visible. Since your myFrame class extends JPanel, you should be able to do something very similar by creating a new instance of your panel class and adding it to your JFrame.

You won't want to be doing this part in paintComponent(), as paintComponent() can potentially be called multiple times. Check here to see what paintComponent() does.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • I answered my own question if you can check my updated code (I don't know how to add code text in comments). And I have to use the code given to me by my professor, otherwise I get marked down points. – stepup.stepout Oct 16 '12 at 06:16
  • For questions like this I would recommend just updating your question with new code, as comments are a bad format for posting new code. Anyway, your professor's initial code looks fine to me, just make sure you're not trying to set your frame within your custom panel class. – Anthony Neace Oct 16 '12 at 06:19
3

@Hyper Anthony

So it would be something similar to this?:

MyPanel Mypanel= new MyPanel();
JFrame Myframe= new JFrame();
Myframe.setSize(500,400);
Myframe.add(Mypanel);
Myframe.setVisible(true);
Myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
stepup.stepout
  • 61
  • 1
  • 1
  • 9
  • 1
    Yeah. As some general advice it is good practice to do all of your work on your frame before you set it visible, and you'll also likely want to move it elsewhere... like your main function or a method called from main. May also want to name your panel something different, if only to not confuse yourself. – Anthony Neace Oct 16 '12 at 06:13