resetting paint graphics

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 77
Reputation: number87 is an unknown quantity at this point 
Solved Threads: 0
number87 number87 is offline Offline
Junior Poster in Training

resetting paint graphics

 
0
  #1
May 12th, 2009
I am doing a game and I need to reset the graphics back to its original picture once a new game begins, however when I click the new game button, everything else resets but the graphics do not even though I called repaint. I have included the code for my main game panel(the new game buttons etc) and the game logic code with paint graphics.

This is my main game panel code.
  1. public class MainPanel extends JPanel implements ActionListener{
  2.  
  3. private JButton newGame, quit;
  4. private JRadioButton onePlayer, twoPlayer;
  5. private JComboBox diff;
  6.  
  7. private Hangman game = new Hangman();
  8.  
  9. public MainPanel (){
  10.  
  11. super();
  12.  
  13. //create menu panel
  14. JPanel menuPanel = new JPanel();
  15. menuPanel.setBorder(BorderFactory.createTitledBorder("Menu"));
  16. menuPanel.setBackground(Color.orange);
  17. menuPanel.setPreferredSize(new Dimension(150, 550));
  18.  
  19. //create game display panel
  20. JPanel gamePanel = new JPanel();
  21. gamePanel.setBorder(BorderFactory.createTitledBorder("Game Display"));
  22. gamePanel.setBackground(Color.white);
  23. gamePanel.setPreferredSize(new Dimension(400, 550));
  24.  
  25. //create game status panel
  26. JPanel statusPanel = new JPanel();
  27. statusPanel.setBorder(BorderFactory.createTitledBorder("Game Status"));
  28. statusPanel.setBackground(Color.yellow);
  29. statusPanel.setPreferredSize(new Dimension(180, 350));
  30.  
  31. //main panel bg color
  32. setBackground(Color.black);
  33.  
  34. //Text labels for game status
  35. JLabel difficulty = new JLabel("Difficulty: ");
  36. JLabel tries = new JLabel("Tries Remaining: " + (6 - game.getNumGuesses()));
  37. JLabel hint = new JLabel("Hint: " );
  38. JLabel currLevel = new JLabel("Level: 1");
  39.  
  40. //create game menu buttons
  41. newGame = new JButton("New Game");
  42. quit = new JButton("Quit");
  43.  
  44. //new game button
  45. newGame.setPreferredSize(new Dimension(100,20));
  46. newGame.setToolTipText("Start New Game");
  47. newGame.addActionListener(this);
  48.  
  49. //quit button
  50. quit.setPreferredSize(new Dimension(100,20));
  51. quit.setToolTipText("Exit game");
  52. quit.addActionListener(this);
  53.  
  54. //radio buttons for one/two player modes
  55. onePlayer = new JRadioButton ("One Player", true);
  56. twoPlayer = new JRadioButton ("Two Players");
  57. onePlayer.addActionListener(this);
  58. twoPlayer.addActionListener(this);
  59.  
  60. //group radio buttons
  61. ButtonGroup group = new ButtonGroup();
  62. group.add (onePlayer);
  63. group.add (twoPlayer);
  64.  
  65. //combo box for difficulty settings
  66. String[] level = {"Auto","Level 1", "Level 2", "Level 3"};
  67. diff = new JComboBox (level);
  68. diff.setSelectedIndex(0);
  69. diff.addActionListener(this);
  70.  
  71. //menu panel, buttons, box layout
  72. menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS));
  73. menuPanel.add (Box.createRigidArea(new Dimension (0, 5)));
  74. menuPanel.add (newGame);
  75. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  76. // menuPanel.add (difficulty);
  77. // menuPanel.add (diff);
  78. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  79. menuPanel.add (onePlayer);
  80. menuPanel.add (twoPlayer);
  81. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  82. menuPanel.add (quit);
  83. menuPanel.add (Box.createRigidArea(new Dimension (0, 10)));
  84.  
  85. //game status panel with labels, box layout
  86. statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.PAGE_AXIS));
  87. statusPanel.add (currLevel);
  88. statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
  89. statusPanel.add (hint);
  90. statusPanel.add (Box.createRigidArea(new Dimension (0, 5)));
  91. statusPanel.add (tries);
  92. statusPanel.add (Box.createVerticalStrut(320));
  93.  
  94. //nest panels in main panel
  95. gamePanel.add(game);
  96. menuPanel.add(statusPanel);
  97. add(menuPanel);
  98. add(gamePanel);
  99.  
  100. game.repaint();
  101. }
  102.  
  103.  
  104.  
  105. /*
  106.  * Action Listener for menu buttons.
  107.  * */
  108. public void actionPerformed(ActionEvent e) {
  109.  
  110. if(e.getSource() == newGame){
  111.  
  112. game.resetGame();
  113. game.repaint();
  114. return;
  115.  
  116. }
  117.  
  118.  
  119. if(e.getSource() == quit){
  120. System.exit(1);
  121. }
  122.  
  123.  
  124.  
  125. }
  126.  
  127.  
  128. }

