Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
Like Tree1Likes

Thread: Help on Homework

  1. #21
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    i think there is a problem with finding my width and length with the largest square inside the circle.
    Help on Homework-circle.png

  2. #22
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Help on Homework

    I believe I figured it out. When the class was instantiated, it was eventually queued to get painted on the EDT. However, that situation was slow enough that the main code was able to set the values before the painting occurred. If you sleep for 100 ms after the creation of the JFrame, the images won't be painted.

    So the last statement in main should be.

    Java Code:
              filledObjects.repaint();
    Here is a little program that demonstrates the problem.

    Java Code:
    public class RaceCondition extends JFrame {
       int dim = 0;
       public RaceCondition() {
          setPreferredSize(new Dimension(500,500));
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          pack();
          setVisible(true);
       }
       public static void main(String[] args) {
          // TODO Auto-generated method stub
          RaceCondition rc = new RaceCondition();
    //      sleep(100);
          rc.dim = 200;
       }
       
       public void paint(Graphics g) {
          super.paint(g);
          g.setColor(Color.blue);
          g.fillRect((500-dim)/2, (500-dim)/2, dim, dim);
       }
       
       public static void sleep(int mill) {
          try {
             Thread.sleep(mill);
          } catch(InterruptedException ie) {}
       }
    
    }
    Regards,
    Jim
    Last edited by jim829; 09-13-2013 at 07:08 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #23
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    I emailed the instructor my code and he said the only thing i have to do is center my inner square . Help on Homework-circle.pngClick image for larger version. 

Name:	circle.png 
Views:	16 
Size:	97.6 KB 
ID:	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;
    	      */
    	     
    	
    }
    }

  4. #24
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Help on Homework

    Your instructor is mistaken. As Tolls pointed out you are creating the Frame before setting the values. You need to do a repaint as the last item in your main method. It is simply coincidental that your program "works" and displays the image. So to guarantee that it will always work, you need to add the following at the end of main.

    Java Code:
    filledObjects.repaint();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #25
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    alright i did added at the end. but my square is still not centered in my circle

  6. #26
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Help on Homework

    Assume that your frame is 500 x 500. And your square is 200 x 200. Then the x coordinate of the top left of your square at (500-200)/2. Similar for the y coordinate.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #27
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    did it just like you told me but it still isnt centered.
    Java Code:
      // 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 = (750-175)/2 ;
    	      filledObjects.square_y2 = (750-175)/2 ;
    	      filledObjects.square_width2 = 175;
    	      filledObjects.square_height2 =175;
    	      filledObjects.square_color2 = Color.red;
    	      
    	      filledObjects.repaint();

  8. #28
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Help on Homework

    Where is the 750 coming from?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #29
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Help on Homework

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Homework Help
    By EZ-Ed in forum New To Java
    Replies: 3
    Last Post: 04-10-2012, 08:11 PM
  2. Homework Help please!
    By ghjk in forum New To Java
    Replies: 5
    Last Post: 03-19-2012, 03:44 AM
  3. I need some homework help
    By brlau in forum New To Java
    Replies: 9
    Last Post: 12-20-2011, 08:25 PM
  4. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 04:37 PM
  5. help with homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 7
    Last Post: 04-07-2009, 02:34 AM

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
  •