Page 1 of 4 123 ... LastLast
Results 1 to 20 of 76
Like Tree1Likes

Thread: Applet Game

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

    Default Applet Game

    Alright, so at this point I am pretty desperate. I am taking a distance ed course and it is a disaster (text is terrible, "tutor" never responds, resources a scarce and useless). I have one final project to do and I am really stuck because I do not understand how to make it work from a theoretical or practical stand point.
    What I need to do is create a simple applet game where I use two existing classes (Martian and Jupiterian) to show up in the boxes of the game grid that I have created as an applet. The grid needs to filled with buttons that when clicked, will display one of the two images. In total there should be six martians and two jupiterians. I will post what I have so far (which is not particularly good) and if anyone has any guidance I would really really appreciate it as I just want to get out of this course and move on.

    Java Code:
    import java.applet.*;  
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    public class MartianGame extends Applet 
    {
         public void init() 
         {
    
         } 
    
         public void stop() 
         {  
         } 
     
    
         public void paint(Graphics g) 
         {
           //draws a grid
           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);
           
    
         }
    
    }
    Java Code:
    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.*;
    
    public class Jupiterian extends Alien
    {
      //this sets the number of eyes, number of legs, and number of arms for the alien
      public Jupiterian()
      {
        super(1,4,2);
      }
      //this sets the information to be displayed in the dialogue box 
      public String toString()
      {
        return("The Jupiterian has " + getNumEyes() + " eyes, " +
                getNumLegs() + " legs, and " + getNumArms() + " arms.");
      }
    }
    
    //this class creates the frame for the drawing of the alien
    class DrawJupiterian extends JFrame
    {
      //this method sets the properties for the frame
      public void JShapes2D()
      {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawJupiterian frame = new DrawJupiterian();
        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;
        
        Ellipse2D.Float body = new Ellipse2D.Float(100F, 90F, 200F, 200F);
        Ellipse2D.Float smallEye = new Ellipse2D.Float(140, 150, 20, 20);
        Ellipse2D.Float bigEye = new Ellipse2D.Float(130, 130, 40, 40);
        Ellipse2D.Float nose = new Ellipse2D.Float(190, 200, 20, 20);
        QuadCurve2D curveSmile = new QuadCurve2D.Float(150, 220, 150, 250, 250, 220);
        Line2D lineLeg1 = new Line2D.Float(140, 270, 140, 330);
        Line2D lineLeg2 = new Line2D.Float(180, 290, 180, 330);
        Line2D lineLeg3 = new Line2D.Float(220, 290, 220, 330);
        Line2D lineLeg4 = new Line2D.Float(260, 270, 260, 330);
        Line2D lineLeftArm = new Line2D.Float(100, 210, 70, 290);
        Line2D lineRightArm = new Line2D.Float(300, 210, 330, 290);
                
        gr2D.draw(body);
        gr2D.fill(smallEye);
        gr2D.draw(bigEye);
        gr2D.fill(nose);
        gr2D.draw(curveSmile);
        gr2D.draw(lineLeg1);
        gr2D.draw(lineLeg2);
        gr2D.draw(lineLeg3);
        gr2D.draw(lineLeg4);
        gr2D.draw(lineLeftArm);
        gr2D.draw(lineRightArm);
      }
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    
    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);
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    Work on it one step at a time:
    grid needs to filled with buttons
    Build a grid of buttons.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    Quote Originally Posted by Norm View Post
    Work on it one step at a time:

    Build a grid of buttons.
    I have added buttons to the code, but do not know how to specify their position; it does not seem to work in the same way as a Swing GUI. And it is giving me an error for the "implements MouseListener"

    Java Code:
         public class MartianGame extends Applet //implements MouseListener
    {
      Button topLeft; 
      Button topMid;
      Button topRight;
      Button midLeft;
      Button midRight;
      Button botLeft;
      Button botMid;
      Button botRight;
      
         public void init() 
         {
           setLayout(new FlowLayout()); 
           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");
           
           add(topLeft);
           add(topMid);
           add(topRight);
           add(midLeft);
           add(midRight);
           add(botLeft);
           add(botMid);
           add(botRight);
    
         }
    Last edited by shodai; 04-05-2014 at 01:45 AM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    it does not seem to work in the same way as a Swing GUI.
    The code is using a FlowLayout layout manager. It should work the same way in an Applet as in a Swing container.
    What is different?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I would expect that I would use coordinates after the addition of the button.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    What layout manager are you going to use?

    How do you want the components positioned on the GUI?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I do not which layout manager to use because well......I did not know that there were options. I have the grid set up the way that I want and I would like a button to be located in the approximate centre of each box in the grid.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    If you want to specify the x,y locauons of the buttons, set layout null ands use the button's setBounds method
    Nozz likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    Alright, so I have been able to set the buttons in the locations that I want, but the setSize function does not seem to be working. Any advice?

    Java Code:
    import java.applet.*;  
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
      
    public class MartianGame extends JApplet //implements MouseListener
    {
      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() 
      {
        setSize(500,500);
        contentPanel = (JPanel)getContentPane();
        contentPanel.setLayout(null);
        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");
        
        //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);
      } 
    
      public void stop()
      {  
      } 
    
      public void paint(Graphics g) 
      {
        //draws a grid
        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);
      }
    
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    setSize function does not seem to be working
    Please explain what happens when the code is executed.

    How is the applet being executed? What is in the applet tag in the html?
    Last edited by Norm; 04-06-2014 at 11:17 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I have just been running the applet in my compiler (Dr Java). As it stands now it starts up fine, but only shows about 1/4 of the screen size.

    I have been confused with the execution of applets and my text and teacher are not too useful. Do I need to make an HTML file to run an applet? Do I just make it in a text editor? How do I go about running the file?

    Sorry for all the questions.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    only shows about 1/4 of the screen
    What is the size of the screen? Is 1/4 of it 500x500?
    Are you saying the applet uses 1/4 of the monitor?

    There are several html pages with applets on the forum. Do a Search.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    First, i want you to know that I really appreciate your help and that I really am trying; so I am sorry if this is dumb. But what is wrong with the following code?

    <html>
    <object code = "MartianGame.class" width = 800 height = 500>
    *</object>
    </html>

    When I click on the file it opens in a web page but it just displays the code???

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    Change object to applet and remove the *
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I have changed the code to the following but it is still just showing the code in the web browser??

    <html>
    <applet code = "MartianGame.class" width = 800 height = 500>
    </applet>
    </html>

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    Copy and paste here what is shown in the browser's window.

    Try opening the html file in the AppletViewer program.
    Last edited by Norm; 04-07-2014 at 03:16 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    This is what prints out on the screen (I added the body code to the text document)

    <html>
    <body>
    <applet code = "MartianGame.class" width = "800" height = "500">
    </applet>
    </body>
    </html>

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    What is the name of the file that contains the html?

    What happens when you open the file with the AppletViewer program?
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    The HTML file is called MartianGame.html. I do not seem to be able to identify the AppletViewer as I am using Mac and cannot find instructions on line for guidance.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Applet Game

    The AppletViewer program is in the JDK's bin folder on my PC
    I don't know anything about a Mac
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 4 123 ... LastLast

Similar Threads

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