Hello guys!
How's you doing?!!
I want some help in Java program that will probply make me crazy soon1:mad:
the question ask us to build a GUI that will make the user choose a shape : either Rectangle or Oval to draw
&
ask him to input the value of the x ,y , hieght & width
It should be like this image:
U.png 45.73K
29 downloads
&
This is my code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newappfortma;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author Sudent
*/
public class ShapesFrame extends JFrame{
JButton buttonDraw= new JButton("Draw");
JRadioButton rbO= new JRadioButton("Oval",true);
JRadioButton rbR= new JRadioButton("Rectangle",false);
private Graphics gr;
public JTextField textf[] =new JTextField[4];
private int x,y,hight,width;
public ShapesFrame(String title)
{
setSize(300,400);
setTitle(title);
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(rbR);
bgroup.add(rbO);
JPanel pan1=new JPanel();
pan1.setLayout(new FlowLayout());
pan1.add(rbR);
pan1.add(rbO);
JPanel pan2= new JPanel();
for(int i=0;i<4;i++)
{
textf[i]=new JTextField(3);
pan2.add(textf[i]);
}
Container con = getContentPane();
con.setLayout(new FlowLayout());
con.add(pan1);
con.add(pan2);
con.add(buttonDraw);
buttonDraw.addActionListener(new MyAction());
}
@Override
public void paint (Graphics s)
{
super.paint(s);
if(rbR.isSelected())
{
s.drawRect(x, y, hight, width);
}else{
s.drawOval(x, y, hight, width);
//
}
}
public class MyAction implements ActionListener{
public void actionPerformed(ActionEvent e){
x=Integer.valueOf(textf[0].getText());
y=Integer.valueOf(textf[1].getText());
hight=Integer.valueOf(textf[2].getText());
width=Integer.valueOf(textf[3].getText());
repaint();
}
}
public static void main(String[]args){
ShapesFrame sf= new ShapesFrame("Drawing test");
sf.setVisible(true);
}
}
The problem I think in paint() & repiant()
What shall I do to draw the graphic that might be selected once the "Draw" button is pressed?!!
By the way , I'm using netBeans 6.9
Thanks indeed ^^
Edited by <Java>, 11 December 2011 - 10:54 AM.


Sign In
Create Account


Back to top









