• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with PaintComponent

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone, I am New to Java. I am creating a HangMan Game and i am not able to Paint the JPanel which is Inside the JFrame. I had overriden the PaintComponent Method for JPanel, but still its not Painting. Need Help on it. Here is the sample Code:-


Thanks



 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your posted code doesn't show us much. Post an SSCCE that shows how you create the frame and add the panel to the frame. Then maybe we can offer some help.
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the Full Code.. The problem is. i used a JPAnel for drawing(drawingPanel named in the code) inside a JFRAME and i overide the PaintComponent Method. I have added that JPanel to Jframe. the PaintComponent should draw Lines based on ErroCount. However the PaintComponent is not drawing anything. i am not able to debug it. Please Help.I have pasted the Code below:-

public class Hangman3 extends JFrame
{
private JLabel welcome;
private JLabel dash;
private String[] str = {"GOAT","LION","SHEEP","ZEBRA","FUN","ABSABSAB"};
private Random generator;
private String guessWord;
private JLabel jlabelDashes;
private int i;
private JPanel jPanelAlphabet;
private String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private char[] chr = alphabets.toCharArray();
private char[] chrGuessWord;
private JButton [] jButtonAlphabet;
private ButtonHandler handler;
private Font font;
private int x=10;
private int y=50;
private int width=50;
private int height=50;
private String populateDashes = "";
private JButton buttonPressed;
private JLabel winLooseMessage;
private int errorCount = 0;
private boolean flag;
private JPanel drawingPanel;


public Hangman3()
{
//super("HangMan");
welcome = new JLabel("Welcome to the Hangman Game.Here is your Word...!");
Font font = new Font("Jokerman", Font.PLAIN, 35);
welcome.setFont(font);
setBackground(Color.yellow);

//this.setBackground(Color.RED);
setLayout(new BorderLayout() );
add(welcome,BorderLayout.NORTH);
generator = new Random();
i = generator.nextInt(str.length) ;
System.out.println(" i is "+i);
guessWord = str[i];
System.out.println(" Guess Word is "+guessWord);

jPanelAlphabet = new JPanel();
//gridLayout = new GridLayout(2, 13, 5, 5);
jPanelAlphabet.setLayout(null);
jButtonAlphabet = new JButton[26];
handler = new ButtonHandler();
for(int i=0;i<chr.length;i++)
{
jButtonAlphabet[i] = new JButton(""+chr[i]);
if(i==0)
{
jButtonAlphabet[i].setBounds(10, 50, 50, 50);


}
else if(i<13 || i>13)
{
x=x+70;
jButtonAlphabet[i].setBounds(x, y, width, height);


}

else if(i==13)
{
x=10;
y=y+70;
jButtonAlphabet[i].setBounds(x, y, width, height);
}
jButtonAlphabet[i].addActionListener(handler);

jPanelAlphabet.add(jButtonAlphabet[i]);
drawingPanel = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
if(errorCount==1)
g.drawRect(450, 450, 150, 150 );
if(errorCount==2)
{
g.setColor(Color.RED);
g.drawRect(350, 350, 150, 150 );
g.drawLine(420,350,420,50);
}
}

};
}

//populate the dashes
for( int i=0;i<guessWord.length();i++)
{
populateDashes = populateDashes +"-";
}
//System.out.println("populateDashes length" +populateDashes.length());
winLooseMessage = new JLabel("");
winLooseMessage.setFont(font);
winLooseMessage.setBounds(10, 300, 900, 150);
jlabelDashes = new JLabel(populateDashes);
dash = new JLabel("Guess Word: ");
dash.setFont(font);
dash.setBounds(10, 200, 300, 150);
Font font1 = new Font( "Serif", Font.BOLD, 45 );
jlabelDashes.setFont(font1);
jlabelDashes.setBounds(270, 200, 250, 150);
jPanelAlphabet.add(dash);
jPanelAlphabet.add(jlabelDashes);
jPanelAlphabet.add(winLooseMessage);

add(jPanelAlphabet,BorderLayout.CENTER);

this.getContentPane().add(drawingPanel,BorderLayout.EAST);

//System.out.println("populateDashes "+populateDashes);
}//end of constructor

