Results 1 to 2 of 2
  1. #1
    agater is offline Member
    Join Date
    Jul 2011
    Posts
    12
    Rep Power
    0

    Default qustion about JFrame

    hi everyone "
    I want to write a java code that should draw a rectangle or oval shape. if the user select a rectangle radio button and then press the draw button the code will draw rectangle and the same thing for oval shape.




    I wrote the below code but it's just radio button and draw button and it doesn't draw anything if you press it. my question is how can I add the two methods paint of the rectangle and oval shape inside my code. so if the user select rectangle the code will draw rectangle and so on.

    PHP Code:
       
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    
    
    public class Draw extends JFrame
    {
    public Draw()
    {
    setTitle("Drawing Test");
    setSize(300, 400);
    JPanel panel = new JPanel();
    getContentPane().add(panel);
    panel.setLayout(null);
    
    JRadioButton rectangle1 = new JRadioButton("Rectanle");
    JRadioButton oval1 = new JRadioButton("Oval");
    JButton button1 = new JButton("Draw");
    
    
    
    rectangle1.setBounds(50, 30, 80, 30);
    oval1.setBounds(140, 30, 80, 30);
    button1.setBounds(200, 70, 70, 30);
    
    
    
    button1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    
    }
    });
    
    panel.add(rectangle1);
    panel.add(oval1);
    panel.add(button1);
    
    }
    
    
    
    public static void main(String[] args) {
    Draw buttons = new Draw();
    buttons.setVisible(true);
    
    }
    }



    the two methods paint of the rectangle and oval shape

    PHP Code:
     public void paint(Graphics g)
    {
    super.paint(g);
    g.drawRect(50, 50, 100, 75);
    }
    
    public void paint(Graphics gr)
    {
    super.paint(gr);
    gr.drawOval(80,100,190,100);
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: qustion about JFrame

    You can only override a method once in a clas. The one paint method that you override will need to make decisions about what shape(s) it will draw based on information provided to it from other parts of the class.
    Your listener code will set some flag for the paint method to use and call the repaint method. The JVM will then call your paint method which can look at the flag and draw the shape.

    Instead of overriding JFrame's paint method, you should create a custom JPanel object, override its paintComponent method and add that to the JFrame.

Similar Threads

  1. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  2. Qustion regarding RAD setting
    By gaikwaddeepali111 in forum New To Java
    Replies: 0
    Last Post: 09-30-2011, 06:12 AM
  3. Qustion about collections
    By nn12 in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 03:36 PM
  4. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 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
  •