Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 76
Like Tree1Likes

Thread: Applet Game

  1. #21
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    Well, perhaps this is a concern for later. For now I should focus my attention of the programming. Which brings me to my next question; how do I use the command buttons located in MartianGame class access the methods located in a different class?

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    For code in a method in one class to call methods in another class, it needs a reference to an instance of that other class.
    See line 21 and line 56, both of those lines have code that is calling a method in a class using an instance of that class in post#9
    If you don't understand my response, don't ignore it, ask a question.

  3. #23
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    I was experimenting but was not sure if the code was working. I tried something really basic

    Java Code:
      public void actionPerformed(ActionEvent e)
      {
        String command = e.getActionCommand();
        
        // if myButton was pressed, 
        if(topLeft.equals(command))
        {
          topLeft.setVisible(false);
        }
        
      }
    But it does not seem to be working, so I am not sure what I might be doing wrong that the command button does not seem to be working.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    the command button does not seem to be working.
    Try debugging the code by printing out the values of command and topleft at line 4 so you can see what the computer sees when it executes the code.
    If you don't understand my response, don't ignore it, ask a question.

  5. #25
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    Alright, I got the command button working. So the nest step is getting the pictures to appear in the same applet window (right now they open in a new window).

    Java Code:
    import java.applet.*;  
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    public class MartianGame extends JApplet implements ActionListener
    {
      DrawMartian aMartian = new DrawMartian();
      Jupiterian aJupiterian = new Jupiterian();
      
      private JPanel contentPanel;
      Button topLeft;
      Button topMid;
      Button topRight;
      Button midLeft;
      Button midRight;
      Button botLeft;
      Button botMid;
      Button botRight;
      
      private void add(Container con, Component widget, int left, int top, int width, int height)
      {
        widget.setBounds(left, top, width, height);
        con.add(widget);
      }
      
      public void init() 
      {
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null);
        
        //this instantiates the command buttons
        topLeft = new Button("1");
        topMid = new Button("2");
        topRight = new Button("3");
        midLeft = new Button("4");
        midRight = new Button("5");
        botLeft = new Button("6");
        botMid = new Button("7");
        botRight = new Button("8");
        
        //this adds the command buttons to the applet screen
        //left, top, width, height
        add(contentPanel,topLeft,75,130,100,100);
        add(contentPanel,topMid,225,130,100,100);
        add(contentPanel,topRight,375,130,100,100);
        add(contentPanel,midLeft,75,280,100,100);
        add(contentPanel,midRight,375,280,100,100);
        add(contentPanel,botLeft,75,430,100,100);
        add(contentPanel,botMid,225,430,100,100);
        add(contentPanel,botRight,375,430,100,100);
        
        topLeft.addActionListener(this);
      } 
    
      public void start()
      {
      }
      
      public void stop()
      {  
      } 
    
      public void paint(Graphics g) 
      {
        //this draws a grid in the applet window
        g.drawRect(50,100,450,450);
        g.drawLine(200,100,200,550);
        g.drawLine(350,100,350,550);
        g.drawLine(50,250,500,250);
        g.drawLine(50,400,500,400);
        g.fillRect(200,250,150,150);
      }
      
      public void actionPerformed(ActionEvent e)
      {
        String command = e.getActionCommand();
        System.out.println(command);
        // if myButton was pressed, 
        if ((command) == "1")
        {
          topLeft.setVisible(false);
          aMartian.JShapes2D();
        }
        
      }
    
    }
    Java Code:
    /**
     * Alien class
     * This class sets out the variables that will be used by the Martian and Jupiterian classes.
     * @author Brad Strassburger
     * @version March 5, 2014
     */
    
    public abstract class Alien
    {
      //setting up variables to be used.
      private int numEyes;
      protected int numLegs;
      protected int numArms;
      
      //This method acts as a container for the variables for the aliens in other classes.
      public Alien(int numEyes, int numLegs, int numArms)
      {
        setNumEyes(numEyes);
        setNumLegs(numLegs);
        setNumArms(numArms);
      }
    
      public int getNumEyes()
      {
        return numEyes;
      }
      
      public void setNumEyes(int eyes)
      {
        numEyes = eyes;
      }
      
      public int getNumLegs()
      {
        return numLegs;
      }
        
      public void setNumLegs(int legs)
      {
        numLegs = legs;
      }
      
      public int getNumArms()
      {
        return numArms;
      }
      
      public void setNumArms(int arms)
      {
        numArms = arms;
      }
      
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    /**
     * Alien class
     * This class sets out the variables that will be used by the Martian and Jupiterian classes.
     * @author Brad Strassburger
     * @version March 5, 2014
     */
    
    public class Martian extends Alien
    {
      public int xStart;
      public int yStart;
      public JFrame frame;
      
    
      //this sets the number of eyes, number of legs, and number of arms for the alien  
      public Martian()
      {
        super(2, 3, 1); //sets 2 eyes, 3 legs, and 1 arm
      }
      //this sets the information to be displayed in the dialogue box  
      public String toString()
      {
        return("The Martian has " + getNumEyes() + " eyes, " +
                getNumLegs() + " legs, and " + getNumArms() + " arms.");
      }
    }
    
    //this class creates the frame for the drawing of the alien
    class DrawMartian extends JFrame
    {
      //this method sets the properties for the frame
      public void JShapes2D()
      {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawMartian frame = new DrawMartian();
        frame.setSize(500, 400);
        frame.setVisible(true);
      }
    
      //this method draws the shapes that make up the picture of the alien
      public void paint(Graphics gr)
      { 
        super.paint(gr);
        Graphics2D gr2D = (Graphics2D)gr;
        
        Rectangle2D.Float rectBody = new Rectangle2D.Float(100F, 150F, 100F, 100F);
        Rectangle2D.Float rectEye1 = new Rectangle2D.Float(120F, 165F, 10F, 10F);
        Rectangle2D.Float rectEye2 = new Rectangle2D.Float(170F, 165F, 10F, 10F); 
        Rectangle2D.Float rectNose = new Rectangle2D.Float(145F, 190F, 10F, 20F);
        QuadCurve2D curveSmile = new QuadCurve2D.Float(120, 220, 150, 250, 170, 220);
        Line2D lineLeg1 = new Line2D.Float(120, 250, 120, 300);
        Line2D lineLeg2 = new Line2D.Float(145, 250, 145, 300);
        Line2D lineLeg3 = new Line2D.Float(170, 250, 170, 300);
        Line2D lineArm1 = new Line2D.Float(100, 200, 70, 250);    
        
        gr2D.draw(curveSmile);
        gr2D.draw(rectBody);
        gr2D.fill(rectEye1);
        gr2D.fill(rectEye2);
        gr2D.fill(rectNose);
        gr2D.draw(lineLeg1);
        gr2D.draw(lineLeg2);
        gr2D.draw(lineLeg3);
        gr2D.draw(lineArm1);
        gr2D.drawString("I'm Marvin the Martian", 80, 330);
      }
    }

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    getting the pictures to appear in the same applet window (right now they open in a new window).
    What are the names of the classes you are talking about? Which has the pictures?
    If you don't understand my response, don't ignore it, ask a question.

  7. #27
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    The pictures are drawn in the DrawMartians class and the DrawJupiterians class.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    They both extend JFrame so their windows will be separate.
    Which window do you want the pictures to show in?
    If you don't understand my response, don't ignore it, ask a question.

  9. #29
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    I want them to show up in the applet window.

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    Then the code for drawing them should be in a method that is called from the code that draws the applet window, not in the Draw... classes. Those classes shouldn't do any drawing and maybe they should not extend JFrame.
    If you don't understand my response, don't ignore it, ask a question.

  11. #31
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    Does this mean to copy the code and put it into MartianGame class?

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    The code could mostly stay where it is but in a different method that would be called from the code in the applet window to do the drawing.
    If you don't understand my response, don't ignore it, ask a question.

  13. #33
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    So I have paired down my code quite a bit, but when I try to call the paintMartian method, I get the following error.

    1 error found:
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assign Final/MartianGame.java [line: 90]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assign Final/MartianGame.java:90: paintMartian(java.awt.Graphics) in DrawMartian cannot be applied to (int)

    This is the code that I currently have:

    Java Code:
    import java.applet.*;  
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    public class MartianGame extends JApplet implements ActionListener
    {
      DrawMartian aMartian = new DrawMartian();
      DrawJupiterian aJupiterian = new DrawJupiterian();
      
      private JPanel contentPanel;
      Button topLeft;
      Button topMid;
      Button topRight;
      Button midLeft;
      Button midRight;
      Button botLeft;
      Button botMid;
      Button botRight;
      
      private void add(Container con, Component widget, int left, int top, int width, int height)
      {
        widget.setBounds(left, top, width, height);
        con.add(widget);
      }
      
      public void init() 
      {
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null);
        
        //this instantiates the command buttons
        topLeft = new Button("1");
        topMid = new Button("2");
        topRight = new Button("3");
        midLeft = new Button("4");
        midRight = new Button("5");
        botLeft = new Button("6");
        botMid = new Button("7");
        botRight = new Button("8");
        
        //this adds the command buttons to the applet screen
        //left, top, width, height
        add(contentPanel,topLeft,75,130,100,100);
        add(contentPanel,topMid,225,130,100,100);
        add(contentPanel,topRight,375,130,100,100);
        add(contentPanel,midLeft,75,280,100,100);
        add(contentPanel,midRight,375,280,100,100);
        add(contentPanel,botLeft,75,430,100,100);
        add(contentPanel,botMid,225,430,100,100);
        add(contentPanel,botRight,375,430,100,100);
        
        topLeft.addActionListener(this);
        topMid.addActionListener(this);
        topRight.addActionListener(this);
        midLeft.addActionListener(this);
        midRight.addActionListener(this);
        botLeft.addActionListener(this);
        botMid.addActionListener(this);
        botRight.addActionListener(this);
      } 
    
      public void start()
      {
      }
      
      public void stop()
      {  
      } 
    
      public void paint(Graphics g) 
      {
        //this draws a grid in the applet window
        g.drawRect(50,100,450,450);
        g.drawLine(200,100,200,550);
        g.drawLine(350,100,350,550);
        g.drawLine(50,250,500,250);
        g.drawLine(50,400,500,400);
        g.fillRect(200,250,150,150);
      }
      
      public void actionPerformed(ActionEvent e)
      {
        String command = e.getActionCommand();
        System.out.println(command);
        // if myButton was pressed, 
        if ((command) == "1")
        {
          topLeft.setVisible(false);
          aMartian.paintMartian();
        }
        else if ((command) == "2")
        {
          topMid.setVisible(false);
        }
        else if ((command) == "3")
        {
          topRight.setVisible(false);
        }
        else if ((command) == "4")
        {
          midLeft.setVisible(false);
        }
        else if ((command) == "5")
        {
          midRight.setVisible(false);
        }
        else if ((command) == "6")
        {
          botLeft.setVisible(false);
        } 
        else if ((command) == "7")
        {
          botMid.setVisible(false);
        }
        else if ((command) == "8")
        {
          botRight.setVisible(false);
        }    
      }
    
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    
    //this class creates the frame for the drawing of the alien
    public class DrawMartian extends MartianGame
    {
      //this method sets the properties for the frame
    /*  public void JShapes2D()
      {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawMartian frame = new DrawMartian();
        frame.setSize(500, 400);
        frame.setVisible(true);
      }
    */
      //this method draws the shapes that make up the picture of the alien
      public void paintMartian(Graphics gr)
      { 
        super.paint(gr);
        Graphics2D gr2D = (Graphics2D)gr;
        
        Rectangle2D.Float rectBody = new Rectangle2D.Float(100F, 150F, 100F, 100F);
        Rectangle2D.Float rectEye1 = new Rectangle2D.Float(120F, 165F, 10F, 10F);
        Rectangle2D.Float rectEye2 = new Rectangle2D.Float(170F, 165F, 10F, 10F); 
        Rectangle2D.Float rectNose = new Rectangle2D.Float(145F, 190F, 10F, 20F);
        QuadCurve2D curveSmile = new QuadCurve2D.Float(120, 220, 150, 250, 170, 220);
        Line2D lineLeg1 = new Line2D.Float(120, 250, 120, 300);
        Line2D lineLeg2 = new Line2D.Float(145, 250, 145, 300);
        Line2D lineLeg3 = new Line2D.Float(170, 250, 170, 300);
        Line2D lineArm1 = new Line2D.Float(100, 200, 70, 250);    
        
        gr2D.draw(curveSmile);
        gr2D.draw(rectBody);
        gr2D.fill(rectEye1);
        gr2D.fill(rectEye2);
        gr2D.fill(rectNose);
        gr2D.draw(lineLeg1);
        gr2D.draw(lineLeg2);
        gr2D.draw(lineLeg3);
        gr2D.draw(lineArm1);
        gr2D.drawString("I'm Marvin the Martian", 80, 330);
      }
    }

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    Use the equals() method to compare Strings, not the == operator.

    Not sure how to read that error message. The paintMartin() method takes an arg but the call to it is missing the arg.
    If you don't understand my response, don't ignore it, ask a question.

  15. #35
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    Maybe I am looking at the code wrong. This is what I am trying to accomplish. I want to run the applet which will display the grid and numbered command buttons. When a user clicks one of the command buttons I want to draw the picture that I have already created in the DrawMartian class in the location where the command button was. This is where I am stuck, I do not know how to make that call and have the instructions for drawing the alien copied to the applet.

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    Usually the drawing methods are called from the paint() method in the applet and the Graphics argument is passed to the method so it can draw on the same GUI window as the other methods that are doing some drawing.
    If you don't understand my response, don't ignore it, ask a question.

  17. #37
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    So would your suggestion to be to keep everything in the one applet class?

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    No. Methods in other classes can be called from the applet class.
    If you don't understand my response, don't ignore it, ask a question.

  19. #39
    shodai is offline Senior Member
    Join Date
    Apr 2012
    Posts
    106
    Rep Power
    0

    Default Re: Applet Game

    So what am I doing wrong. I try calling the methods but they do not work. I have tried a number of combinations but cannot seem to work it out. What should I do???

  20. #40
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Applet Game

    Can you post the code?
    If you don't understand my response, don't ignore it, ask a question.

Page 2 of 4 FirstFirst 1234 LastLast

Similar Threads

  1. game applet example
    By hasan2222 in forum New To Java
    Replies: 5
    Last Post: 05-29-2013, 04:47 PM
  2. Snake Game Applet
    By Growler in forum Java Applets
    Replies: 6
    Last Post: 07-11-2010, 03:47 PM
  3. Applet to Application (Game)
    By AndrewM16921 in forum Java Applets
    Replies: 3
    Last Post: 04-12-2009, 01:24 PM
  4. An Applet Game
    By T3000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2008, 05:18 PM
  5. Battery 1.02 (Applet Game)
    By M77 in forum Java Software
    Replies: 3
    Last Post: 05-30-2008, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •