Need help with small assignment pleaz...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Need help with small assignment pleaz...

  1. #1
    Join Date
    Jan 2009
    Posts
    6

    Unhappy Need help with small assignment pleaz...

    Hi all,
    I please need help with this question please:

    "Write an application that draws an ellipse. It must accept two
    parameters that are the sizes of the two axes in pixels.
    Center the ellipse in the application"

    Here is the code that I've tried so far:

    Code:
    public class EllipseApp extends JComponent implements ActionListener{
    	
    	JFrame frame = new JFrame();
    	JTextField field1 = new JTextField("Width", 5);
    	JTextField field2 = new JTextField("Height", 5);
    	JButton button = new JButton("OK");
    	
    	public EllipseApp() {
    		super();
    		frame.setTitle("Ellipse");
    		frame.setSize(500, 500);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    		button.addActionListener(this);
    		JPanel pane = new JPanel();
    		pane.add(field1);
    		pane.add(field2);
    		pane.add(button);
    		frame.add(pane);
    		setVisible(true);
    	}
    	
    	public void paintComponent(Graphics2D g, float a, float b) {
    		super.paintComponent(g);
    		Graphics2D g2 = (Graphics2D)g;
    		Ellipse2D.Float ellipse = new Ellipse2D.Float(0, 0, a, b);
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g2.draw(ellipse);
    		g2.fill(ellipse);
    		g2.setColor(Color.blue);
    	}
    	
    	public static void main(String[] args) {
    		EllipseApp e = new EllipseApp();
    		JPanel panel = new JPanel();
    		panel.add(e);
    		panel.setVisible(true);
    		
    	}
    	
    	public void actionPerformed(ActionEvent ae) {
    		Object source = ae.getSource();
    		if (source == button) {
    			
    		}
    		
    	}
    	
    
    }
    This program doesn't want to work correctly and I do not know what to do further,
    can anyone please help me with this,
    I need your help.
    Thanks in advance

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    What is it doing that drives it into "doesn't want to work" terrority?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Jan 2009
    Posts
    6

    Unhappy Need further help with my assignment pleaze

    I'm still struggling with this program I need to write for an assignment.

    I've tweaked the code quite a lot from the last time, but there is still two things that I don't know how to do...

    1. How do I set the input that was inserted by the user in the 'text fields' (Width & Height) to the 'width' and 'height' in 'g2.fillOval(x, y, width, height)' after the user has pressed the 'OK' button?? So that the ellipse can be drawn according to that input the user has inserted.
    I think it must be done in the 'actionPerformed' method but I do not know how.

    2. Setting the x and y positions in 'g2.fillOval(x, y, width, height)' so that the ellipse that was drawn, be centered in the middle of the window\frame??

    Here is the code for this prog:

    Code:
    public class EllipseApp extends JFrame implements ActionListener {
    	 
    	 private int width;
    	 private int height;
    	 private boolean draw;
    	 
    	 JTextField text1 = new JTextField("Width", 5);
    	 JTextField text2 = new JTextField("Height", 5);
    	 JButton button = new JButton("OK");
    	 
    	 
    	 public EllipseApp() {
    		 
    		 setSize(500, 500);
    		 setTitle("An Ellipse");
    		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		 text1.addActionListener(this);
    		 text2.addActionListener(this);
    		 button.addActionListener(this);
    		 JPanel panel = new JPanel();
    		 panel.add(text1);
    		 panel.add(text2);
    		 panel.add(button);
    		 add(panel);
    		 setVisible(true);
    	 }
    	 
    	 
    	public void actionPerformed(ActionEvent e) {
    		Object source = e.getSource();
    		if (source == button) { 
    			draw = true;    
    			repaint();
    		}
    	}
    	
    	
    	public void paint(Graphics g)    {        
    		 Graphics2D g2 = (Graphics2D)g;              
    		 g2.setColor(Color.BLUE);       
    		 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		 if(draw == true){          
    			 g2.fillOval(x, y, width, height);
    		 }    
    	}
    	 
    	
    	public static void main(String[] args) {
    		EllipseApp ell = new EllipseApp();
    		
    	}
    	
    }
    I please need your help.
    Thanks.

  4. #4
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    You are almost there. I could give you the answer directly, but it might be better if you try once more... These are the methods you can use:

    * To get the text in text field: use textField.getText()
    * To convert string to integer: use Integer.parseInt(string) [ returns int ]
    * To display in the centre position, x = (width of frame - width of ellipse) / 2; similar for y
    * To get width of Frame: frame.getWidth(); (You can use the 'this' variable in this case)

    Do post back the final answer you get.

  5. #5
    Join Date
    Jan 2009
    Posts
    6

    Red face Help with ellipse application

    I managed to sort out the two things which I was stuck with thanks to the help of Razee Marikar, but I have one more quetion to ask on this application:

    When I input values into the 'Width' and 'Height' textfields of my program e.g - Width = 300 & Height = 300, and then press the 'OK' button the program draws the ellipse\oval correctly and the program runs fine.
    But now here's the problem, I can only input bigger new values in the textfields if I want the ellipse to be redrawn to different sizes but can't input smaller values(well I can but it does not change anything!)


    So my question is this:
    Is it because of some part of my code that restricts it to only bigger values that can be inserted and then be redrawn? (Because I can't find the part of the 'problem' code)

    The code for the program is as follows:

    Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class EllipseApp extends JFrame implements ActionListener {
    	 
    	 private int width;
    	 private int height;
    	 private boolean draw;
    	 
    	 JTextField text1 = new JTextField("Width", 5);
    	 JTextField text2 = new JTextField("Height", 5);
    	 JLabel label = new JLabel("(Press enter after you have inserted each value)");
    	 JButton button = new JButton("OK");
    	 
    	 
    	 public EllipseApp() {
    		 
    		 setSize(600, 600);
    		 setTitle("An Ellipse");
    		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		 text1.addActionListener(this);
    		 text2.addActionListener(this);
    		 button.addActionListener(this);
    		 JPanel panel = new JPanel();
    		 panel.add(text1);
    		 panel.add(text2);
    		 panel.add(label);
    		 panel.add(button);
    		 add(panel);
    		 setVisible(true);
    	 }
    
    	 
    	public void actionPerformed(ActionEvent e) {
    		Object source = e.getSource();
    		if (source == text1) {
    			String a = text1.getText();
    			width = Integer.parseInt(a); 
    		}
    		if (source == text2) {
    			String b = text2.getText();
    			height = Integer.parseInt(b);
    		}
    		if (source == button) { 
    			draw = true;    
    			repaint();
    		}
    	}
    	
    	
    	public void paint(Graphics g)    {        
    		 Graphics2D g2 = (Graphics2D)g;              
    		 g2.setColor(Color.BLUE);       
    		 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		 if(draw == true){ 
    			 int x = (this.getWidth() - width) / 2;
    			 int y = (this.getHeight() - height) / 2;
    			 g2.fillOval(x, y, width, height);
    		 }    
    	}
    	 
    	
    	public static void main(String[] args) {
    		EllipseApp ell = new EllipseApp();
    		
    	}
    	
    }
    Please give me some ideas or examples on how to fix this.
    Thanks

  6. #6
    Join Date
    Jan 2009
    Location
    Whitehall, OH
    Posts
    1
    I am not a programmer (I dabble; took a class or two), so I can't advise you as to code problems, but it seems that the problem you're having is that you are drawing smaller ellipses within the already drawn larger one. A screen clear, or change in color assignment for the next ellipse might solve your problem. In an extreme solution, could you close the window and open a new one for the next ellipse? Having a 'Draw new ellipse' button would trigger the action, with proper code clearing all ellipse variables.

Similar Threads

  1. Assignment Operator, Returning const reference
    By codemonkey32 in forum C++
    Replies: 6
    Last Post: 02-06-2006, 04:39 PM
  2. Replies: 3
    Last Post: 10-21-2005, 10:06 AM
  3. Earn $100 for doing a small sql assignment
    By ryan in forum Database
    Replies: 2
    Last Post: 10-30-2001, 03:45 PM
  4. Earn $100 for doing a small sql assignment
    By ryan in forum Database
    Replies: 0
    Last Post: 10-27-2001, 11:32 PM
  5. Microsoft Breakup Lunacy
    By rlynchttl in forum Talk to the Editors
    Replies: 19
    Last Post: 06-21-2000, 10:38 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center