View RSS Feed

Messi5454

Help centering the square inside

Rate this Entry
by , 09-14-2013 at 12:40 AM (1635 Views)
Quote Originally Posted by Messi5454 View Post
I emailed the instructor my code and he said the only thing i have to do is center my inner square .
i attached picture
Attachment 5522

Java Code:
import java.awt.*;

import javax.swing.*;


// begining of the Gepmetry class
// geometry class uses JFrame to draw the window and 
// geometric shapes  
public class Hw2  extends JFrame {

//Variables to use for drawing 3 shapes.

//variable for circle
public double cir_x = 0;
public double cir_y = 0;
public double cir_width = 0;
public double cir_height = 0;
public Color cir_color = null;

//variables for square1
public double square_x1 = 0;
public double square_y1 = 0;
public double square_width1 = 0;
public double square_height1 = 0;
public Color square_color1 = null;

//variables for square2
public double square_x2 = 0;
public double square_y2 = 0;
public double square_width2 = 0;
public double square_height2 = 0;
public Color square_color2 = null;

//constructor function
//this function is executed once we define a variable of
//type class geometry in main.
//this function creates the canvas with size of 500 by 500.
//you can change the size if its needed
public Hw2() {

 setSize(new Dimension(500, 500));
 // program will be finished by closing the window 
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 // make the window visible
 setVisible(true);
}

//This function draws the shapes on the window
//and get called automatically ( no need to call it saparely)
//this function uses the variables that are defined above. The value of the
//variables are set in main file.
public void paint(Graphics g) {

     // draws the rectangle without color 
	if(square_color1 == null)
	{
	    g.drawRect((int)square_x1, (int)square_y1, (int)square_width1, (int)square_height1);
		
	}
	else
	{
        // draws rectangle with color 
		g.setColor(square_color1);
	    g.fillRect((int)square_x1, (int)square_y1, (int)square_width1, (int)square_height1);
	}
	

	if (cir_color == null)
	{
		g.drawOval((int)cir_x, (int)cir_y, (int)cir_width, (int)cir_height);
	}
	else
	{
		g.setColor(cir_color);
		g.fillOval((int)cir_x, (int)cir_y, (int)cir_width, (int)cir_height);
	}


   
	if (square_color2 == null)
	{
		    g.drawRect((int)square_x2, (int)square_y2, (int)square_width2, (int)square_height2);
	}
	else
	{
		   g.setColor(square_color2);
		   g.fillRect((int)square_x2, (int)square_y2, (int)square_width2, (int)square_height2);

	}
}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String input = " ";
		double radius = 0,circumfrence,area1,area2,area3,diameter=0;
		
	
				
	input = JOptionPane.showInputDialog("Please enter the diameter of the circle");
	diameter= Integer.parseInt( input );
	
		radius= diameter/2;
		area1 =  Math.PI * (radius *radius);
		area2 = diameter * diameter;
		area3 = 2* (radius * radius);
		circumfrence= Math.PI * diameter;
		area1 =  Math.PI * (radius *radius);
		;
		
		
		JOptionPane.showMessageDialog(null, "the diameter of the circle:" + input );
		JOptionPane.showMessageDialog(null, "the area of the circle:" + area1 );
		JOptionPane.showMessageDialog(null, " the radius of the circle:" + radius );
		JOptionPane.showMessageDialog(null, "the circumference of the circle:" + circumfrence);
		JOptionPane.showMessageDialog(null, "area of the smallest square containing the circle:" + area2 );
		JOptionPane.showMessageDialog(null, "area of the largest square contained the circle:" + area3 );
		
		// defining variable filledObjects as type of Geometry
	     Hw2 filledObjects =   new Hw2();
	      
	      // set the coordinates and width and heights 
	      // of the shapes
	      filledObjects.square_x1 = 175;
	      filledObjects.square_y1 = 175;
	      filledObjects.square_width1 =diameter;
	      filledObjects.square_height1 =diameter;
	      filledObjects.square_color1 = Color.yellow;
	      
	      filledObjects.cir_width =diameter;
	      filledObjects.cir_height = diameter;
	      filledObjects.cir_x = 175;
	      filledObjects.cir_y = 175;
	      filledObjects.cir_color = Color.blue;
	      
	      filledObjects.square_x2 = 175+(radius*Math.sqrt(2))/2 ;
	      filledObjects.square_y2 = 175+(radius*Math.sqrt(2))/2 ;
	      filledObjects.square_width2 = radius*Math.sqrt(2);
	      filledObjects.square_height2 =radius *Math.sqrt(2);
	      filledObjects.square_color2 = Color.red;
	      


	     /* 
	      Geometry emptyObjects =   new Geometry();
	      emptyObjects.cir_width = 150;
	      emptyObjects.cir_height = 150;
	      emptyObjects.cir_x = 30;
	      emptyObjects.cir_y = 30;
	      emptyObjects.cir_color = null;
	      
	      emptyObjects.square_x1 = 30;
	      emptyObjects.square_y1 = 30;
	      emptyObjects.square_width1 = 100;
	      emptyObjects.square_height1 = 100;
	      emptyObjects.square_color1 = null;
	      */
	     
	
}
}

Submit "Help centering the square inside" to Facebook Submit "Help centering the square inside" to Digg Submit "Help centering the square inside" to del.icio.us Submit "Help centering the square inside" to StumbleUpon Submit "Help centering the square inside" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. kaydell2's Avatar
    • |
    • permalink
    Hi,

    Here is the essence of what you need to do to draw a shape in the center of the screen:

    Java Code:
    	public int centerX(double shapeWidth) {
    		int windowWidth = getWidth();
    		return (int)(windowWidth/2) - (int)(shapeWidth/2);
    	}
    
    	public int centerY(double shapeHeight) {
    		int windowHeight = getHeight();
    		return (int)(windowHeight/2) - (int)(shapeHeight/2);
    	}
    
    	//This function draws the shapes on the window
    	//and get called automatically ( no need to call it saparely)
    	//this function uses the variables that are defined above. The value of the
    	//variables are set in main file.
    	public void paint(Graphics g) {
    
    		// set the color (if there is one specified)
    		if(square_color1 == null) {
    			g.setColor(square_color1);
    		}
    		// draw the rectangle, centered in its window
    		g.drawRect(centerX(square_width1), centerY(square_height1), (int)square_width1, (int)square_height1);
    
    	}
  2. pj6444's Avatar
    • |
    • permalink
    So, basically when you paint the square, the x should be (windowWidth / 2) - (shapeWidth / 2). For the y it would be, (windowHeight / 2) - (shapeHeight / 2).