944,119 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2826
  • Java RSS
Oct 31st, 2004
0

Spelling Test using a button GUI environment..how do I create a brand new window?

Expand Post »
This is my skeleton of my very basic computing project, which is to create a spelling test displaying twenty questions:

Java Syntax (Toggle Plain Text)
  1. //
  2. // spell_test.java
  3. // spell_test
  4. //
  5. // Created by chocobo74 on 21/09/2004.
  6. // Copyright (c) 2004 __MyCompanyName__. All rights reserved.
  7. // A simple Java applet
  8. //
  9.  
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.applet.*;
  13.  
  14. public class TrivialApplet extends Applet implements ActionListener {
  15.  
  16. private Button beuts, boots, bootz, bots, next_q;
  17. private Image image1, image2, image3, image4, image5;
  18. private AudioClip sound1, sound2;
  19. private int choice;
  20.  
  21. public void init () {
  22. Label title1;
  23. title1= new Label ("How do you spell a pair of rather large shoes?");
  24.  
  25. add(title1);
  26.  
  27. image1=getImage (getDocumentBase(), "beuts.jpg");
  28. beuts = new Button ("beuts");
  29. add(beuts);
  30. beuts.addActionListener(this);
  31.  
  32. image2=getImage (getDocumentBase(), "boots.jpg");
  33. boots = new Button ("boots");
  34. add(boots);
  35. boots.addActionListener(this);
  36.  
  37. image3=getImage (getDocumentBase(), "bootz.jpg");
  38. bootz= new Button ("bootz");
  39. add(bootz);
  40. bootz .addActionListener(this);
  41.  
  42. image4=getImage (getDocumentBase(), "bots.jpg");
  43. bots= new Button ("bots");
  44. add(bots);
  45. bots.addActionListener(this);
  46.  
  47. next_q = new Button ("Next Question");
  48. add(next_q);
  49. next_q.addActionListener(this);
  50.  
  51. image5=getImage (getDocumentBase(), "these.jpg");
  52.  
  53. sound1=getAudioClip(getDocumentBase(),"spam_song.au");
  54. sound2=getAudioClip(getDocumentBase(),"doh.au");
  55. //these declare the buttons, images and sounds to be used//
  56.  
  57. }
  58.  
  59. public void paint (Graphics g) {
  60.  
  61. g.drawImage(image5,0,10,100,150,this);
  62.  
  63. if (choice == 1){ //this method declares what will happen when the user chooses a button//
  64. g.drawImage(image1,200,70,100,150,this);
  65. g.drawString ("Incorrect answer!",250,60);
  66. sound2.play();
  67. sound1.stop();
  68. }
  69.  
  70. if (choice == 2){
  71. g.drawImage(image2,200,70,100,150,this);
  72. g.drawString ("Correct answer!",250,60);
  73. sound1.play();
  74. sound2.stop();
  75. }
  76.  
  77. if (choice == 3){
  78. g.drawImage(image3,200,70,100,150,this);
  79. g.drawString ("Incorrect answer! ",250,60);
  80. sound2.play();
  81. sound1.stop();
  82. }
  83.  
  84. if (choice == 4){
  85. g.drawImage(image4,200,70,100,150,this);
  86. g.drawString ("Incorrect answer!",250,60);
  87. sound2.play();
  88. sound1.stop();
  89. }
  90.  
  91. if (choice == 5){
  92. g.drawString("go to next question",250,60);
  93. }
  94. }
  95.  
  96. public void actionPerformed(ActionEvent event) {
  97.  
  98. if (event.getSource () == beuts)
  99. choice=1;
  100. if (event.getSource () == boots) // if user presses a button,this method declares their choices//
  101. choice=2;
  102. if (event.getSource () == bootz)
  103. choice=3;
  104. if (event.getSource () == bots)
  105. choice=4;
  106. if (event.getSource () == next_q)
  107. choice=5;
  108.  
  109.  
  110.  
  111. repaint();
  112.  
  113.  
  114. }
  115.  
  116. }
I've tried using the repaint method to redraw the screen and wish to create a window with four new buttons, images and sounds for the next question. I will also shortly be adding arrays (which i know how to do) but how do i create a new window? Also tried WindowListener closing window method, got an error in Metrowerks Code Warrior 3.2 for Mac OS 9.2.1. Any suggestions?
Last edited by alc6379; Nov 1st, 2004 at 12:04 am. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chocobo74 is offline Offline
1 posts
since Oct 2004
Oct 31st, 2004
0

Re: Spelling Test using a button GUI environment..how do I create a brand new window?

couldnt you make a new frame and just hide the one that called the new frame ??
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004

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: Cannot utilize an array properly
Next Thread in Java Forum Timeline: Would like for someone to check this to see if works and any help if it does not





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


Follow us on Twitter


© 2011 DaniWeb® LLC