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.
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.
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).
Consider Paul's rocket mass heater. |