This program is asking the user to input the data of a circle. The following are included: the position of x, position of y, and the width and height of the circle.
So when I test this program, I enter the location and its size; and then I hit the draw button. The circle did not appear.
Here is my code!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Draw extends JFrame
{
private JButton draw;
private JTextField posOfX;
private JTextField posOfY;
private JTextField Jwidth;
private JTextField Jheight;
private ActionListener listener;
private JLabel JLx;
private JPanel drawingPanel;
private JLabel JLy;
private JLabel JLwidth;
private JLabel JLheight;
private JComponent component;
public int x =100 ;
public int y =100 ;
public int width = 100;
public int height = 100 ;
private JPanel panel;
public Draw()
{
listener = new actionPerform();
component = new drawCircle();
panel = new JPanel();
draw = new JButton ("Draw");
draw.addActionListener(listener);
posOfX = new JTextField( 15);
posOfY = new JTextField(15);
Jwidth = new JTextField(15);
Jheight = new JTextField(15);
JLx = new JLabel("X");
JLy = new JLabel("Y");
JLwidth = new JLabel("Width");
JLheight = new JLabel("Height");
panel.add(JLx);
panel.add(posOfX);
panel.add(JLy);
panel.add(posOfY);
panel.add(JLwidth);
panel.add(Jwidth);
panel.add(JLheight);
panel.add(Jheight);
panel.add(draw);
panel.add(component);
add(drawingPanel,BorderLayout.SOUTH);
add(panel,BorderLayout.NORTH);
}
class drawCircle extends JComponent
{
public void paintComponent(Graphics g)
{
g.drawOval(x,y,width,height);
}
}
class actionPerform implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
if(e.getSource() == draw)
{
width = width + Integer.parseInt(Jwidth.getText());
height = height + Integer.parseInt(Jheight.getText());
x = Integer.parseInt(posOfX.getText()) + width;
y = Integer.parseInt(posOfY.getText())+ height;
Jwidth.setText("");
Jheight.setText("");
posOfX.setText("");
posOfY.setText("");
}
}
catch (Exception except)
{
Jwidth.setText("");
Jheight.setText("");
posOfX.setText("");
posOfY.setText("");
JOptionPane.showMessageDialog(null,"You should enter numbers only","Error",JOptionPane.ERROR_MESSAGE);
}
}
}
}
import java.awt.*;
import javax.swing.*;
public class DrawViewer
{
public static void main(String []args)
{
Draw d = new Draw();
d.setVisible(true);
d.setTitle("Draw circle");
d.setSize(1000,1000);
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
When creating a new JComponent
object without a layout manager, the width of its preferred size is initially 0 and its height 0, so it's not at all visible. To fix this, your drawCircle
class should override the getPreferredSize
method so that the component you add it to knows what its size should be. For example:
@Override
public Dimension getPreferredSize() {
return new Dimension(width, height);
}
It is strongly encouraged that you use a layout manager of some sort, however, so that you won't end up with various problems such as the size of components being wrong.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments