Results 1 to 9 of 9
  1. #1
    timalloy is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Display an object twice from same class

    Hello everyone,

    I am trying to get an object to display twice from a class in an applet.

    Java Code:
    if (label == 4) {
    		  game.stop();
    		  timer.start();
    		  result.setText("  You Have Destroyed the World");
    		  marFoundDisplay.setText("Martian: " + String.valueOf(mCount));
    		  jupFoundDisplay.setText("Jupiterian: " + String.valueOf(jCount));
    		  highScoreDisplay.setText("HighScore : " + String.valueOf(HighScore));
    		  j.draw(g, 350, 150);
    		  j.draw(g, 350, 150);
    Obviously calling it twice doesn't get the desired effect, I'm ultimately trying to get them to be side by side in the middle of the applet, but clearly what I've done won't achieve that.

    Can you provide me an example that would allow me to display the code below to have the item be displayed twice side by side?

    Java Code:
    j.draw(g, 350, 150);

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

    Default Re: Display an object twice from same class

    have the item be displayed twice side by side
    Change the x value for the second draw to have the image draw at a new location
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    timalloy is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Display an object twice from same class

    Quote Originally Posted by Norm View Post
    Change the x value for the second draw to have the image draw at a new location
    I tried that, however, it only draws one of the items still. I tried changing the x and the y at different builds and it still only displayed 1 drawing:

    Java Code:
    j.draw(g, 350, 450);
    Java Code:
    j.draw(g, 650, 150);

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

    Default Re: Display an object twice from same class

    Please post the code with the two draw() statements that only draw one image.

    What class in the draw() method in?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    timalloy is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Display an object twice from same class

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    /*
     * Description: Jupiterian class that descends from Alien. Has a constructor set that of the
     * Alien data fields with their own unique values.
     * Date: March 31, 2014
     */
    
    public class Jupiterian extends Alien //this class will extend the Alien class so that we can display the features of the Jupiterian.
    {
      public Jupiterian()
      {
        super(4, 4, "green"); //sets 4 eyes, 4 legs, and the skin color for the Jupiterian.
      }
      
      public void draw(Graphics j, int x, int y) {
    	    j.setColor(Color.GREEN); // face outline
    	    j.drawOval(140, 30, 100, 100);
    	    j.setColor(Color.RED); // eyes
    	    j.fillOval(150, 70, 10, 10);
    	    j.fillOval(173, 70, 10, 10);
    	    j.fillOval(195, 70, 10, 10);
    	    j.fillOval(220, 70, 10, 10);
    	    j.setColor(Color.BLUE); // mouth
    	    j.drawLine(200, 100, 170, 100);
    	    j.setColor(Color.GREEN); //arms
    	    j.drawLine(230, 110, 215, 140);
    	    j.drawLine(150, 110, 160, 140);
    	    j.setColor(Color.GREEN); // legs
    	    j.drawLine(170, 127, 150, 160);	 
    	    j.drawLine(175, 126, 170, 160);
    	    j.drawLine(180, 128, 190, 160);
    	    j.drawLine(185, 127, 210, 160);
      }
        
      public String toString()
      {
        return("Jupiterians have: " + getNumEyes() + " eyes, " +
                getNumLegs() + " legs, and are the color " + getSkinColor()); //returns the values for quantity of eyes, legs, and the skin color for the Jupiterian.
      }
    }
    It supposed to display the alien once everytime the button is clicked for it. But at the end of the game, I want it to display the alien twice.
    Last edited by timalloy; 05-02-2014 at 07:11 PM.

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

    Default Re: Display an object twice from same class

    That method ignores the and y values that are passed to it? All the draw methods have hardcoded, constant values.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    timalloy is offline Member
    Join Date
    Apr 2014
    Posts
    12
    Rep Power
    0

    Default Re: Display an object twice from same class

    Quote Originally Posted by Norm View Post
    That method ignores the and y values that are passed to it? All the draw methods have hardcoded, constant values.
    Is there no way to override those values keeping them in a "box" and having them display wherever I want?

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

    Default Re: Display an object twice from same class

    The draw() method you posted needs to be modified to use the x and y values that are passed to it.
    keeping them in a "box"
    The caller of the draw() method should validate the x,y values and change them as required.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Display an object twice from same class

    As I see it, you have a way to draw a character. What you want to do is change the location where you draw the character. Easiest method is for you to translate the origin.

    First thing in your draw method, do this:
    Java Code:
    j = j.create();  // create a temporary, disposable graphics context.
    j.translate(x,y);  // this positions the origin where you want to draw the character.
    Then, just before you leave the draw method.

    Java Code:
    j.dispose();  // you're done with this graphics context.
    Regards,
    Jim
    Last edited by jim829; 05-02-2014 at 07:33 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  3. Replies: 1
    Last Post: 11-23-2010, 06:26 PM
  4. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  5. Replies: 3
    Last Post: 03-15-2010, 08:44 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
  •