943,692 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1310
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 31st, 2008
0

Something about Graphics

Expand Post »
Good day!

hello! lately i've been working on a project of mine and im almost done. but right now i have a problem. in my hangman program i cant seem to make thing that hangs the hangman appear, it will only appera after ive clicked the check button. can anyone help me please? the code is below.

P.S.

I've already tried the:
Java Syntax (Toggle Plain Text)
  1. public void paintComponent (Graphics g)
  2. {
  3. super.paintComponent(g);
  4. g.setColor(Color.red);
  5. g.drawLine(130,450,240,450);
  6. g.drawLine(185,40,185,450);
  7. g.drawLine(110,40,185,40);
  8. g.drawLine(110,40,110,100);
  9. }

using the code above the thing (that hangs the man) still wont appear when i execute the program.
any help is greatly appreciated





Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Hangman extends JFrame implements ActionListener {
  5.  
  6. // DECLARATIONS
  7.  
  8. JLabel inputL,
  9. lettersL,
  10. wordL;
  11.  
  12. JTextField inputTF,
  13. lettersTF,
  14. wordTF;
  15.  
  16. JButton checkB,
  17. exitB;
  18.  
  19. final String WORD[] = { "SPLINTER" , "MAGICAL" , "FUNDAMENTAL" , "ONYX" , "UNIVERSAL"
  20. , "MYSTERIOUS" , "QUAIL" , "DISCOVER" , "UNIQUE" , "OLYMPICS" };
  21.  
  22. static int index;
  23.  
  24. int chances = 6;
  25.  
  26. char[] blanks,guess;
  27.  
  28. String usedLetters = "";
  29.  
  30. public Hangman()
  31. {
  32. JPanel thePanel = new JPanel();
  33. thePanel.setBackground(Color.black);
  34.  
  35. inputL = new JLabel("Enter a letter");
  36. lettersL = new JLabel("Used Letters");
  37. wordL = new JLabel("The Word");
  38.  
  39. inputTF = new JTextField();
  40. lettersTF = new JTextField(26);
  41. wordTF = new JTextField(16);
  42.  
  43. inputTF.setFont(new Font("Arial",Font.BOLD,20));
  44. inputTF.setHorizontalAlignment(0);
  45. lettersTF.setFont(new Font("Arial",Font.BOLD,20));
  46. lettersTF.setEditable(false);
  47. lettersTF.setHorizontalAlignment(JTextField.CENTER);
  48. wordTF.setFont(new Font("Arial",Font.BOLD,20));
  49. wordTF.setEditable(false);
  50. wordTF.setHorizontalAlignment(JTextField.CENTER);
  51.  
  52. String text = "";
  53. for(int ctr=0 ; ctr < WORD[index].length() ; ctr++)
  54. text = text + "_ ";
  55. wordTF.setText(text);
  56. blanks = text.toCharArray();
  57.  
  58. checkB = new JButton("Check");
  59. checkB.addActionListener(this);
  60.  
  61. exitB = new JButton("EXIT");
  62. exitB.addActionListener(this);
  63.  
  64. Container pane = getContentPane();
  65. pane.setLayout(new GridLayout(1,2,10,0));
  66.  
  67. Container pane1 = new Container();
  68. pane1.setLayout(new GridLayout(8,1,8,8));
  69. pane1.add(wordL);
  70. pane1.add(wordTF);
  71. pane1.add(lettersL);
  72. pane1.add(lettersTF);
  73. pane1.add(inputL);
  74. pane1.add(inputTF);
  75. pane1.add(checkB,BorderLayout.EAST);
  76. pane1.add(exitB,BorderLayout.SOUTH);
  77.  
  78. pane.add(thePanel);
  79. pane.add(pane1);
  80.  
  81. setResizable(false);
  82. setTitle("Hangman BETA VERSION");
  83. setLocation(120,120);
  84. setVisible(true);
  85. setDefaultCloseOperation(EXIT_ON_CLOSE);
  86. setSize(500,500);
  87. }
  88.  
  89.  
  90. public void actionPerformed (ActionEvent e)
  91. {
  92. Graphics g = getGraphics();
  93. g.setColor(Color.red);
  94. g.drawLine(130,450,240,450);
  95. g.drawLine(185,40,185,450);
  96. g.drawLine(110,40,185,40);
  97. g.drawLine(110,40,110,100);
  98.  
  99. if(e.getSource()==exitB)
  100.  
  101. System.exit(0);
  102.  
  103. else if(e.getSource()==checkB)
  104. {
  105. char letter;
  106. String input;
  107. Boolean correct = false;
  108.  
  109. input = inputTF.getText();
  110. letter = Character.toUpperCase( input.charAt(0) );
  111.  
  112. if(input.equals(null))
  113.  
  114. JOptionPane.showMessageDialog(null,"You Have to Enter Something!","ERROR",JOptionPane.WARNING_MESSAGE);
  115.  
  116. else if( input.length() != 1)
  117.  
  118. JOptionPane.showMessageDialog(null,"ENTER A SINGLE CHARACTER!","ERROR",JOptionPane.WARNING_MESSAGE);
  119.  
  120. else if(Character.isDigit(letter))
  121.  
  122. JOptionPane.showMessageDialog(null,"ENTER A CHARACTER NOT A NUMBER","ERROR",JOptionPane.WARNING_MESSAGE);
  123.  
  124. else
  125. {
  126. guess = WORD[index].toCharArray();
  127.  
  128. for(int ctr =0 ; ctr < WORD[index].length() ; ctr++)
  129. {
  130. if( letter == guess[ctr] )
  131. {
  132. blanks[ctr*2]=guess[ctr];
  133. correct = true;
  134. }
  135. }
  136.  
  137. if (!correct)
  138. {
  139. usedLetters = usedLetters + letter + " ";
  140.  
  141. switch (chances)
  142. {
  143. case 1: g.drawArc(85,150,50,30,0,180); chances--; break;
  144. case 2: g.drawLine(90,130,105,130); g.drawLine(115,130,130,130); chances--; break;
  145. case 3: g.drawLine(70,230,150,230); chances--; break;
  146. case 4: g.drawLine(110,350,70,400); g.drawLine(110,350,150,400); chances--; break;
  147. case 5: g.drawLine(110,180,110,350); chances--; break;
  148. case 6: g.drawOval(70,100,80,80); chances--; break;
  149. }
  150.  
  151. if(chances==0)
  152. {
  153. JOptionPane.showMessageDialog(null,"GAMEOVER!\n The Word is " + WORD[index],"GAMEOVER!",JOptionPane.INFORMATION_MESSAGE);
  154. System.exit(0);
  155. }
  156.  
  157. }
  158. wordTF.setText(new String(blanks));
  159. }
  160.  
  161. lettersTF.setText(new String(usedLetters));
  162. inputTF.setText("");
  163.  
  164. }
  165.  
  166. }
  167.  
  168.  
  169. public static void main (String [] args)
  170. {
  171.  
  172. index = (int) ( ( Math.random() ) * 10 );
  173. Hangman theFrame = new Hangman();
  174.  
  175. }
  176.  
  177.  
  178. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008
Jul 31st, 2008
0

Re: Something about Graphics

your the reason it won't draw is because your doing all of your drawing in your action listener, and it doesn't get called at the start of your program, so nothing gets drawn.

one way i see to solve the problem...

make a new boolean variable, i would call it something like startup and initially set it to true.
then surround everything in your action performed from if(e.getsourc()==exitB) to the end of the action listener with an if(!startup) block. and then in your constructor call checkB.doClick() then set startup to false

or...

for a shorter way put this:
Java Syntax (Toggle Plain Text)
  1. Graphics g = getGraphics();
  2. g.setColor(Color.red);
  3. g.drawLine(130,450,240,450);
  4. g.drawLine(185,40,185,450);
  5. g.drawLine(110,40,185,40);
  6. g.drawLine(110,40,110,100);
at the bottom of your constructor
Last edited by sciwizeh; Jul 31st, 2008 at 11:54 pm.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 1st, 2008
0

Re: Something about Graphics

thanks! ive tried the first way and it kinda works, but the second way wont work, the hanging thing just flashes for a milisecond and then it gets covered up by the black background
Reputation Points: 10
Solved Threads: 0
Light Poster
esy928 is offline Offline
42 posts
since Feb 2008
Aug 1st, 2008
0

Re: Something about Graphics

Use a separate panel for the painting, don't paint directly on the JFrame.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Aug 1st, 2008
0

Re: Something about Graphics

that is what i usually do... but i didn't think he wanted to rewrite his whole project to do that. it would require adding and removing much from his current implementation to account for another class.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 1st, 2008
0

Re: Something about Graphics

He needn't rewrite the whole thing. Just a new panel where the drawing is to occur and use that graphic context to drawn on instead of the JFrame context.

By the way, be sure to dispose of a graphics context obtained with getGraphics() by calling dispose() on the reference when you are finished with it.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Aug 1st, 2008
0

Re: Something about Graphics

ok, sure i forgot about that... i usually extend JPanel so i can give it a functionality which would require rewriting his code. can't be right always oh well
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 1st, 2008
0

Re: Something about Graphics

Yes, extending a component and overriding paintComponent() is the usual and preferable way to do it and this code would certainly benefit from that (drag another window over the frame and see what happens to the hangman graphic), but at a minimum the existing code could draw to a panel (or label) reference with few changes.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Aug 1st, 2008
1

Re: Something about Graphics

Given the above discussion on using a panel to draw on and the fact that overriding paintComponent() really is the better way to go, I'd recommend the following, which is only a minor re-arrangement of your existing code (see comments on changes)
java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Hangman extends JFrame implements ActionListener {
  6.  
  7. // DECLARATIONS
  8. JLabel inputL,
  9. lettersL,
  10. wordL;
  11. JTextField inputTF,
  12. lettersTF,
  13. wordTF;
  14. JButton checkB,
  15. exitB;
  16. final String WORD[] = {"SPLINTER", "MAGICAL", "FUNDAMENTAL", "ONYX", "UNIVERSAL", "MYSTERIOUS", "QUAIL", "DISCOVER", "UNIQUE", "OLYMPICS"};
  17. static int index;
  18. int chances = 6;
  19. char[] blanks, guess;
  20. String usedLetters = "";
  21. // New reference to your hangman panel
  22. JPanel graphicPanel;
  23.  
  24. public Hangman() {
  25. // override paintComponent to call the drawHangman() method
  26. graphicPanel = new JPanel() {
  27. protected void paintComponent(Graphics g) {
  28. super.paintComponent(g);
  29. drawHangman(g);
  30. }
  31. };
  32. graphicPanel.setBackground(Color.black);
  33.  
  34. inputL = new JLabel("Enter a letter");
  35. lettersL = new JLabel("Used Letters");
  36. wordL = new JLabel("The Word");
  37.  
  38. inputTF = new JTextField();
  39. lettersTF = new JTextField(26);
  40. wordTF = new JTextField(16);
  41.  
  42. inputTF.setFont(new Font("Arial", Font.BOLD, 20));
  43. inputTF.setHorizontalAlignment(0);
  44. lettersTF.setFont(new Font("Arial", Font.BOLD, 20));
  45. lettersTF.setEditable(false);
  46. lettersTF.setHorizontalAlignment(JTextField.CENTER);
  47. wordTF.setFont(new Font("Arial", Font.BOLD, 20));
  48. wordTF.setEditable(false);
  49. wordTF.setHorizontalAlignment(JTextField.CENTER);
  50.  
  51. String text = "";
  52. for(int ctr = 0; ctr<WORD[index].length(); ctr++) {
  53. text = text+"_ ";
  54. }
  55. wordTF.setText(text);
  56. blanks = text.toCharArray();
  57.  
  58. checkB = new JButton("Check");
  59. checkB.addActionListener(this);
  60.  
  61. exitB = new JButton("EXIT");
  62. exitB.addActionListener(this);
  63.  
  64. Container pane = getContentPane();
  65. pane.setLayout(new GridLayout(1, 2, 10, 0));
  66.  
  67. Container pane1 = new Container();
  68. pane1.setLayout(new GridLayout(8, 1, 8, 8));
  69. pane1.add(wordL);
  70. pane1.add(wordTF);
  71. pane1.add(lettersL);
  72. pane1.add(lettersTF);
  73. pane1.add(inputL);
  74. pane1.add(inputTF);
  75. pane1.add(checkB, BorderLayout.EAST);
  76. pane1.add(exitB, BorderLayout.SOUTH);
  77.  
  78. pane.add(graphicPanel);
  79. pane.add(pane1);
  80.  
  81. setResizable(false);
  82. setTitle("Hangman BETA VERSION");
  83. setLocation(120, 120);
  84. setVisible(true);
  85. setDefaultCloseOperation(EXIT_ON_CLOSE);
  86. setSize(500, 500);
  87. }
  88.  
  89. /** draws the hangman graphic */
  90. private void drawHangman(Graphics g) {
  91. // the gallows
  92. g.setColor(Color.red);
  93. g.drawLine(130, 450, 240, 450);
  94. g.drawLine(185, 40, 185, 450);
  95. g.drawLine(110, 40, 185, 40);
  96. g.drawLine(110, 40, 110, 100);
  97.  
  98. // intentional switch fall-through to
  99. // draw the man as chances decrease
  100. switch(chances) {
  101. case 0:
  102. g.drawArc(85, 150, 50, 30, 0, 180);
  103. case 1:
  104. g.drawLine(90, 130, 105, 130);
  105. g.drawLine(115, 130, 130, 130);
  106. case 2:
  107. g.drawLine(70, 230, 150, 230);
  108. case 3:
  109. g.drawLine(110, 350, 70, 400);
  110. g.drawLine(110, 350, 150, 400);
  111. case 4:
  112. g.drawLine(110, 180, 110, 350);
  113. case 5:
  114. g.drawOval(70, 100, 80, 80);
  115. }
  116. }
  117.  
  118. public void actionPerformed(ActionEvent e) {
  119.  
  120. if(e.getSource()==exitB) {
  121. System.exit(0);
  122. } else if(e.getSource()==checkB) {
  123. char letter;
  124. String input;
  125. Boolean correct = false;
  126.  
  127. input = inputTF.getText();
  128. letter = Character.toUpperCase(input.charAt(0));
  129.  
  130. if(input.equals(null)) {
  131. JOptionPane.showMessageDialog(null, "You Have to Enter Something!", "ERROR", JOptionPane.WARNING_MESSAGE);
  132. } else if(input.length()!=1) {
  133. JOptionPane.showMessageDialog(null, "ENTER A SINGLE CHARACTER!", "ERROR", JOptionPane.WARNING_MESSAGE);
  134. } else if(Character.isDigit(letter)) {
  135. JOptionPane.showMessageDialog(null, "ENTER A CHARACTER NOT A NUMBER", "ERROR", JOptionPane.WARNING_MESSAGE);
  136. } else {
  137. guess = WORD[index].toCharArray();
  138.  
  139. for(int ctr = 0; ctr<WORD[index].length(); ctr++) {
  140. if(letter==guess[ctr]) {
  141. blanks[ctr*2] = guess[ctr];
  142. correct = true;
  143. }
  144. }
  145.  
  146. if(!correct) {
  147. usedLetters = usedLetters+letter+" ";
  148.  
  149. // update chances and repaint the panel
  150. chances--;
  151. graphicPanel.repaint();
  152.  
  153. if(chances==0) {
  154. JOptionPane.showMessageDialog(null, "GAMEOVER!\n The Word is "+WORD[index], "GAMEOVER!", JOptionPane.INFORMATION_MESSAGE);
  155. System.exit(0);
  156. }
  157. }
  158. wordTF.setText(new String(blanks));
  159. }
  160.  
  161. lettersTF.setText(new String(usedLetters));
  162. inputTF.setText("");
  163. }
  164. }
  165.  
  166. public static void main(String[] args) {
  167. index = (int)((Math.random())*10);
  168. Hangman theFrame = new Hangman();
  169. }
  170. }
(I did not correct or alter anything else in the code - just the graphics)
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Aug 1st, 2008
0

Re: Something about Graphics

Quote ...
Java Syntax (Toggle Plain Text)
  1. graphicPanel = new JPanel() {
  2. protected void paintComponent(Graphics g) {
  3. super.paintComponent(g);
  4. drawHangman(g);
  5. }
  6. };
i didn't know you could do that, cool, that makes some stuff a lot simpler for some of my projects
Last edited by sciwizeh; Aug 1st, 2008 at 7:43 pm.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: EXCEL CSV FILES Handling
Next Thread in Java Forum Timeline: problem with rmi





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC