Search code examples
javagraphicsjpanelpaintcomponentjapplet

Finding the width and height of a JApplet


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.


Solution

  • The problem starts here..

    Main m = new Main();
    

    This is a new applet, one that is never shown on screen & which has (by default) a size of 0 x 0 pixels.

    The correct approach here is to entirely ignore the parent container. All that is relevant is the size of the panel in which custom painting is being done. So..

     g.drawOval(m.x, m.y, m.width, m.height);
    

    Should be:

    g.drawOval(0, 0, getWidth(), getHeight());
    

    Re applets:

    1. Why code an applet? If it is due to the teacher specifying it, please refer them to Why CS teachers should stop teaching Java applets.
    2. See Java Plugin support deprecated and Moving to a Plugin-Free Web.

    Other tips:

    1. No applet should ever try to resize itself. The size is set in the HTML that launches it, so remove this line.

      setSize(1000, 500);
      
    2. These four attributes are all defined in the Component super class of the applet. An applet inherits them. Don't redefine them, as that can only cause confusion. If the default attributes give the information needed, use them. If not, then name your attributes differently. Given they seem to be involved in drawing a circle, I suggest circleX. circleY, circleWidth & circleHeight.

      public int x, y, width = 40, height = 40;