2

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.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • Step 1) don't use a `JApplet`. Or any kind of Java applet. – Boris the Spider Aug 10 '16 at 21:35
  • How come? Why can't I use a JApplet? – 0xCursor Aug 10 '16 at 21:54
  • It's not supported by biggest browser currently in use - Chrome. It will be completely removed from Java in Java 9. It is **old**, **insecure** and **unsupported**. Don't use it. Don't learn it. – Boris the Spider Aug 10 '16 at 21:55
  • What should I use then, Applet? – 0xCursor Aug 10 '16 at 21:59
  • 1
    There is no more Java in the browser. It's done. You could look at Java WebStart. – Boris the Spider Aug 10 '16 at 21:59
  • Are webpages and browsers mostly made up of html now, with Java incorporated? – 0xCursor Aug 10 '16 at 22:03
  • 1
    *".. webpages and browsers mostly made up of html now"* Yes. With some styles (CSS) thrown in to make them pretty and JavaScript for anything that is 'dynamic'. *"..with Java incorporated?"* No. In fact, most browser manufacturers have removed all support for embedded Java applets. Sun/Oracle f**ked up the security one time too many, and they got sick of trying to support it (while keeping their users safe). – Andrew Thompson Aug 11 '16 at 10:16

2 Answers2

2

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;
    
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
-1

JFrame has a better implementation of this.

public class Main extends JFrame {

public static void main(String[] args) {
    setSize(500, 500);
    setVisible(true);
    setLayout(new FlowLayout()); // THIS LINE IS IMPORTANT
    // Put rest of code here :D
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • How is this helpful? – Boris the Spider Aug 10 '16 at 21:56
  • No, flow layout will flow around as the `JFrame` is resized. You need to look at a [`BorderLayout`](https://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html). – Boris the Spider Aug 11 '16 at 07:42
  • 1) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. 2) On a wider note, this is an extremely poor answer that the person asking should ignore. a) They were dealing with an applet, not a frame. b) they are doing custom painting, which should be inside the `paintComponent(Graphics)` method of a `JPanel` c) the frame decorations will mean that the panel is always smaller than the frame, so finding the frame size is irrelevant. - Minus one – Andrew Thompson Aug 11 '16 at 08:55