Here is my game logic code with the paint graphics. My game buttons resets and works, I get a new random word too. Only thing that doesnt work is the graphics resetting to original.

  1. public class Hangman extends JPanel implements ActionListener{
  2.  
  3. private boolean used[];
  4.  
  5. private String guessword;
  6.  
  7. private int numguesses;
  8.  
  9. private boolean finished;
  10.  
  11. private Button a[];
  12.  
  13. private boolean won;
  14.  
  15. private RandomWord rw = new RandomWord();
  16.  
  17. public Hangman(){
  18.  
  19. setPreferredSize(new Dimension(350, 350));
  20. initgame();
  21. repaint();
  22.  
  23. }
  24.  
  25.  
  26. public int getNumGuesses(){
  27. return numguesses;
  28. }
  29.  
  30.  
  31. /*
  32.  * initialise the game
  33.  * */
  34. public void initgame() {
  35.  
  36. //initialise game variables
  37. rw.newGame(); //generate random word
  38. guessword = rw.getWord();//get the random word into word buffer
  39.  
  40. won = false;
  41. finished = false;
  42. numguesses = 0;
  43.  
  44. used = new boolean[26];
  45.  
  46. a = new Button[26];
  47. StringBuffer buffer;
  48. //create keyboard buttons
  49. for (int i = 0; i <26; i++) {
  50. buffer = new StringBuffer();
  51. buffer.append((char)(i+65));
  52. a[i] = new Button(buffer.toString());
  53. a[i].addActionListener(this);
  54. a[i].setEnabled(true);
  55. add(a[i]);
  56. }
  57.  
  58.  
  59.  
  60.  
  61. }
  62.  
  63.  
  64. /*
  65.  * Paint Hangman picture
  66.  *
  67.  * */
  68. @Override
  69. public void paintComponent(Graphics g) {
  70.  
  71.  
  72. Graphics2D g2 = (Graphics2D) g;
  73.  
  74. //draw the hangman pole
  75. setBackground(Color.WHITE);
  76. g2.fillRect(10, 250, 150, 20);
  77. g2.fillRect(40,70,10,200);
  78. g2.fillRect(40,70,60,10);
  79. g2.setColor(Color.GRAY);
  80. g2.fillRect(95,70,5,25);
  81.  
  82. //draw head
  83. g2.setColor(Color.MAGENTA);
  84. if (numguesses >=1 )
  85. g2.drawOval(82,95,30,30);
  86.  
  87. //draw body
  88. g2.setColor(Color.PINK);
  89. if (numguesses >=2 )
  90. g2.drawLine(97,125,97,150);
  91.  
  92. if (numguesses >=3 )
  93. g2.drawLine(97,150,117,183);
  94.  
  95. if (numguesses >=4 )
  96. g2.drawLine(97,150,77,183);
  97.  
  98. if (numguesses >=5 )
  99. g2.drawLine(97,125,117,135);
  100.  
  101. if (numguesses >=6 )
  102. g2.drawLine(97,125,77,135);
  103.  
  104. //fill up the used letters
  105. StringBuffer st = new StringBuffer();
  106. for (int l=0; l<=25; l++) {
  107. if (used[l])
  108. st.append((char)(l+65));
  109. else
  110. st.append("-");
  111. }
  112.  
  113. g2.setColor(Color.BLUE);
  114. Font f = new Font("Times New Roman",Font.ITALIC,14);
  115.  
  116. //display the used letters
  117. g2.setFont(f);
  118. g2.drawString(st.toString(),25,285);
  119. st.delete(0, 26);
  120.  
  121. StringBuffer guessed = new StringBuffer();
  122. Font ff = new Font("Times New Roman",Font.BOLD,24);
  123. g2.setColor(Color.BLACK);
  124. g2.setFont(ff);
  125.  
  126. //display the hidden word as - and letters
  127. for (int mm = 0;mm < guessword.length();mm++) {
  128. if (used[(int)guessword.charAt(mm)-65])
  129. guessed.append(guessword.charAt(mm));
  130. else
  131. guessed.append("-");
  132. }
  133.  
  134. g2.drawString(guessed.toString(),75,230);
  135. guessed.delete(0, guessword.length()+1);
  136.  
  137.  
  138. if (numguesses >=6) {//more than 6 tries = lose
  139. g2.setColor(Color.WHITE);
  140. g2.fillRect(70, 200, 200, 30);
  141. g2.setColor(Color.BLACK);
  142. g2.drawString(guessword.toString(),75,230);
  143. Font fff = new Font("Times New Roman",Font.BOLD,36);
  144. g2.setFont(fff);
  145.  
  146. g2.setColor(Color.RED);
  147. g2.drawString("You lose!",130,150);//display you lose
  148.  
  149. finished = true;//game finish = true
  150. }
  151.  
  152. if (won) {//if win game
  153. Font fff = new Font("Times New Roman",Font.BOLD,36);
  154. g2.setFont(fff);
  155.  
  156. g2.setColor(Color.GREEN);
  157.  
  158. g2.drawString("You Win!",130,150);//display you win
  159.  
  160. finished = true;//game finish = true
  161.  
  162. }
  163.  
  164. repaint();
  165.  
  166. }
  167.  
  168.  
  169. /*
  170.  * Action listener for key buttons
  171.  * */
  172. public void actionPerformed( ActionEvent ev) {
  173.  
  174. for (int i = 0; i < 26; i++) {
  175. if (ev.getSource() == a[i]) {
  176. checker(i);//call check letter method
  177. a[i].setEnabled(false);//disable once pressed
  178.  
  179. }
  180. }
  181. }
  182.  
  183.  
  184. /*
  185.  * Check letter input from user
  186.  * */
  187. public void checker(int letter) {
  188.  
  189. if (!finished) {
  190.  
  191. boolean found = false;
  192. boolean wrong = false;
  193.  
  194. if (!used[letter]) {//if not in the used letter array
  195. for (int mm = 0;mm < guessword.length(); mm++) {
  196. if (((char)(letter+65)) == guessword.charAt(mm))
  197. { System.out.println((char)(letter+65));
  198. found = true;//letter matching found = true
  199. }
  200.  
  201. }
  202. if (!found)//if not matching
  203. numguesses++;//number of guesses used increased
  204. System.out.println(numguesses);
  205.  
  206. }
  207.  
  208. used[letter] = true;//set true when letter is used
  209.  
  210.  
  211. //if word not matching wrong = true
  212. for (int mm = 0;mm < guessword.length(); mm++) {
  213. if (!used[(int)(guessword.charAt(mm))-65])
  214. wrong = true;
  215. }
  216.  
  217. if (!wrong)//if word is not wrong win game
  218. won = true;
  219.  
  220. repaint();
  221. }
  222.  
  223. }
  224.  
  225.  
  226. public void resetGame(){
  227.  
  228. //initialise game variables
  229. rw.newGame(); //generate random word
  230. guessword = rw.getWord();//get the random word into word buffer
  231. used = new boolean[26];
  232. won = false;
  233. finished = false;
  234. numguesses = 0;
  235.  
  236. //reset letter board
  237. for (int i = 0; i <26; i++) {
  238. a[i].setEnabled(true);
  239. }
  240.  
  241.  
  242. }
  243.  
  244. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: toucan is an unknown quantity at this point 
Solved Threads: 1
toucan toucan is offline Offline
Newbie Poster

Re: resetting paint graphics

 
0
  #2
May 12th, 2009
In MainPanel, at the very end, try doing "validate();" right before repainting, so the game object can note any changes that were made.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: resetting paint graphics

 
0
  #3
May 12th, 2009
In Hangman paintComponent(Graphics g)
erase the background first.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,657
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: resetting paint graphics

 
0
  #4
May 12th, 2009
In your paint() method, call super.paintComponent(g). It will clear the screen. Alternatively, you could use this piece of code from inside your paintComponent method:

  1. g.clearRect(0, 0, this.getWidth(), this.getHeight());
Last edited by BestJewSinceJC; May 12th, 2009 at 9:12 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 533 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC