13 Replies - 3156 Views - Last Post: 26 May 2012 - 10:13 PM Rate Topic: -----

#1 Xente  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 31-January 12

paintComponent method not being called

Posted 26 May 2012 - 02:37 PM

I'm trying to make a simple GUI application for a hangman game, and I have a JPanel nested inside of another JPanel but the nested JPanel's paintComponent method is never called. Looking around the internet, I have found different suggestions such as explicitly setting them to visible, setting a layout, and setting a size, but none of them have worked.

Here's the code:
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class HangmanGUI extends JPanel{
	private static final long serialVersionUID = 1L;

	JLabel picture;
	BufferedImage image;
	String word = Hangman.getWord();
	int dashLength = 60;

	public HangmanGUI() {
		setLayout(new BorderLayout(4, 4));

		//EAST_PANEL
		//opens the picture and adds it to the panel
		try{
			image = ImageIO.read(new File("hangman.png"));
			picture = new JLabel(new ImageIcon(image)); 
		}
		catch(Exception E){
			System.out.println("Error");
		}
		Panel eastPanel = new Panel();
		eastPanel.add(picture);
		this.add(eastPanel, BorderLayout.EAST);

		//SOUTH_PANEL
		Panel southPanel = new Panel();
		JPanel drawPanel = new JPanel(){
                        //this method is never called
			public void paintComponent(Graphics g){
				super.paintComponent(g);
				for(int i = 0; i < word.length(); i++){
					g.drawLine( (20 + (i * dashLength)), 300, (60 + (i * dashLength)), 300);
				}
			}
		};
		southPanel.add(drawPanel);
		this.add(southPanel, BorderLayout.SOUTH);
	}

//	public void paintComponent(Graphics g){
//		super.paintComponent(g);
//		for(int i = 0; i < word.length(); i++){
//			g.drawLine( (20 + (i * dashLength)), 300, (60 + (i * dashLength)), 300);
//		}
//	}
}



I have tried to add drawPanel to the main JPanel and to the southPanel. If there's anyone who could help, it would be greatly appreciated. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: paintComponent method not being called

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10569
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: paintComponent method not being called

Posted 26 May 2012 - 02:48 PM

I inserted a System.out.println() into the paintComponent() method, and it printed. When I added drawPanel alone to the JPanel, it displayed the dashes. Since you want the user to guess the word, I would suggest using a single row of JTextFields for each letter rather than drawing the dashes yourself.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: paintComponent method not being called

Posted 26 May 2012 - 04:02 PM

I personally do not like anonymous class and thus never use them. The major reason it is that it puts code that is executed at run time within your constructor which, as far as I am concencerned should be a "run one" code.
But I understand some people might like them and this is their decision and when they post a problem with them I try to find a solution
but here we have an example of a case where I won't even tried to figure out what goes wrong. Wrtting an anonymous class for a JPanel.. please keep it apart your constructor or will be holding all your class code.
Was This Post Helpful? 1
  • +
  • -

#4 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: paintComponent method not being called

Posted 26 May 2012 - 06:44 PM

That's not an anonymous class, but anyway, how could you see it? That paintComponent override only exists within the scope of the constructor. When the constructor finishes and the frame is set visible, a repaint is done by Swing, and of course your custom paintComponent is never called again.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10569
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: paintComponent method not being called

Posted 26 May 2012 - 06:46 PM

Quote

That's not an anonymous class, but anyway, how could you see it?

That's actually incorrect. The new JPanel(){}; syntax designates an anonymous inner class. If you compile the code, an extra .class File will be generated for the anonymous inner class.
Was This Post Helpful? 1
  • +
  • -

#6 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: paintComponent method not being called

Posted 26 May 2012 - 07:17 PM

View Postmacosxnerd101, on 27 May 2012 - 01:46 AM, said:

Quote

That's not an anonymous class, but anyway, how could you see it?

That's actually incorrect. The new JPanel(){}; syntax designates an anonymous inner class. If you compile the code, an extra .class File will be generated for the anonymous inner class.


