Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Another applet to program problem....

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Another applet to program problem....

    I do not know why my teacher decided she wanted all our handed in work to be like this. So far I complete almost all my assignments to be like this but this one. This is just a small quiz in a Jframe.

    This is my file to run it as an application:
    import java.awt.*;
    public class go {
      public static void main(String[] args) {
            Quiz test = new Quiz();
    		  TheApplet myApplet = new TheApplet(); 
    	     Frame myFrame = new Frame("Applet Holder"); 
     
        myApplet.init();	
     
        myFrame.add(myApplet, BorderLayout.CENTER);
        myFrame.pack(); 
        myFrame.setVisible(true);
      } 
    }

    This is my Jframe:

    import java.io.*;
    import java.awt.*;
    import java.util.*;
    import java.applet.*;
    import javax.swing.*;
    import java.awt.event.*; 
    import javax.swing.event.*;
    import java.util.Random;
     
     
     
    public class Quiz extends JFrame implements ActionListener
    {
      public Font titleFont;
      public Font buttonFont;
      public Font normalFont;
      public Image head;
      public Button BA, BB,BC, BD, BNQ;
      public int score = 0, questionsAsked = 0;
      public Label scoreLabel;
      public Label statusLabel;
      public String scoreString;
      public final int NUMQUESTIONS = 5;       //this is a constant
      public final int NUMLINES = 4;           //also a constant
      public String messageString[][];         //will be 6 msgs by 4 lines
      public int currentQuestion = 0;
      public int count = 0;
      public char answer[];
      public RandInt randNumGen;               //random num generator
     
     
      public void init()
      {
         setLayout(null);
         setSize(640,550);                      //Size of screen
         setBackground (Color.cyan);
         titleFont = new Font ("SansSerif", Font.BOLD, 28);
         buttonFont = new Font ("Dialog", Font.BOLD, 16);
         normalFont = new Font ("SansSerif", Font.PLAIN, 16);
         head = getImage(getCodeBase(), "head.gif");
     
     
         BA = new Button("A");                    //Button A
         BA.setBounds(485,120,60,36);
         BA.setFont(buttonFont);
         BA.setBackground(Color.lightGray);
         BA.addActionListener(this);
         add(BA);
         BB = new Button("B");                    //Button B
         BB.setBounds(485,180,60,36);
         BB.setFont(buttonFont);
         BB.setBackground(Color.lightGray);
         BB.addActionListener(this);
         add(BB);
         BC = new Button("C");                    //Button C
         BC.setBounds(485,240,60,36);
         BC.setFont(buttonFont);
         BC.setBackground(Color.lightGray);
         BC.addActionListener(this);
         add(BC);
         BD = new Button("D");                    //Button D
         BD.setBounds(485,300,60,36);
         BD.setFont(buttonFont);
         BD.setBackground(Color.lightGray);
         BD.addActionListener(this);
         add(BD);
     
     
         BNQ = new Button("New Quiz");            //Button New Quiz
         BNQ.setBounds(445,360,140,36);
         BNQ.setFont(buttonFont);
         BNQ.setBackground(Color.lightGray);
         BNQ.addActionListener(this);
         add(BNQ);
     
         //initialise score and add to layout
         scoreString = "Score: " + score + " / " + questionsAsked;
         scoreLabel = new Label(scoreString);
         scoreLabel.setBounds(20,240,240,28);
         scoreLabel.setFont(normalFont);
         add(scoreLabel);
     
    	 statusLabel = new Label("");
    	 statusLabel.setBounds(20,300,440,28);
    	 statusLabel.setFont(normalFont);
    	 add(statusLabel);
     
         //initialise messages and answers
         messageString = new String[NUMQUESTIONS+1][NUMLINES];
         answer = new char[NUMQUESTIONS+1];
         messageString[0][0] = "End of quiz";
         messageString[0][1] = "Please click on the New";
         messageString[0][2] = "Quiz button to restart";
         messageString[0][3] = "";
         answer[0] = 'Z';
         messageString[1][0] = "Is a wombat (A) a quadruped Australian Mammal";
         messageString[1][1] = "(B)a kind of flying mammal ";
         messageString[1][2] = "(C)alien  or (D)human";
         messageString[1][3] = "";
         answer[1] = 'A';
         messageString[2][0] = "What is the cube root of 64?";
         messageString[2][1] = "(A)8  (B)4";
         messageString[2][2] = "(C)7  (D)12";
         messageString[2][3] = "";
         answer[2] = 'B';
         messageString[3][0] = "Who made it to the South Pole First";
         messageString[3][1] = "(A) Amundsen or (B) Scott?";
         messageString[3][2] = "(C) Nelson or (D) Jonathan?";
         messageString[3][3] = "";
         answer[3] = 'A';
         messageString[4][0] = "When did the first men walk on the moon?";
         messageString[4][1] = "(A)1970 or (B)1969";
         messageString[4][2] = "(C) 1965, or (D) 1980?";
         messageString[4][3] = "";
         answer[4] = 'B';
         messageString[5][0] = "How many teeth does a human have?";
         messageString[5][1] = "(A)42, or  (B)50 ";
         messageString[5][2] = "(C)100,or  (D)45";
         messageString[5][3] = "";
         answer[5] = 'A';
     
         //create random integer generator with numbers 1 - NUMQUESTIONS
         randNumGen = new RandInt(NUMQUESTIONS);
     
         //choose first question
         currentQuestion = randNumGen.nextVal();
     
      }
     
      public void paint(Graphics g)
      {
        // display title
        g.setColor(Color.red);
        g.setFont(titleFont);
        g.drawString("Quiz Applet", 20, 40);
        g.drawImage(head, 280, 10, this);
     
        //display the current question
        g.setColor(Color.black);
        g.setFont(normalFont);
        for (int n = 0; n < NUMLINES; n++)
        {
          g.drawString(messageString[currentQuestion][n], 20, 120+n*26);
        }
      }
     
      public void actionPerformed(ActionEvent event)
      {
        String command = event.getActionCommand();
        if (command.equals("New Quiz"))
        {
          score = questionsAsked = 0;
          randNumGen.reSort();
          currentQuestion = randNumGen.nextVal();
          scoreString = "Score: " + score + " / " + questionsAsked;
          scoreLabel.setText(scoreString);
          repaint();
          return;
        }
     
    	else if (command.equals(Character.toString(answer[currentQuestion]))) {
    		score++;
    		questionsAsked++;
    		scoreString = "Score: " + score + " / " + questionsAsked;
    		scoreLabel.setText(scoreString);
    		statusLabel.setText(""); // or do statusLabel.setText("Correct!");
    		count = 0;
     
    	}
     
    	else {
    		count++;
    		if (count == 1) {
    			scoreString = "Score: " + score + " / " + questionsAsked;
    			scoreLabel.setText(scoreString);
    			statusLabel.setText("Wrong, try again.");
    			return;
    		}
    		else {
    			count = 0;
    			questionsAsked++;
    			scoreString = "Score: " + score + " / " + questionsAsked;
    			scoreLabel.setText(scoreString);
    			statusLabel.setText("Wrong again, correct answer to previous question is " + answer[currentQuestion]);
    		}
    	}
     
    	if (questionsAsked >= NUMQUESTIONS)
    		currentQuestion = 0;
    	else
    		currentQuestion = randNumGen.nextVal();
    	repaint();
      }
    }

    When I run compiler after these changes, now my program is broken. This is the compiler message I am receiving.

    Quiz.java:29: cannot find symbol
    symbol  : class RandInt
    location: class Quiz
      public RandInt randNumGen;               //random num generator
             ^
    go.java:5: cannot find symbol
    symbol  : class TheApplet
    location: class go
    		  TheApplet myApplet = new TheApplet(); 
    		  ^
    go.java:5: cannot find symbol
    symbol  : class TheApplet
    location: class go
    		  TheApplet myApplet = new TheApplet(); 
    		                           ^
    Quiz.java:40: cannot find symbol
    symbol  : method getCodeBase()
    location: class Quiz
         head = getImage(getCodeBase(), "head.gif");
                         ^
    Quiz.java:123: cannot find symbol
    symbol  : class RandInt
    location: class Quiz
         randNumGen = new RandInt(NUMQUESTIONS);
                          ^
    5 errors

    Pretty sure I imported the correct group.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Another applet to program problem....

    I assume the class RandInt is some class you have inside your package? If it's outside your package you will have to add an import for it.

Similar Threads

  1. Applet paint program frame work.
    By ShaunB in forum Java Swing Tutorials
    Replies: 4
    Last Post: October 31st, 2012, 08:44 AM
  2. [SOLVED] Help with my Java program: Applet that draws 5 random circles.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 27th, 2012, 01:20 AM
  3. Creating an Applet program using JTextFields, JLabel, Jbutton.
    By Maxly in forum Java Theory & Questions
    Replies: 1
    Last Post: March 9th, 2012, 02:39 PM
  4. java applet program question (on getting path fails)
    By hellocheese in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 30th, 2011, 04:34 PM
  5. Applet paint program frame work.
    By ShaunB in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: May 5th, 2010, 06:35 PM

Tags for this Thread