Results 1 to 7 of 7
Thread: Help centering the square inside

Help centering the square inside
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 Autogenerated 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; */ } }
 09142013, 02:00 AM #2
Re: Help centering the square inside
How have you analyzed the problem for the solution you have coded? It looks like you need to take a piece of graph paper and a pencil, draw the objects on the paper and then work out the x,y locations for where all the shapes should be drawn.
If you don't understand my response, don't ignore it, ask a question.

Re: Help centering the square inside
i have honestly been on this computer since 2 trying to figure it out. Yes i have done that. i have even seeked help from one of my geometry teachers in high school. and he said all the fourmals were correct and had no idea why it wasnt working
 09142013, 02:54 AM #4
Re: Help centering the square inside
When you drew the shapes, what were the x,y values for each shape?
What x,y values does the program use to draw the shapes?
Are those two sets of values the same?If you don't understand my response, don't ignore it, ask a question.

Re: Help centering the square inside
the x and the y are set the same. except for the one that cant be the same and thats the square inside the circle
Java Code: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;
 09142013, 03:13 AM #6
Re: Help centering the square inside
What are the x,y values for the inner box you get from drawing the shape with paper and pencil?
What are the x,y values does the program when it draws the inner box?
Why do you think the formula the program uses will compute the correct values?
In the drawing what is the relationship of the x,y values for the inner box to the x,y of the outer box and the sizes of the two boxes? Those are the values that should be used to compute the x,y for the inner box. Look at your drawing, to see the relationship.
What would be the formula to compute the center of the outer box?Last edited by Norm; 09142013 at 03:16 AM.
If you don't understand my response, don't ignore it, ask a question.
 09142013, 06:28 AM #7
Re: Help centering the square inside
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads

centering an applet
By smithdennis4 in forum New To JavaReplies: 1Last Post: 09222011, 12:44 AM 
Flipping square box inside picture help
By sman865 in forum New To JavaReplies: 3Last Post: 10162010, 08:10 PM 
component centering
By BigBear in forum AWT / SwingReplies: 3Last Post: 01252010, 12:48 AM 
centering a label inside a rectangle
By Brain_Child in forum New To JavaReplies: 3Last Post: 11192009, 06:22 PM 
Centering inside a JFrame
By kahaj in forum AWT / SwingReplies: 9Last Post: 09232009, 07:23 PM
Bookmarks