2

Intention

I have a java.applet.Applet subclass MyApplet with a java.awt.Canvas subclass MyCanvas added to it.

My code changes the size of MyApplet to new Dimension(600,400) and changes the size of MyCanvas to match.

When MyCanvas is painted, it should

  1. fill its whole area with red
  2. draw a blue circle of width 300 and height 300.

Problem

Instead (when run as a Java Applet from Eclipse), the paint of MyCanvas clips to a far smaller area than 600,400 (I measured it to be 195,200) even though MyApplet resizes correctly. This is what it looks like.

enter image description here

The printouts are OK too -- see bottom of post.

Code

This is my code:

import java.applet.Applet;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

public class MyApplet extends Applet {

    Canvas mainCanvas;

    public void init() {

        // Set the size of the applet
        setSize(600, 400);

        // Print dimensions
        System.out.println("Applet dimensions: " + getSize());

        // Make a canvas with the same sizes as this applet
        mainCanvas = new MyCanvas(getWidth(), getHeight());
        add(mainCanvas);
    }

    public class MyCanvas extends Canvas {

        public MyCanvas(int w, int h) {

            setSize(w, h);

            System.out.println("Canvas dimensions: " + getSize());
        }

        public void paint(Graphics g) {

            g.setColor(Color.RED);
            System.out.println("Canvas dimensions when painting: " + getSize());
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }
}

Printouts

It produces the following printout:

Applet dimensions: java.awt.Dimension[width=600,height=400]
Canvas dimensions: java.awt.Dimension[width=600,height=400]
Canvas dimensions when painting: java.awt.Dimension[width=600,height=400]
Canvas dimensions when painting: java.awt.Dimension[width=600,height=400]

The sizes are correct throughout!

Attempted solutions

  • I tried setBounds() instead of setSize() both in MyApplet and MyCanvas just in case the position was offset toward the top-left. This just shifted the circle -- the clip persisted.

Am I missing something?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • "My code changes the size of `MyApplet`.." If you can find one browser that resizes an applet according to Java code, I'll find two that won't. – Andrew Thompson May 31 '11 at 14:09
  • I know. This is for testing. I don't want to re-JAR my code and open a browser every time I test my applet. Even if I did bother, I couldn't use the debugger. – Anko - inactive in protest May 31 '11 at 17:27

3 Answers3

3

In the first line of your init put this:

setLayout(new GridLayout(1,1));   

This will cause your canvas to take up all the space of your applet.

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
1

You want to be calling setPreferredSize and this explains the differences

Community
  • 1
  • 1
jberg
  • 4,758
  • 2
  • 20
  • 15
  • `setPreferredSize` makes it worse -- now `MyApplet` resizes to the size of the small clip! Also, the thread you linked to says to use `setSize` over `setPreferredSize` when I have no set layout manager, which is the case here. – Anko - inactive in protest May 31 '11 at 13:42
  • @Aku: "..I have no set layout manager.." You may not have *set* one, but there is a *default.* Prove it by putting this as the first line in the `init()` method: `System.out.println(getLayout());`. – Andrew Thompson May 31 '11 at 14:14
0

Finally found it!

The environment that Eclipse provides to Applets is by default configured to have size 200, 200. All painting clips to this area unless the default Run Configuration is changed.

This can be done as follows:

  1. Right-click the root folder of your applet in the Package view
  2. Select Run As... -> Run Configurations...
  3. Click the Parameters tab
  4. Change Width and Height fields as required.