public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
buttonPressed = (JButton)e.getSource();
System.out.println("You Pressed "+buttonPressed.getText());
isCharacterPresent(buttonPressed.getText());
}

}

public void isCharacterPresent( String userInput)
{
flag=false;

char ui = userInput.charAt(0);
char[] abc = (jlabelDashes.getText()).toCharArray();
chrGuessWord = guessWord.toCharArray();
for(int i=0; i<chrGuessWord.length; i++ )
{
if(ui==chrGuessWord[i])
{
abc[i]=chrGuessWord[i];
System.out.println("Match Found");
flag=true;
}
}
String s =new String(abc);
jlabelDashes.setText(s);
if(s.equals(guessWord) )
{
winLooseMessage.setText("Congratulation.. you have Saved the MAN !");

}
if( !flag )
errorCount++;
if(errorCount==9)
winLooseMessage.setText("You Lost it.Correct Answer is "+guessWord);
}
public static void main( String [] args )
{
Hangman3 h3 = new Hangman3();
h3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
h3.setSize( 1600, 900 ); // set frame size
h3.setVisible( true ); // display frame


}
}






 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AmritP Singh wrote:Here is the Full Code.. The problem is. i used a JPAnel for drawing(drawingPanel named in the code) inside a JFRAME and i overide the PaintComponent Method. I have added that JPanel to Jframe. the PaintComponent should draw Lines based on ErroCount. However the PaintComponent is not drawing anything. i am not able to debug it. Please Help.I have pasted the Code below:-

public class Hangman3 extends JFrame
{
private JLabel welcome;
private JLabel dash;
private String[] str = {"GOAT","LION","SHEEP","ZEBRA","FUN","ABSABSAB"};
private Random generator;
private String guessWord;
private JLabel jlabelDashes;
private int i;
private JPanel jPanelAlphabet;
private String alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private char[] chr = alphabets.toCharArray();
private char[] chrGuessWord;
private JButton [] jButtonAlphabet;
private ButtonHandler handler;
private Font font;
private int x=10;
private int y=50;
private int width=50;
private int height=50;
private String populateDashes = "";
private JButton buttonPressed;
private JLabel winLooseMessage;
private int errorCount = 0;
private boolean flag;
private JPanel drawingPanel;


public Hangman3()
{
//super("HangMan");
welcome = new JLabel("Welcome to the Hangman Game.Here is your Word...!");
Font font = new Font("Jokerman", Font.PLAIN, 35);
welcome.setFont(font);
setBackground(Color.yellow);

//this.setBackground(Color.RED);
setLayout(new BorderLayout() );
add(welcome,BorderLayout.NORTH);
generator = new Random();
i = generator.nextInt(str.length) ;
System.out.println(" i is "+i);
guessWord = str[i];
System.out.println(" Guess Word is "+guessWord);

jPanelAlphabet = new JPanel();
//gridLayout = new GridLayout(2, 13, 5, 5);
jPanelAlphabet.setLayout(null);
jButtonAlphabet = new JButton[26];
handler = new ButtonHandler();
for(int i=0;i<chr.length;i++)
{
jButtonAlphabet[i] = new JButton(""+chr[i]);
if(i==0)
{
jButtonAlphabet[i].setBounds(10, 50, 50, 50);


}
else if(i<13 || i>13)
{
x=x+70;
jButtonAlphabet[i].setBounds(x, y, width, height);


}

else if(i==13)
{
x=10;
y=y+70;
jButtonAlphabet[i].setBounds(x, y, width, height);
}
jButtonAlphabet[i].addActionListener(handler);

jPanelAlphabet.add(jButtonAlphabet[i]);
drawingPanel = new JPanel()
{
@Override
public void paintComponent(Graphics g)
{
if(errorCount==1)
g.drawRect(450, 450, 150, 150 );
if(errorCount==2)
{
g.setColor(Color.RED);
g.drawRect(350, 350, 150, 150 );
g.drawLine(420,350,420,50);
}
}

};
}

//populate the dashes
for( int i=0;i<guessWord.length();i++)
{
populateDashes = populateDashes +"-";
}
//System.out.println("populateDashes length" +populateDashes.length());
winLooseMessage = new JLabel("");
winLooseMessage.setFont(font);
winLooseMessage.setBounds(10, 300, 900, 150);
jlabelDashes = new JLabel(populateDashes);
dash = new JLabel("Guess Word: ");
dash.setFont(font);
dash.setBounds(10, 200, 300, 150);
Font font1 = new Font( "Serif", Font.BOLD, 45 );
jlabelDashes.setFont(font1);
jlabelDashes.setBounds(270, 200, 250, 150);
jPanelAlphabet.add(dash);
jPanelAlphabet.add(jlabelDashes);
jPanelAlphabet.add(winLooseMessage);

add(jPanelAlphabet,BorderLayout.CENTER);

this.getContentPane().add(drawingPanel,BorderLayout.EAST);

//System.out.println("populateDashes "+populateDashes);
}//end of constructor

public class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
buttonPressed = (JButton)e.getSource();
System.out.println("You Pressed "+buttonPressed.getText());
isCharacterPresent(buttonPressed.getText());
}

}

