Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Applet painting clips to [195,200] despite resize
    primarykey
    data
    text
    <h2>Intention</h2> <p>I have a <code>java.applet.Applet</code> subclass <code>MyApplet</code> with a <code>java.awt.Canvas</code> subclass <code>MyCanvas</code> added to it.</p> <p>My code changes the size of <code>MyApplet</code> to <code>new Dimension(600,400)</code> and changes the size of <code>MyCanvas</code> to match.</p> <p>When <code>MyCanvas</code> is <code>paint</code>ed, it should</p> <ol> <li>fill its <strong>whole area</strong> with <strong>red</strong></li> <li>draw a <strong>blue circle</strong> of width 300 and height 300.</li> </ol> <h2>Problem</h2> <p>Instead (when run as a Java Applet from Eclipse), the paint of <code>MyCanvas</code> clips to a far smaller area than 600,400 (I measured it to be 195,200) even though <code>MyApplet</code> resizes correctly. <a href="http://dl.dropbox.com/u/196227/MyAppletContents.png" rel="nofollow noreferrer">This is what it looks like.</a> </p> <p><img src="https://i.stack.imgur.com/8AApg.png" alt="enter image description here"></p> <p>The printouts are OK too -- see bottom of post.</p> <h2>Code</h2> <p>This is my code:</p> <pre><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()); } } } </code></pre> <h2>Printouts</h2> <p>It produces the following printout:</p> <pre><code>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] </code></pre> <p>The sizes are correct throughout!</p> <h2>Attempted solutions</h2> <ul> <li>I tried <code>setBounds()</code> instead of <code>setSize()</code> both in <code>MyApplet</code> and <code>MyCanvas</code> just in case the position was offset toward the top-left. This just shifted the circle -- the clip persisted.</li> </ul> <p>Am I missing something?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload