I'm just trying to draw circle using the drawOval() method and it shows only small square when I run the program. I was trying to add the constructor to the Surface class but it didn't work as well. Here is the code that I've been made:
package swing22;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame {
JFrame frame = new JFrame(" Test Frame ");
JPanel panel = new JPanel();
JButton button = new JButton("CLICK");
JLabel label = new JLabel(" 33 ");
public MyFrame(){
gui();
}
public void gui(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setBackground(Color.GREEN);
panel.add(button);
panel.add(label);
panel.add(new Surface());
frame.add(panel, BorderLayout.CENTER);
button.addActionListener(new Action());
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
frame.setResizable(false);
}
class Action implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("new value");
}
}
}
class Surface extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.red);
g.drawOval(80, 80, 30, 30);
g.fillArc(140, 140, 30, 30, 0, 90);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}