Results 1 to 6 of 6
  1. #1
    janyc71876 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Centering graphics on the graphics window

    I am new to programming and I am following the Programming Methodology Course (iTunesU) taught by Mehran Sahami at Standford. I have been trying to no avail to get my graphics centered in the graphics window. Can you help please?

    p.s please feel free to critique my code. I just want to get better.

    /*File: RobotFace.java
    * This program will create a robot face
    * according to the specifications of problem
    * set two.
    */

    import acm.program.*;
    import acm.graphics.*;
    import java.awt.*;

    Java Code:
    public class robotFace extends GraphicsProgram {
    	/*Defines the width of the robot's head.*/
    	private static final int HEAD_WIDTH = 90;
    	
    	/*Defines the height of the robots head.*/
    	private static final int HEAD_HEIGHT = 120;
    
    	/*Defines the radius of the eye*/
    	private static final int EYE_RADIUS = 10;
    
    	/*Defines the width of the mouth.*/
    	private static final double MOUTH_WIDTH = 40;
    
    	/*Defines the height of the mouth*/
    	private static final double MOUTH_HEIGHT = 13;
    	
    	
    
    	public void run() {
    		addFace(getWidth() / 2, getHeight() / 2);
    		
    	}

    /*Centers the entire face and adds it.*/
    Java Code:
    	
              private void addFace(double cx, double cy) {
    		addHead(cx, cy);
    		addEye(cx - HEAD_WIDTH / 4, cy - HEAD_HEIGHT / 4);
    		addEye(cx + HEAD_WIDTH / 4, cy - HEAD_HEIGHT / 4);
    		addMouth(cx, cy + HEAD_HEIGHT / 4);
    	}
    /*Centers and adds mouth*/

    Java Code:
    	private void addMouth(double cx, double cy) {
    		double x = cx - MOUTH_WIDTH / 2;
    		double y = cy - MOUTH_HEIGHT /2;
    		
    		GRect mouth = new GRect(x, y, MOUTH_WIDTH, MOUTH_HEIGHT);
    		mouth.setFilled(true);
    		mouth.setColor(Color.WHITE);
    		add(mouth);
    		
    	}
    /*Centers and adds eyes*/

    Java Code:
    	private void addEye(double cx, double cy) {
    		double x = cx - EYE_RADIUS;
    		double y = cy - EYE_RADIUS;
    		
    		GOval eye = new GOval(x, y, 2*EYE_RADIUS, 2*EYE_RADIUS);
    		eye.setFilled(true);
    		eye.setColor(Color.YELLOW);
    		add(eye);
    	}
    
    
    	/*Centers and adds head*/
    
    
    	private void addHead(double cx, double cy) {
    		double x = cx - HEAD_WIDTH/ 2;
    		double y = cy - HEAD_HEIGHT / 2;
    		
    		GRect head = new GRect(x, y, HEAD_WIDTH, HEAD_HEIGHT);
    		head.setFilled(true);
    		head.setColor(Color.GRAY);
    		add(head);
    		
    	}
    }
    Last edited by janyc71876; 11-21-2013 at 10:57 PM.

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

    Default Re: Centering graphics on the graphics window

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    What are the formulas you are using to find the x,y location to draw the shape so it is centered?

    Not many people use the acm packages and classes.

    graphics centered in the graphics window
    Can you explain what the problem is? Where does the image show? Where should it show?
    Last edited by Norm; 11-21-2013 at 07:21 PM.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Centering graphics on the graphics window

    The general method for centering something that is w x h in a panel which is width x height. Is to draw it at location (width - w)/2, (height-h)/2. This would be the top left corner of the image. Of course, it depends on the coordinate system your are using. Other factors may also be involved.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    janyc71876 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Centering graphics on the graphics window

    The result should look like this Attachment 5702.


    Instead, it looks like this Attachment 5701.

    The exact assignment is right here http://see.stanford.edu/materials/ic...-handout-2.pdf, assignment two, question two.

    I realize that about the cam packages which is unfortunate when you are seeking help on the internet forums.

  5. #5
    natdizzle's Avatar
    natdizzle is offline Nathan Nelson
    Join Date
    Jan 2009
    Posts
    101
    Rep Power
    0

    Default Re: Centering graphics on the graphics window

    i use jframes getwidth / getheight - imagesize / 2 for both height width to center an image on a jframe

    im doing shape centering which is similar to image centering here: Rotating shape on an affinetransform

  6. #6
    janyc71876 is offline Member
    Join Date
    Nov 2013
    Posts
    3
    Rep Power
    0

    Default Re: Centering graphics on the graphics window

    Thanks everyone for the responses. The images were centered but the applet viewer default settings in Eclipse (200, 200) was too small so I have changed it and now it works just fine.

Similar Threads

  1. help with Graphics 2D
    By jwl in forum New To Java
    Replies: 1
    Last Post: 11-06-2012, 05:14 PM
  2. Replies: 9
    Last Post: 07-28-2011, 10:05 AM
  3. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  4. Scrollbar on graphics output window
    By ann123 in forum AWT / Swing
    Replies: 3
    Last Post: 06-27-2009, 08:47 PM
  5. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM

Tags for this Thread

Posting Permissions

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