Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Need help with assignment - JButton

  1. #1
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Need help with assignment - JButton

    hi there, can somebody help me fix my code? so my assignment is to write a GUI application that lets users select one of the two shapes and draw the shape. The user can select either a Circle, or a Triangle. After one of the shapes is selected, the user can draw it as follow: when Circle is selected, let the user draw a circle with two mouse clicks. The first one on the center and the second on a point on the periphery of the circle. when Triangle is selected, let the user draw a triangle with three mouse clicks. so in other to choose, my instructor wants us to use JButton.

    My problem is in my JustViewer class = I can't make 2 buttons small and the other button is not displayed...

    so here is my code
    //circle
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class CircleComponent extends JComponent
    {
       public CircleComponent()
        {
            clicks = 0;
            MouseListener listener = new CircleListener();
            addMouseListener(listener);
            positionX = new int[2];
            positionY = new int[2];
        }
    
        public void paintComponent(Graphics g)
        {
        	Graphics2D g2 = (Graphics2D) g;
        	int postY = positionY[1] - positionY[0];
        	int postX = positionX[1] - positionX[0];
        	double diameter = 2 * (Math.sqrt((Math.pow(postX, 2)) - (Math.pow(postY, 2))));
    
        	Ellipse2D.Double circle = new Ellipse2D.Double(positionX[0], positionY[0], diameter, diameter);
        	g2.draw(circle);
        }
    
        class CircleListener implements MouseListener
        {
        	public void mouseClicked(MouseEvent event)
        	{
        		positionX[clicks] = event.getX();
        		positionY[clicks] = event.getY();
                clicks++;
                if(clicks % 2 ==0)
                {
        	    	repaint();
                    clicks = 0;
                }
        	}
    
        				public void mouseReleased(MouseEvent event) {}
             			public void mousePressed(MouseEvent event) {}
             			public void mouseEntered(MouseEvent event) {}
             			public void mouseExited(MouseEvent event) {}
        			}
    
        private int[] positionX;
        private int[] positionY;
        private int clicks;
    }


    //triangle
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
    
    public class TriangleComponent extends JComponent
    {
        public TriangleComponent()
        {
            clicks = 0;
            MouseListener listener = new MouseTriListener();
            addMouseListener(listener);
            positionX = new int[3];
            positionY = new int[3];
        }
    
        public void paintComponent(Graphics g)
        {
        	Graphics2D g2 = (Graphics2D) g;
            Point2D.Double r1 = new Point2D.Double(positionX[0], positionY[0]);
            Point2D.Double r2 = new Point2D.Double(positionX[1], positionY[1]);
            Point2D.Double r3 = new Point2D.Double(positionX[2], positionY[2]);
    
            Line2D.Double post1 = new Line2D.Double(r1, r2);
            Line2D.Double post2 = new Line2D.Double(r2, r3);
            Line2D.Double post3 = new Line2D.Double(r3, r1);
    
            g2.draw(post1);
            g2.draw(post2);
            g2.draw(post3);
        }
    
            class MouseTriListener implements MouseListener
            {
                public void mousePressed (MouseEvent event)
                {
                    positionX[clicks] = event.getX();
                    positionY[clicks] = event.getY();
                    clicks++;
                    if (clicks % 3 ==0)
                    {
                        repaint();
                        clicks = 0;
                    }
                }
    
                public void mouseReleased(MouseEvent event) {}
                public void mouseClicked(MouseEvent event) {}
                public void mouseEntered(MouseEvent event) {}
                public void mouseExited(MouseEvent event) {}
            }
    
        private int[] positionX;
        private int[] positionY;
        private int clicks;
    }

    //the viewer
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JustViewer
    {
       public static void main(String[] args)
       {
           final TriangleComponent component = new TriangleComponent();
           final CircleComponent component1 = new CircleComponent();
    
           final JFrame frame = new JFrame();
    
           JButton button = new JButton("Triangle");
           JButton button1 = new JButton("cirlce");
           frame.add(button);
       	   frame.add(button1);
    
       class TriangleListener implements ActionListener
       {
       		public void actionPerformed(ActionEvent e)
       		{
       			frame.add(component);
       		}
       }
    
       ActionListener listener = new TriangleListener();
       button.addActionListener(listener);
    
       class CircleListener implements ActionListener
       {
       	public void actionPerformed(ActionEvent e)
       	{
       		frame.add(component1);
       	}
       }
    
       ActionListener listener1 = new CircleListener();
       button1.addActionListener(listener1);
    
       frame.setSize(300, 400);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
    	}
    }
    Thank you so much, I really appreciate it for reading
    Last edited by r31n4ard; 07-13-2011 at 04:20 AM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,890
    Rep Power
    25

    Default

    My problem is in my JustViewer class = I can't make 2 buttons small and the other button is not displayed...
    I only see 2 buttons in the JustViewer class. Where is the other button?

  3. #3
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    if you try to run the JustViewer class, you will see only circle button and it takes all the frame.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,890
    Rep Power
    25

    Default

    Create a JPanel, add both buttons to the JPanel and add that to the JFrame.

  5. #5
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    alrite I will try, thanks a lot Moderator :)

  6. #6
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    one more question, can I add JComponent stuffs to a panel? *for example add TriangleComponent class above to a panel, then add the panel to the frame? is it possible?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  8. #8
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    @Fubarable: I tried it and it doesnt work,however adding the buttons to the panel then add the panel to the frame works but I dont get the result I want, I have to expand the frame to the right to make the clicks and paint the triangle or circle

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  10. #10
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    so based on TriangleComponent and CircleComponent class, I made JustViewer class to make the circle or triangle with 2 or 3 clicks.
    as recommended by moderator Norm, I put the buttons into panel and add the panel to frame. so here is the updated code of JustViewer ( Triangle and Circle component are okay, at above)
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class JustViewer
    {
       public static void main(String[] args)
       {
           final TriangleComponent component = new TriangleComponent();
           final CircleComponent component1 = new CircleComponent();
    
           final JFrame frame = new JFrame();
    
           JButton button = new JButton("Triangle");
           JButton button1 = new JButton("cirlce");
    
    
       class TriangleListener implements ActionListener
       {
       		public void actionPerformed(ActionEvent e)
       		{
       			frame.add(component);
       		}
       }
    
       ActionListener listener = new TriangleListener();
       button.addActionListener(listener);
    
       class CircleListener implements ActionListener
       {
       	public void actionPerformed(ActionEvent e)
       	{
       		frame.add(component1);
       	}
       }
    
       ActionListener listener1 = new CircleListener();
       button1.addActionListener(listener1);
    
       JPanel panel = new JPanel();
       panel.add(button);
       panel.add(button1);
       frame.add(panel);
    
       frame.setSize(300, 400);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
    	}
    }


    So my goal is, when I click one of the 2 buttons, I can make either the circle or triangle once. (e.g I click triangle button, I just click 3 times in the space given in the frame and then draw the triangle after that I can't make any triangle anymore. So next press the circle button to erase the triangle and just click twice in the same space to draw the circle).

    But my problem now is, for example, when I pressed "Triangle" button, I can't make the triangle but instead I have to expand the frame to the right and make the clicks there. another problem is, then I pressed circle button (after the triangle button), I have to expand more the frame to the right to be able make the clicks and draw the circle. but at the same time, I also can draw the triangle.

    Thank you for reading my problems. I really appreciate it.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Suggestions:
    • Learn about layouts. For instance the JFrame contentPane (which is what you're adding components to when adding them to a JFrame) uses a BorderLayout, and you'll want to read the tutorial about BorderLayouts so you add components to the right spot. For instance the button panel should be added to the north position.
    • Use debugging code to help you check the state of your program while it runs. For instance, it would be a good idea to check the size of your components when they are added to the GUI, and this can be done via System.out.println'ing the result of calling getSize() on your component.
    • Use variable names that make sense and which make your code self-commenting. Which makes more sense, calling a variable button1 or calling it circleButton? Which variable name would make debuggin easier?
    • Learn and use a CardLayout to do your swapping for you. If you use this, you won't have to call revalidate and repaint on the container that holds your drawing JComponents (something that you're not doing now anyway).


    Luck!
    DarrylBurke likes this.

  12. #12
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    alrite, thanks alot moderator! I will try your suggestions!

  13. #13
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    @moderators: I tried to do this as suggested by Fubarable

    Java Code:
     JPanel panelButton = new JPanel();
       panelButton.setLayout(new BorderLayout());
    
       panelButton.add(button);
       panelButton.add(button1);
       frame.add(panelButton, BorderLayout.NORTH);
    But the output is only the last component is displayed. Can you explain to me why it happens and give me hints to solve this?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,890
    Rep Power
    25

    Default

    The BorderLayout manager needs instructions on where you want it to place your components. These are called constraints in the syntax for the add method.
    If you don't give any instructions/constraints it uses the default which probably says to put them all in the same place. One on top of the other. Read the API doc for the BorderLayout class for examples.

  15. #15
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    I have looked at the API doc but still don't really understand (sorry I start to learn java from 2 months ago). Can you give me an example?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,890
    Rep Power
    25

    Default

    Did you try any of the examples in the API doc?
    For example:
    Java Code:
    add(new Button("North"), BorderLayout.NORTH);
    This will put a button in the North section of the layout.

  17. #17
    r31n4ard is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default

    oh I never tried this one, alrite I will try it now, thx Moderator

Similar Threads

  1. Need Help With Assignment
    By zeo in forum New To Java
    Replies: 10
    Last Post: 02-25-2011, 01:09 AM
  2. Can anyone help me in my Assignment
    By nono in forum New To Java
    Replies: 18
    Last Post: 11-15-2010, 06:52 AM
  3. Assignment
    By shinkymar in forum New To Java
    Replies: 2
    Last Post: 03-06-2010, 02:17 PM
  4. need help! assignment due by tmr!
    By keycoffee in forum New To Java
    Replies: 3
    Last Post: 02-10-2010, 06:45 PM
  5. Can somebody help me in my assignment
    By coolstruxx in forum NetBeans
    Replies: 0
    Last Post: 03-24-2009, 02:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •