0

Essentially the question says it all. I have a program where I initiate the canvas (JApplet) and set its size to not the default values. I wait for the timer to end and then it resizes. I don't want a delay, I aiming for instant.

How do I initiate the applet with a different size or background color on its first instance?

import javax.swing.JApplet; 
import java.awt.MouseInfo;
import java.awt.Graphics;

public class GameBoard extends JApplet {

    public void paint(Graphics canvas) {
        setSize(100,450);
        // TODO Auto-generated method stub
        //int 
        int i = 0;
        while(i < 2){
            waiting(1.0);
            i++;
        }
    }

public static void waiting (double n){
        long t0, t1;
        t0 =  System.currentTimeMillis();
        do{
        t1 = System.currentTimeMillis();
        }
        while ((t1 - t0) < (n * 1000));
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1. DON'T call `setSize` inside `paint` EVER. 2. DON'T "pause" inside `paint` EVER - or for that fact, minimize all potential delays in the EDT full stop. I though the `HTML` tag specified the preferred size of the applet, not the applet itself – MadProgrammer May 07 '13 at 04:04
  • 2
    Don't override the paint() method. You are not using a Timer. You are using a loop which is not the way to do animation. Read the Swing tutorial on [How to Make Applets](http://docs.oracle.com/javase/tutorial/uiswing/components/applet.html) for the proper way to write an applet. – camickr May 07 '13 at 04:04
  • @MadProgrammer Your technical points are spot on, if a little.. enthusiastic. An applet does not have the luxury of setting its own size, it is a guest in the HTML that gives it whatever size is declared in the applet element. – Andrew Thompson May 07 '13 at 04:17
  • @AndrewThompson Sorry, people screwing with `paint` just get me so :@ – MadProgrammer May 07 '13 at 04:22
  • 1
    @MadProgrammer I spent the better part of yesterday writing a script that includes my most sage comments. Click each check box & the result is shown in a text area that I can copy/paste. It might get to stage where I'll select 1,6,9 & 11, make a comment then opt out till I get a notification or notice it in the daily round-up. It was a very calming process.. :) – Andrew Thompson May 07 '13 at 04:30
  • @AndrewThompson I just need a bot to do the same thing :P – MadProgrammer May 07 '13 at 04:31

1 Answers1

1

How do I initiate the applet with a different size or background color on its first instance?

This is really two separate things, so I'll break it down.

  1. Applet size
  2. BG Color on load

Applet Size

The applet size is set in HTML. The applet viewer now supports parsing an applet element form the applet's source code file (that's right, the .java file), like this..

import javax.swing.*;

/* <applet code='HelloWorldApplet' width=400 height=100></applet> */
public class HelloWorldApplet extends JApplet {

    @Override
    public void init() {
        add(new JLabel("Hello World!"));
        validate();
    }
}

Applet BG color

See Special attributes of applets: boxbgcolor, boxfgcolor...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Also consider a [hybrid applet/application](http://stackoverflow.com/a/12449949/230513) deployed via [tag:javawebstart]. – trashgod May 07 '13 at 10:58