executing another class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: executing another class

  1. #1
    Join Date
    Apr 2005
    Posts
    2

    executing another class

    I have an applet, that when it starts up it has a riddle and a little text field for the user to enter the answer. What i want the program to do is that when the user hits the "Enter Answer" button, it clears everything and loads the things from another class.

    Here is the code:

    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class threeRiddles extends Applet implements ActionListener
    {
    	public Font nameFont;
    	public Font buttonFont;
    	public Font buttonFont2;
    	public Image pic;
    	public Button BNQ;
    	public Font normalFont;
    	public String answerToRiddle;
    	public String riddlel1;
    	public String riddlel2;
    	public String riddlel3;
    	public String riddlel4;
    	public String riddlel5;
    	public String riddlel6;
    	public Label riddleLA1;
    	public Label riddleLA2;
    	public Label riddleLA3;
    	public Label riddleLA4;
    	public Label riddleLA5;
    	public Label riddleLA6;
    	public TextField answerRiddle;
    	
    	public void init()
    	{
    		setLayout(null);
    		setSize(500,680);
    		setBackground (Color.blue);
    		
    		normalFont = new Font ("Times New Roman", Font.BOLD, 14);
    		buttonFont = new Font ("Dialog", Font.BOLD, 12);
    		buttonFont2 = new Font ("Dialog", Font.BOLD, 12);
    		nameFont = new Font ("SansSerif", Font.BOLD, 28);
    		
    		pic = getImage(getCodeBase(), "albert14.gif");
    		
    		riddlel1 = "This thing all things devours;";
    		riddlel2 = "Birds, beasts, trees, flowers;";
    		riddlel3 = "Gnaws irons, bites steel;";
    		riddlel4 = "Grinds hard stones to meal;";
    		riddlel5 = "Slays king, ruins town,";
    		riddlel6 = "And beats high mountain down.";
    	    
    		answerRiddle = new TextField();
    		answerRiddle.setBounds(135,180,75,20);
    		add(answerRiddle);
    		
    	    BNQ = new Button("Quit");             
    	    BNQ.setBounds(135,210,100,36); 
    	    BNQ.setFont(buttonFont); 
    	    BNQ.setBackground(Color.yellow);
    	    BNQ.addActionListener(this);
    	    add(BNQ); 
    	    
    	    riddleLA1 = new Label(riddlel1); 
    	    riddleLA1.setBounds(20,70,240,20); 
    	    riddleLA1.setFont(normalFont); 
    	    add(riddleLA1); 
    	    
            riddleLA2 = new Label(riddlel2); 
    	    riddleLA2.setBounds(20,85,240,20); 
    	    riddleLA2.setFont(normalFont); 
    	    add(riddleLA2); 
    
    	    riddleLA3 = new Label(riddlel3); 
    	    riddleLA3.setBounds(20,100,240,20); 
    	    riddleLA3.setFont(normalFont); 
    	    add(riddleLA3); 
    	     
    	    riddleLA4 = new Label(riddlel4); 
    	    riddleLA4.setBounds(20,115,240,20); 
    	    riddleLA4.setFont(normalFont); 
    	    add(riddleLA4); 
    	     
    	    riddleLA5 = new Label(riddlel5); 
    	    riddleLA5.setBounds(20,130,240,20); 
    	    riddleLA5.setFont(normalFont); 
    	    add(riddleLA5); 
    	     
    	    riddleLA6 = new Label(riddlel6); 
    	    riddleLA6.setBounds(20,145,240,20); 
    	    riddleLA6.setFont(normalFont); 
    	    add(riddleLA6);
    	}
    	
    	public void paint(Graphics g) 
    	{ 
    	    g.setColor (Color.red); 
    	    g.setFont (nameFont); 
    	    g.drawString("Java Riddle Game", 20, 40); 
    	    g.drawString("Clue...", 90, 270);
    	    g.drawImage(pic, 90, 275, this); 
        }
    	
    	public void actionPerformed(ActionEvent event) 
    	{
    		String command = event.getActionCommand(); 
    		String answerToRiddle = answerRiddle.getText();
    		If (command.equals("time"));
    		{
    			
    		}
    	} 	
    }
    Thnx for any help.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    I'm not quite certain what you mean by this.

    Do you mean clear the drawing area and the text components, or load
    another applet page, or do you just want to switch to another panel, or... ?
    Last edited by sjalle; 04-19-2005 at 03:18 PM.

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Ok, I've tried to guess it...

    I have commented out the image rendering (since I dont have it here). This
    example shows how to clear out the old contents and displays a new
    panel after the Quit button is clicked. It's obviously not what you want to
    happend, but it illustrates the swapping of guis.
    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    class RiddlesPanel extends Panel {
      public Button BNQ;
      public String answerToRiddle;
      public String riddlel1;
      public String riddlel2;
      public String riddlel3;
      public String riddlel4;
      public String riddlel5;
      public String riddlel6;
      public Label riddleLA1;
      public Label riddleLA2;
      public Label riddleLA3;
      public Label riddleLA4;
      public Label riddleLA5;
      public Label riddleLA6;
      public TextField answerRiddle;
      public ThreeRiddles applet=null;
      public RiddlesPanel (ThreeRiddles applet) {
        this.applet=applet;
         setLayout(null);
         setBackground(Color.blue);
         setup();
      }
      private void setup() {
        riddlel1 = "This thing all things devours;";
        riddlel2 = "Birds, beasts, trees, flowers;";
        riddlel3 = "Gnaws irons, bites steel;";
        riddlel4 = "Grinds hard stones to meal;";
        riddlel5 = "Slays king, ruins town,";
        riddlel6 = "And beats high mountain down.";
    
        answerRiddle = new TextField();
        answerRiddle.setBounds(135, 180, 75, 20);
        add(answerRiddle);
    
        BNQ = new Button("Quit");
        BNQ.setBounds(135, 210, 100, 36);
        BNQ.setFont(ThreeRiddles.buttonFont);
        BNQ.setBackground(Color.yellow);
        BNQ.addActionListener(applet);
        add(BNQ);
    
        riddleLA1 = new Label(riddlel1);
        riddleLA1.setBounds(20, 70, 240, 20);
        riddleLA1.setFont(ThreeRiddles.normalFont);
        add(riddleLA1);
    
        riddleLA2 = new Label(riddlel2);
        riddleLA2.setBounds(20, 85, 240, 20);
        riddleLA2.setFont(ThreeRiddles.normalFont);
        add(riddleLA2);
    
        riddleLA3 = new Label(riddlel3);
        riddleLA3.setBounds(20, 100, 240, 20);
        riddleLA3.setFont(ThreeRiddles.normalFont);
        add(riddleLA3);
    
        riddleLA4 = new Label(riddlel4);
        riddleLA4.setBounds(20, 115, 240, 20);
        riddleLA4.setFont(ThreeRiddles.normalFont);
        add(riddleLA4);
    
        riddleLA5 = new Label(riddlel5);
        riddleLA5.setBounds(20, 130, 240, 20);
        riddleLA5.setFont(ThreeRiddles.normalFont);
        add(riddleLA5);
    
        riddleLA6 = new Label(riddlel6);
        riddleLA6.setBounds(20, 145, 240, 20);
        riddleLA6.setFont(ThreeRiddles.normalFont);
        add(riddleLA6);
      }
      public void paint(Graphics g) {
    
        g.setColor(Color.red);
        g.setFont(ThreeRiddles.nameFont);
        g.drawString("Java Riddle Game", 20, 40);
        g.drawString("Clue...", 90, 270);
        //g.drawImage(pic, 90, 275, this);
      }
    }
    class AnotherPanel extends Panel {
    
      public void paint(Graphics g) {
        g.setColor(Color.red);
        g.setFont(ThreeRiddles.nameFont);
        g.drawString("And here is the other panel", 20, 40);
      }
    
    }
    public class ThreeRiddles extends Applet implements ActionListener {
      public static Font nameFont=new Font("SansSerif", Font.BOLD, 28);
      public static Font buttonFont=new Font("Dialog", Font.BOLD, 12);
      public static Font buttonFont2=new Font("Dialog", Font.BOLD, 12);
      public static Font normalFont=new Font("Times New Roman", Font.BOLD, 14);
      //public Image pic;
      RiddlesPanel riddlesPan=null;
      AnotherPanel anotherPanel=null;
    
      public void init() {
        setLayout(new GridLayout());
        riddlesPan=new RiddlesPanel(this);
        add(riddlesPan);
        setSize(500, 680);
      }
      //pic = getImage(getCodeBase(), "albert14.gif");
    
      public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        String answerToRiddle = riddlesPan.answerRiddle.getText();
        // remove riddlespanel and show anotherpanel
        anotherPanel=new AnotherPanel();
        replacePanel(riddlesPan, anotherPanel);
       }
      public void replacePanel(Panel oldPanel, Panel newPanel) {
        Component [] cmp=this.getComponents();
        for (int i=0; i<cmp.length; i++) {
          if (cmp[i]==oldPanel) {
            this.remove(oldPanel);
            break;
          }
        }
        this.add(newPanel);
        validate();
        repaint();
      }
    }
    Last edited by sjalle; 04-19-2005 at 03:51 PM.

  4. #4
    Join Date
    Apr 2005
    Posts
    2
    I meant so the Once the user enters the correct answer to the riddle,
    it should load everything from another class. In this other class, everything is similar, except for captions/pictures.

    Im sorry i can't explain any better.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Allright

    Then what you need is not a new class, but a class (like the one you
    already have) that is capable of loading "sets" of parameters, where a "set"
    is the info required for a complete screenload (riddle)

    I have hacked up a setup here, I haven't tested it yet ...,
    but I'll do that later today, I bet it crashes BIGTIME now ..
    Attached Files Attached Files
    Last edited by sjalle; 04-21-2005 at 05:52 AM.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Ok I have tested it now.

    The java source and html is attatched. It still has a few quirks, like
    too short labels for some lines, and it lacks the code for answer checking.
    I figured that could be a nice thing for you to do. Also, a "Quit" button
    is an appilcation thing, on an applet like this it should be "Submit answer" or
    something like that.

    A demo can be seen here:

    http://home.no.net/sjalle/demos/ThreeRiddles.html
    Attached Files Attached Files
    Last edited by sjalle; 04-21-2005 at 12:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center