Nevermind... To be anonymous, it must not be named, in this case drawPanel.

I believe it is a local inner class inside of the constructor.

You learn something every day...maybe :)

This post has been edited by farrell2k: 26 May 2012 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 Xente  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 36
  • Joined: 31-January 12

Re: paintComponent method not being called

Posted 26 May 2012 - 07:39 PM

View Postmacosxnerd101, on 26 May 2012 - 02:48 PM, said:

I inserted a System.out.println() into the paintComponent() method, and it printed. When I added drawPanel alone to the JPanel, it displayed the dashes. Since you want the user to guess the word, I would suggest using a single row of JTextFields for each letter rather than drawing the dashes yourself.


I forgot all about trusty System.out.println(). I knew it worked when it was added directly to the JPanel, but I wanted to add it to the southPanel, but I'll try the JtextFields, it should be easier that way.

View Postpbl, on 26 May 2012 - 04:02 PM, said:

Wrtting an anonymous class for a JPanel.. please keep it apart your constructor or will be holding all your class code.


Will keep this is mind from now on:)
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: paintComponent method not being called

Posted 26 May 2012 - 09:00 PM

View Postfarrell2k, on 26 May 2012 - 09:44 PM, said:

That's not an anonymous class,

No? Sop how do you call it ?
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10569
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: paintComponent method not being called

Posted 26 May 2012 - 09:01 PM

Some of the points pbl made about anonymous inner classes (like with your JPanel) are re-emphasized in my tutorial on Approaching Swing. :)
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: paintComponent method not being called

Posted 26 May 2012 - 09:03 PM

View Postfarrell2k, on 26 May 2012 - 10:17 PM, said:

Nevermind... To be anonymous, it must not be named, in this case drawPanel.

Where did you get this definition of an anonymous class ? :) :) :)
Was This Post Helpful? 0
  • +
  • -

#11 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: paintComponent method not being called

Posted 26 May 2012 - 09:44 PM

View Postpbl, on 27 May 2012 - 04:03 AM, said:

View Postfarrell2k, on 26 May 2012 - 10:17 PM, said:

Nevermind... To be anonymous, it must not be named, in this case drawPanel.

Where did you get this definition of an anonymous class ? :) :) :)

HERE

"Local and Anonymous Inner ClassesThere are two additional types of inner classes. You can declare an inner class within the body of a method. Such a class is known as a local inner class. You can also declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes. You will encounter such classes in advanced Java programming."

Put this in a method or a constructor and it is a local inner class, a subclass of JPanel named drawPanel. It CAN be referenced only within the method, as such, it is local and NOT anonymous!

JPanel drawPanel = new JPanel(){
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    }
};    



Put this in a method or constructor and you have an anonymous class that CANNOT be referenced, thus is is anonymous!

webButton.setonclickListener(new onclickListener() {
    public void onclick(View v) {
        startActivity(new Intent(getApplicationContext(), Upgrade.class));
    }
});


This post has been edited by farrell2k: 26 May 2012 - 09:56 PM

Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: paintComponent method not being called

Posted 26 May 2012 - 09:53 PM

You can reference drawPanel wherever you want
Was This Post Helpful? 0
  • +
  • -

#13 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: paintComponent method not being called

Posted 26 May 2012 - 10:00 PM

View Postpbl, on 27 May 2012 - 04:53 AM, said:

You can reference drawPanel wherever you want


...in the constructor in which it is declared, which is why it is not an anonymous class. It's good to learn things, pbl. Just stop arguing about it. I told you what it is. :)
Was This Post Helpful? 1
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: paintComponent method not being called

Posted 26 May 2012 - 10:13 PM

I give you a +1 just for the principle
I won't argue with you because this problem will never occur to me
I hate them, because you never know (unless you are really love to use them) won't use them them and won't bother understanding them (and never let one of my programmers using them in real life) unless I can fix a DIC problem using them
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1