public void isCharacterPresent( String userInput)
{
flag=false;

char ui = userInput.charAt(0);
char[] abc = (jlabelDashes.getText()).toCharArray();
chrGuessWord = guessWord.toCharArray();
for(int i=0; i<chrGuessWord.length; i++ )
{
if(ui==chrGuessWord[i])
{
abc[i]=chrGuessWord[i];
System.out.println("Match Found");
flag=true;
}
}
String s =new String(abc);
jlabelDashes.setText(s);
if(s.equals(guessWord) )
{
winLooseMessage.setText("Congratulation.. you have Saved the MAN !");

}
if( !flag )
errorCount++;
if(errorCount==9)
winLooseMessage.setText("You Lost it.Correct Answer is "+guessWord);
}
public static void main( String [] args )
{
Hangman3 h3 = new Hangman3();
h3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
h3.setSize( 1600, 900 ); // set frame size
h3.setVisible( true ); // display frame


}
}





Can someone Help me out with This.

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when posting code, highlight the code then click the 'code' button
(located just a bit right of the quote button - which you seem to have found OK)

it changes this

public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}

to this

and is much easier to read

your problem/s seem to be
1) drawingPanel has no components (just a quick glance), so it has no size - you'll need to give it a preferrredSize
so BorderLayout.EAST will give it that space
2) drawingPanel will not automatically update itself - you need to call drawingPanel.repaint(), when required
3) you seem to be creating drawingPanel numerous times (i.e. inside the for loop) - no big problem, as there's only one of them.
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Michael Thanks for your reply. I will make the changes and see if It Works or Not.

 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Micheal
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Its still Not Working.

and it's still not in code tags - so most won't read it.

drawingPanel.setPreferredSize(new Dimension(200, 200));
g.drawRect(450, 450, 150, 150 );

does anything look funny/silly here?

it's 200 wide, and you wan it to start the rect at 450?

> drawingPanel.repaint();

ask yourself *when* you want the drawingPanel to repaint itself.
the repaint code goes there - as mentioned earlier, it will not automatically repaint

when you fix those - your next question will be answered by using super.paintComponent(g);
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Micheal,

that was really so stupid of me. Well it worked now. Thanks for your Help. Just a Quick Question .. I do not see the code Button anywhere. if you could guide me where can i see that. Next to Quote , there is no code button.

Thanks
ASingh
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Micheal
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for your Help. Just a Quick Question .. I do not see the code Button anywhere. if you could guide me where can i see that. Next to Quote , there is no code button.


To the right of the "Quote" button is a drop down to select the programming language and to the right of that is a button labeled "Code", that is the button to press once you have highlighted your code.
Also read this UseCodeTags (← click).
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

Thanks for your Help. Just a Quick Question .. I do not see the code Button anywhere. if you could guide me where can i see that. Next to Quote , there is no code button.


To the right of the "Quote" button is a drop down to select the programming language and to the right of that is a button labeled "Code", that is the button to press once you have highlighted your code.
Also read this UseCodeTags (← click).



I got it .. Thanks Tony.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My pleasure.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic