Results 1 to 2 of 2
Thread: qustion about JFrame
- 12-09-2011, 01:15 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 12
- Rep Power
- 0
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); }
- 12-09-2011, 01:30 PM #2
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
-
Calling another JFrame from a JFrame through a JButton...(Help Reqired)
By nish.singha20 in forum AWT / SwingReplies: 3Last Post: 11-27-2011, 05:18 AM -
Qustion regarding RAD setting
By gaikwaddeepali111 in forum New To JavaReplies: 0Last Post: 09-30-2011, 06:12 AM -
Qustion about collections
By nn12 in forum New To JavaReplies: 2Last Post: 01-10-2011, 03:36 PM -
to pass a parameter from a jframe children to its jframe mother
By anix in forum NetBeansReplies: 5Last Post: 06-14-2010, 06:10 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks