I am looking to find the width and height of a JApplet
. I have tried different things and have looked for an answer but haven't found one yet.
This below is the main part of the code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.Timer;
public class Main extends JApplet implements ActionListener {
private static final long serialVersionUID = 1L;
Timer timer;
public int x, y, width = 40, height = 40;
public void init() {
Painter painter = new Painter();
JApplet component = new JApplet();
x = (component.getWidth())/2 - (width/2) - 20;
y = (component.getHeight())/2 - (height/2) - 40;
painter.setBackground(Color.white);
setContentPane(painter);
setSize(1000, 500);
}
public void start() {
if (timer == null) {
timer = new Timer(100, this);
timer.start();
} else {
timer.restart();
}
}
public void stop() {
if (timer != null) {
timer.stop();
timer = null;
}
}
public void actionPerformed(ActionEvent ae) {
repaint();
}
}
Below is the code for painting a circle:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Painter extends JPanel {
private static final long serialVersionUID = 2L;
Main m = new Main();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawOval(m.x, m.y, m.width, m.height);
}
}
The above still generates the circle at the top right corner of the JApplet
frame at 0,0
but it is supposed to be in the center.