Page 4 of 4 FirstFirst ... 234
Results 61 to 76 of 76
Like Tree1Likes

Thread: Applet Game

  1. #61
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    An example of what?

    See post#50 for an example of passing 3 args to a method.

    The code could pass the x,y,w,h args to the paintMartian() method in addition to the Graphics object. That is where the x,y values are used.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I have to apologize. I have been playing around with this but I am more guessing than anything else. I have been experimenting with the command button #1 and setting drawMartian (0,0) but it does not recognize the variable. I believe that I need to declare this somewhere, but I do not know where as my last attempt was way off base.

  3. #63
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    When you call the paint method you need to pass it the x,y locations to use in drawing:
    Java Code:
      aJupiterian.paintJupiterian(g, theX, theY);
    The values of theX and theY will change to where in the grid you want the drawing to be done.
    Try testing with another button. Button#1 is 0,0. What would Button#2 use for x and y?
    Then Button#3 etc
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I must be missing something really simple. I am getting an error on lines 88 and 106.

    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 boolean shouldDrawMartian = false;
      private boolean shouldDrawJupiterian = false;
      private JPanel contentPanel;
      Button topLeft;
      Button topMid;
      Button topRight;
      Button midLeft;
      Button midRight;
      Button botLeft;
      Button botMid;
      Button botRight;
      public int startX;
      public int startY;
      
      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 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);
        
        if (shouldDrawMartian)
        {
          aMartian.paintMartian(g);
        }
        
        if (shouldDrawJupiterian)
        {
          aJupiterian.paintJupiterian(g, startX, startY);
        }
      }
      
      public void actionPerformed(ActionEvent e)
      {
        
        String command = e.getActionCommand();
        System.out.println(command);
        // if Button was pressed, 
        if ((command).equals("1"))
        {
          topLeft.setVisible(false);
          shouldDrawMartian = true;
        }
        else if ((command).equals("2"))
        {
          topMid.setVisible(false);
          paintJupiterian(0,0);
          shouldDrawJupiterian = true;
        }
        else if ((command).equals("3"))
        {
          topRight.setVisible(false);
        }
        else if ((command).equals("4"))
        {
          midLeft.setVisible(false);
        }
        else if ((command).equals("5"))
        {
          midRight.setVisible(false);
        }
        else if ((command).equals("6"))
        {
          botLeft.setVisible(false);
        } 
        else if ((command).equals("7"))
        {
          botMid.setVisible(false);
        }
        else if ((command).equals("8"))
        {
          botRight.setVisible(false);
        }    
      }
    
    }

  5. #65
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    I am getting an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    This is the error message that I am getting


    2 errors found:
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assign Final/MartianGame.java [line: 88]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assign Final/MartianGame.java:88: paintJupiterian(java.awt.Graphics) in DrawJupiterian cannot be applied to (java.awt.Graphics,int,int)
    File: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assign Final/MartianGame.java [line: 106]
    Error: /Users/brad/Documents/Courses/Mobile App Development/Java Intermediate/Assignments/Assign Final/MartianGame.java:106: cannot find symbol
    symbol : method paintJupiterian(int,int)
    location: class MartianGame

  7. #67
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    paintJupiterian(java.awt.Graphics) in DrawJupiterian cannot be applied to (java.awt.Graphics,int,int)
    The definition of the method does not match how it is being called. One has a single arg, the other has 3 args.
    Change one or the other so they match.

    cannot find symbol
    symbol : method paintJupiterian(int,int)
    The compiler can not find that method in the MartianGame class
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I am still running into an issue. I have made the paint method with three arguments ( public void paintJupiterian(Graphics gr, int x, int y) ) and then I try making the call but I am not sure about a third argument ( aJupiterian.paintJupiterian(gr,0,0); ) I think the error is with the gr, but I do not know what to put there instead.

  9. #69
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    When there are error messages, you need to copy the full text and paste it here.

    I assume the variable: gr is the Graphics object that is passed to the paint() method. The calls the the painting methods need to be made from inside the paint() method.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I tried replacing the g with null and it compiles. I thought I had success, but instead when I run the program I get the following very long error message.

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at DrawJupiterian.paintJupiterian(DrawJupiterian.java :28)
    at MartianGame.actionPerformed(MartianGame.java:106)
    at java.awt.Button.processActionEvent(Button.java:392 )
    at java.awt.Button.processEvent(Button.java:360)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4776)
    at java.awt.Component.dispatchEvent(Component.java:46 04)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:717)
    at java.awt.EventQueue.access$400(EventQueue.java:82)
    at java.awt.EventQueue$2.run(EventQueue.java:676)
    at java.awt.EventQueue$2.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:86)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:690)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:86)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 687)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

  11. #71
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    I tried replacing the g with null and it compiles.
    Of course you can NOT use null for the value of a variable when that variable is expected to have a value.
    That will never work.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    so what value should I be using then? I have tried numbers, true, false, but I am really just guessing.

  13. #73
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    Look at the definition of the method the code is calling and provide the variables or values for the data types that are defined there.
    For example:
    Java Code:
      public void someMethod(String s, int i) {    //  definition for method
        ....
      }
      ...
      someMethod("A String", 12); // call the method with the correct args
      someMethod("what?"); // invalid call, int arg is missing
      someMethod(123);      //  invalid call, String arg is missing
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Applet Game

    I am still stumped on the variable. My method call is

    Java Code:
    public void paintMartian(Graphics gr, int x, int y)
    Then I have the call but I am stumped as to what to put in for the Graphics gr

    Java Code:
    if ((command).equals("1"))
        {
          topLeft.setVisible(false);
          aMartian.paintMartian([COLOR="#FF0000"]???[/COLOR] ,0,0);
          shouldDrawMartian = true;
        }

  15. #75
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Applet Game

    You can NOT call the drawing method from outside of the paint() method because the drawing method needs the Graphics object that is passed to the paint() method.
    The action Listener method needs to tell the paint() method to do the call and call the repaint() method so that the paint() method is called.
    If you don't understand my response, don't ignore it, ask a question.

  16. #76
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,090
    Rep Power
    6

    Default Re: Applet Game

    Quote Originally Posted by Norm View Post
    The action Listener method needs to tell the paint() method to do the call and call the repaint() method so that the paint() method is called.
    My brain just exploded reading that. I think this sentence needs some rude finger pointing, such as pointing out which exact paint method is being referred too ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 4 of 4 FirstFirst ... 234

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
  •