Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: using ActionListeners to draw circles

  1. #1
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default using ActionListeners to draw circles

    Hey there,

    We're learning GUI, and I'm working on a program that takes the input of x, y, and radius, and draws a circle from those points.

    I'm stuck, because in my listener, I'm not sure how to specify that I want the program to take the input from x, y, and radius JTextFields, and use the DrawOval() method to make a circle. So far I have:

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class Circle extends JPanel {
     
     
     
    JButton draw = new JButton("draw circle");
    JTextField xcoord = new JTextField("x-coord");
    JTextField ycoord = new JTextField("y-coord");
    JTextField rad = new JTextField("radius");
     
    public Circle() {
    	JPanel northPanel = new JPanel();
    	northPanel.add(draw);
    	northPanel.add(xcoord);
    	northPanel.add(ycoord);
    	northPanel.add(rad);
    	add(northPanel);
    }
     
    class drawlistener implements ActionListener {
    	public void actionPerformed(ActionEvent event) {
     
    		String inputString = xcoord.getText();
    	    int x = Integer.parseInt(inputString);
     
    	    String inputString2 = ycoord.getText();
    	    int y = Integer.parseInt(inputString2);
     
    	    String inputString3 = rad.getText();
    	    int r = Integer.parseInt(inputString3);
     
     
     
    	}
    }
     
     
    public static void main(String[] args) {
    	JFrame frame = new JFrame();
    	Circle c1 = new Circle();
    	frame.add(c1);
    	frame.setSize(500,500);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
     
    }
     
     
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: using ActionListeners to draw circles

    What are the values of x, y, and r after running the code you have so far? Did it work as expected? If so, what is your question? If not, what values did you get and what did you expect?

  3. #3
    Member Scorks's Avatar
    Join Date
    Jan 2013
    Posts
    56
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: using ActionListeners to draw circles

    Well, x,y, and r are just the values that the user would input into the JTextFields, then upon clicking the "draw" button, I want a circle to be drawn. I just don't know how to actually invoke the drawOval() method from inside my ActionListener.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: using ActionListeners to draw circles

    How do you invoke the add method inside the constructor?
    How do you invoke the parseInt method inside the action listener?
    You call drawOval the same way you call any other method. Just type the name, open paren, any parameters if any are needed, close paren, and semicolon.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: using ActionListeners to draw circles

    Swing's drawing or painting mechanism can be used to respond to actions on the user interface. In this case, the ActionListener that handles the button press could read and set the values x, y, and r and then call repaint() to draw the oval.

    I recommend the following changes to your fundamental design:

    The main class (CircleFrame) composes a JFrame in which 2 panels are placed:
    1. North (or South?): A JPanel to contain the text fields and button is built in and returned from a CircleFrame method. An action listener is added to the button much as you've already done. (Class names begin with capital letters.) The action listener's actionPerformed() method reads and sets the values x, y, and r and then calls repaint() to draw the oval.
    2. Center: A JPanel to draw the ovals (OvalPanel) is built from an enclosed class that extends JPanel.

    The paintComponent() method of OvalPanel is overridden to draw ovals specified by x, y, and radius as set by the button's action listener which calls repaint() after the values are set.

    The main() method of CircleFrame simply creates an instance of CircleFrame on the EDT, nothing else.

    Good luck!

Similar Threads

  1. Replies: 1
    Last Post: October 20th, 2013, 11:54 PM
  2. Connecting Two Different ActionListeners
    By steme in forum Java Theory & Questions
    Replies: 1
    Last Post: April 26th, 2013, 08:35 AM
  3. Change the circles to start
    By m7abraham in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 3rd, 2012, 02:52 AM
  4. Help with creating random circles
    By cool48 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2011, 09:18 AM
  5. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM

Tags for this Thread