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 23 of 23

Thread: GUI Exercise

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default GUI Exercise

    hello guys,

    I hop you are all well,

    I have an exercise in how to build GUI primitive, and I hop any can help with it.

    You are required to build a GUI interface MyFrameMain to be able to input the required
    values to draw an oval or a rectangle, select the colour and the number of repeated
    shapes. The GUI interface should appear as in Figure 1 below, according to the
    following specifications:
    • Set its size to 700x500.
    • Set its title "Drawing shapes"
    • The frame has Border Layout. It consists of three main JPanels, North, Centre,
    South

    • The North JPanel, user can do the following:
     The user can draw the outline of an oval using the current colour. The
    result is a circle or an ellipse that fits within the rectangle whose top lefthand
    corner is specified by the first two arguments, whose width is given by
    the third argument and whose height is specified by the final argument. The
    values will be filled in using four JTextFields. The oval will be displayed
    once the user clicks on Add Oval JButton.
     The user can draw the outline of the specified rectangle using the current
    colour. The top left-hand corner of the rectangle is defined by a pair of x
    and y-coordinates. The final two arguments give the width and height. The
    values will be filled in using four JTextFields. The Rectangle will be
    displayed once the user clicks on Add Rectangle JButton.
     The user can select one of the three colours using JRadioButton group.
    Red is the default, with green and blue available as well.
     The user can select the number of repeated shapes up to 10 from the
    JList. The code repeats drawing the shape many times by adding a fixed
    number to the values of x and y to shift the position of the new shape.
     The user can draw the two shapes by filling the values of the two shapes
    and then clicking on Add Both JButton


    • The Centre JPanel is the drawing area where the required shapes will be
    displayed.


    • The South JPanel contains two JButtons
     Clear button which clears all values in all JTextFields once the user clicks
    on it.
     Exit Button which terminates the program once the user clicks on it.
    • An appropriate event listener should be used and the buttons should implement
    the listener objects for all buttons.
    • paint() and repaint() methods should be implemented to be invoked to draw the
    shapes.
    • The class should have a main method to instantiate the Frame.



    thank you all,


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    What have you tried?

    Do you have any specific questions?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    how to put the element, such as JButton, on a specific position?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    To put components at specific positions, turn off the layout manager for the container the components are being added to by setting it to null and use the setBounds method.
    That is often a bad idea and can make the GUI layout hard to maintain.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    in the question, he asked me to create three JPanel north, centre and south.
    how can I create sub panel inside the north JPanel.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    Define a panel, add the desired components to that panel and add that panel to the north.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    thank you Norm,

    I want to build a three sub panel inside one JPanel and the JPanel be in the north.
    figure below will show you what I mean
    Screen_Shot_2014_05_02_at_5_13_26_PM.png

    thank you again,

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    Can you post the code you are having problems with?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

     
    package myframemain;
    import java.awt.*;
    import javax.swing.*;
     
    public class MyFrame extends JFrame {
     
        JLabel lblOval;
        JLabel lblX;
        JLabel lblY;
        JLabel lblWidth;
        JLabel lblHeight;
        JLabel lblRectangle;
        JLabel lblX1;
        JLabel lblY1;
        JLabel lblX2;
        JLabel lblY2;
        JLabel lblColor;
        JLabel lblNumberOfShapes;
     
        JTextField txtX;
        JTextField txtY;
        JTextField txtWidth;
        JTextField txtHeight;
        JTextField txtX1;
        JTextField txtY1;
        JTextField txtX2;
        JTextField txtY2;
     
        JButton btnAddOval;
        JButton btnAddRectangle;
        JButton btnAddBoth;
        JButton btnClear;
        JButton btnExit;
     
        JRadioButton radRed;
        JRadioButton radGreen;
        JRadioButton radBlue;
     
        JComboBox cboNumber;
     
     
        JPanel northPanel;
        JPanel centrePanel;
        JPanel southPanel;
     
        public MyFrame()
        {
             setSize(700, 500);
             setTitle("Welcome");
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
             GridBagConstraints gbc = new GridBagConstraints();
             gbc.insets = new Insets(5,5,5,5);
     
              lblOval = new JLabel("Oval");
              lblX = new JLabel("X");
              lblY = new JLabel("Y");
              lblWidth = new JLabel("Width");
              lblHeight = new JLabel("Height");
              lblRectangle = new JLabel("Rectangle");
              lblX1 = new JLabel("X1");
              lblY1 = new JLabel("Y1");
              lblX2 = new JLabel("X2");
              lblY2 = new JLabel("Y2");
              lblColor = new JLabel("Color");
              lblNumberOfShapes = new JLabel("Number of shapes");
     
              txtX = new JTextField("",5);
              txtY = new JTextField("",5);
              txtWidth = new JTextField("",5);
              txtHeight = new JTextField("",5);
              txtX1 = new JTextField("",5);
              txtY1 = new JTextField("",5);
              txtX2 = new JTextField("",5);
              txtY2 = new JTextField("",5);
     
              btnAddOval = new JButton("Add Oval");
              btnAddRectangle = new JButton("Add Rectangle");
              btnAddBoth = new JButton("Add Both");
              btnClear = new JButton("Clear");
              btnExit = new JButton("Exit");
     
              radRed = new JRadioButton("RED");
              radGreen = new JRadioButton("GREEN");
              radBlue = new JRadioButton("BLUE");
     
              cboNumber = new JComboBox();
     
     
              northPanel = new JPanel(new GridBagLayout());
              centrePanel = new JPanel();
              southPanel = new JPanel();
     
     
     
             cboNumber.addItem("1");
             cboNumber.addItem("2");
             cboNumber.addItem("3");
             cboNumber.addItem("4");
             cboNumber.addItem("5");
             cboNumber.addItem("6");
             cboNumber.addItem("7");
             cboNumber.addItem("8");
             cboNumber.addItem("9");
             cboNumber.addItem("10");
             cboNumber.setSelectedItem("1");
     
             northPanel.add(lblOval, gbc);
             gbc.gridx = 0;
             gbc.gridy = 0;
             northPanel.add(lblX, gbc);
             gbc.gridx = 1;
             gbc.gridy = 0;
             northPanel.add(txtX, gbc);
             gbc.gridx = 2;
             gbc.gridy = 0;
             northPanel.add(lblY, gbc);
             gbc.gridx = 3;
             gbc.gridy = 0;
             northPanel.add(txtY, gbc);
             gbc.gridx = 4;
             gbc.gridy = 0;
             northPanel.add(lblWidth, gbc);
             gbc.gridx = 5;
             gbc.gridy = 0;
             northPanel.add(txtWidth, gbc);
             gbc.gridx = 6;
             gbc.gridy = 0;
             northPanel.add(lblHeight, gbc);
             gbc.gridx = 7;
             gbc.gridy = 0;
             northPanel.add(txtHeight, gbc);
             gbc.gridx = 8;
             gbc.gridy = 0;
             northPanel.add(btnAddOval, gbc);
             gbc.gridx = 9;
             gbc.gridy = 0;
             northPanel.add(lblRectangle, gbc);
             gbc.gridx = 0;
             gbc.gridy = 1;
             northPanel.add(lblX1, gbc);
             gbc.gridx = 1;
             gbc.gridy = 1;
             northPanel.add(txtX1, gbc);
             gbc.gridx = 2;
             gbc.gridy = 1;
             northPanel.add(lblY1, gbc);
             gbc.gridx = 3;
             gbc.gridy = 1;
             northPanel.add(txtY1, gbc);
             gbc.gridx = 4;
             gbc.gridy = 1;
             northPanel.add(lblX2, gbc);
             gbc.gridx = 5;
             gbc.gridy = 1;
             northPanel.add(txtX2, gbc);
             gbc.gridx = 6;
             gbc.gridy = 1;
             northPanel.add(lblY2, gbc);
             gbc.gridx = 7;
             gbc.gridy = 1;
             northPanel.add(txtY2, gbc);
             gbc.gridx = 8;
             gbc.gridy = 1;
             northPanel.add(btnAddRectangle, gbc);
             gbc.gridx = 9;
             gbc.gridy = 1;
             northPanel.add(lblColor, gbc);
             gbc.gridx = 0;
             gbc.gridy = 2;
             northPanel.add(radRed, gbc);
             gbc.gridx = 1;
             gbc.gridy = 2;
             northPanel.add(radGreen, gbc);
             gbc.gridx = 2;
             gbc.gridy = 2;
             northPanel.add(radBlue, gbc);
             gbc.gridx = 3;
             gbc.gridy = 2;
             northPanel.add(lblNumberOfShapes, gbc);
             gbc.gridx = 4;
             gbc.gridy = 2;
             northPanel.add(cboNumber, gbc);
             gbc.gridx = 5;
             gbc.gridy = 2;
             northPanel.add(btnAddBoth, gbc);
             gbc.gridx = 6;
             gbc.gridy = 2;
     
             getContentPane().add(northPanel, BorderLayout.NORTH);
     
             centrePanel.setBackground(Color.BLUE);
     
             add(centrePanel, BorderLayout.CENTER);
     
     
     
             southPanel.add(btnClear);
             southPanel.add(btnExit);
     
             add(southPanel, BorderLayout.SOUTH);
     
     
        }
     
    }


    and now I have this output.
    I don't know what is the wrong!
    Screen_Shot_2014_05_02_at_5_42_23_PM.jpg

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    I don't know what is the wrong!
    can you explain what is wrong?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    as you can see, the elements not arranged well.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    What needs to be changed?

    How do you test the code? There is no main() method so it can be executed.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    this is a Class.
    I invoked it at the main() method. this is no problem.

    I want to arrange the elements and if you see the attached screen shot, you will see:
    -label "oval" is over "X" label
    -I want to put the label Rectangle in the second line.
    -there is no space in the text fields.
    - etc.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    I can not test it without a main() method.
    - etc.
    etc is a hard question to respond to.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    here is the main() method

    package myframemain;
     
    public class MyFrameMain {
     
        public static void main(String[] args) {
     
            MyFrame fm = new MyFrame();
            fm.setVisible(true);
     
     
        }
     
    }


    you just answer me about these:
    -label "oval" is over "X" label
    -I want to put the label Rectangle in the second line.
    -there is no space in the text fields.

    and I will handle the rest.
    I just want to understand the concept.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    I'll leave this for a layout expert. I don't do much with layouts.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    thank you norm
    I appreciate your cooperation.

  18. #18
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    I have solved the problem and another problem appeared.

    How can I handle several ActionListener?
    there is a several buttons and each button has an action.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    Here are 2 solutions:
    1)use a separate actionlistener for each button
    2)use one actionlistener for all buttons and use the getSource method to determine which button sent the action event
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    thank you norm, I appreciate your help.

    I chose separate actionlistener for each button.

    The "Add Rectangle" button should draw a rectangle based on:

    - the x, y, width and height.
    - the colors from the radio buttons.
    - the numbers of rectangle from the combo box

    I did the first two, and I am stuck in the third one.

    there is my code

     public class Action implements ActionListener
              {
            private Graphics g;
     
     
                  public void paint(Graphics g)
                  {
     
                          g = centrePanel.getGraphics();
                          int int1 = Integer.parseInt(txtX1.getText());
                          int int2 = Integer.parseInt(txtY1.getText());
                          int int3 = Integer.parseInt(txtX2.getText());
                          int int4 = Integer.parseInt(txtY2.getText());
     
     
     
                          if (radRed.isSelected()) {
                                 g.setColor(Color.red);
                              }
                          else
     
                          if (radGreen.isSelected()) {
                                 g.setColor(Color.GREEN);
                          }
                          else
     
                          if (radBlue.isSelected()) {
                                   g.setColor(Color.BLUE);
                              }
     
     
     
     
     
                           g.drawRect(int1, int2,  int3, int4);
                  }
     
                   public void actionPerformed(ActionEvent a)
                   {
     
                      paint(g);
     
                    }
     
     
     
     
                 }


    how can I select the number from the combo box "such as 3" and then draw a three shifted rectangle ?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    the paint() method is normally called by the JVM following a request by calling the repaint() method.

    The paint method needs to override the paint method in a component that has been added to the GUI.
    With swing classes the paintComponent() method is the one to override, not paint.

    The values of int1,2,3,4 should be calculated in the listener. The variables need to be defined at as class instance variables so the paint method can see them.

    Get this part of the drawing code to work before worrying about getting values from a combo box.

    See the tutorial: http://docs.oracle.com/javase/tutori...ing/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    May 2014
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: GUI Exercise

    hahaha man I am beginner in java and your words big for me. However, my code right now is working and give me what I want except the value from the combo box.

    I want to ask you, what is the different between paint() method and repaint() method?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GUI Exercise

    different between paint() method and repaint()
    paint() is called by the JVM when the GUI needs to be drawn
    repaint() asks the jvm to call the paint() method
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 10:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 08:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 11:31 PM
  4. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  5. Exercise
    By phite2009 in forum Member Introductions
    Replies: 3
    Last Post: September 30th, 2011, 08:51 AM