0

So I have been working on this tutorial for a couple of days, and I noticed that when I wrote a class that extended Applet I had to click the X twice.

When I click it the first time the "canvas" disappears but the window remains. Then with a second click to the X the window disappears.

I have looked around and found little help. I know how to do this with JFrame and Frame, but I don't have much experience with Applet.

I have tried to do it the same as I would with Frame, but thats not right. Before writing this I tried to get a hold of the parent of my Applet hoping I could add a WindowListener, but I had little to no success.

Any help would be great. Heres my code...I'm thinking the problem is in the init(), but there might be something else I'm missing. And heres the link to the tutorial if you would like a reference for 2D game java development.

package edu.KiloBotGame.njh;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

public class Renderer extends Applet{

    static int [][] map;
    static int rows, cols;

    public Renderer() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init() {
        setSize(800,480);
        setBackground(Color.BLACK);
        createTileMap();
    }

    private void createTileMap() {
        map = new int[50][30];
        rows = map.length;
        cols = map[0].length;
        Random r = new Random();
        for(int i = 0; i < rows; i++){
            for(int j = 0;j < cols; j++){
                map[i][j] = r.nextInt(5);
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        for(int i =0; i < rows; i ++){
            for(int j =0; j < cols;j++){
                int mod_i = i*16;
                int mod_j = j*16;
                switch(map[i][j]){
                case 0:
                    g.setColor(Color.RED);
                    g.fillRect(mod_i, mod_j, 16, 16);
                    break;
                case 1:
                    g.setColor(Color.BLUE);
                    g.fillRect(mod_i, mod_j, 16, 16);
                    break;
                case 2:
                    g.setColor(Color.YELLOW);
                    g.fillRect(mod_i, mod_j, 16, 16);
                    break;
                case 3:
                    g.setColor(Color.GREEN);
                    g.fillRect(mod_i, mod_j, 16, 16);
                    break;
                case 4:
                    g.setColor(Color.ORANGE);
                    g.fillRect(mod_i, mod_j, 16, 16);
                    break;
                }
            }
        }
    }
}
Noah Herron
  • 630
  • 4
  • 23
  • 1
    1- Using the applet view, I have no issue. 2- `java.awt.Applet` are some 15+ years out of date, event using a `javax.swing.JApplet` is not recommended as they do not make a good platform for learning from, they have too many issues which you won't have in normal, every day development. – MadProgrammer Aug 20 '14 at 00:21
  • How are you showing the applet?? – MadProgrammer Aug 20 '14 at 00:22
  • Thats a good question lol. I just noticed I don't have a main :-\ I'm away from my computer at the moment. Ill give it a go when I get home. Copy paste my code if you don't mind and see if it at least runs. – Noah Herron Aug 20 '14 at 00:30
  • 1
    It runs fine...applets aren't meant to be loaded within frames, they're meant to be load in html pages... – MadProgrammer Aug 20 '14 at 00:31
  • I thought about it a little and wonder if Renderer should implement windowlistener. I don't know if that will work. – Noah Herron Aug 20 '14 at 00:31
  • I wouldn't think so, technically, `Applet` isn't really a window – MadProgrammer Aug 20 '14 at 00:31
  • 1
    1) Why code an applet? If it is due to spec by your instructor, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT components rather than Swing? See [this answer](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon AWT. – Andrew Thompson Aug 20 '14 at 14:13
  • Im following a tutorial thats why I'm using Applet. There has to be a way. @Andrew Thompson I'll check those links when I get home – Noah Herron Aug 20 '14 at 18:46
  • I just changed it to a JFrame and it works nicely. Looking at the tutorial again, we were using Applet because it is "easy to embed in HTML" http://www.kilobolt.com/day-1-foundations (I'll have to learn how to do that soon) Thanks guys for your comments! – Noah Herron Aug 20 '14 at 22:10

0 Answers0