Search code examples
javaswinguser-interfacejapplet

How do I run a JApplet from itself?


Basically I need to do this for school, Ive been through all kinds of posts about this and everyone just says "why'd you wanna do that?" and don't answer. So a lot of people need help on this and your answer could get a lot of likes someday

So here's my class - what couple lines of code do i need to add to main to make this JApplet pop up and draw the bricks into a JApplet window?

public class Wall extends JApplet {
ArrayList<Brick> bricks = new ArrayList<Brick>();
Color[] colors = {Color.decode("#1abc9c"), Color.decode("#f1c40f"), Color.decode("#d35400"), Color.decode("#e74c3c"), Color.decode("#2ecc71"), Color.decode("#3498db"), Color.decode("#9b59b6"), Color.decode("#34495e")};
ArrayList<Integer> usedInts = new ArrayList<Integer>();

public void makeBricks(){
    int xPos = 20;
    int yPos = 50;
    int height = 50;
    int width = 60;
    for(int i=0; i<8;i++){
        Brick b = new Brick();
        b.setxPosition(xPos);
        xPos =+60;
        b.setyPosition(yPos);
        if (xPos == 200){
            yPos+=50;
        }
        b.setColor(randomColor());
        b.setHeight(height);
        b.setWidth(width);
        bricks.add(b);

    }
}
public Color randomColor(){
    Random r = new Random(System.currentTimeMillis());
    boolean allAssigned = false;
    while(!allAssigned){
        int newInt = r.nextInt(8);
        if(!usedInts.contains(newInt)){
            usedInts.add(newInt);
            return colors[newInt];
        }
        if(usedInts.size()>7){
            usedInts.clear();
        }
    }
    return Color.BLACK;
}

public void draw(Graphics g) {
    for(Brick b: bricks){
        b.draw(g);
    }

}
@Override
public void paint(Graphics g){
    draw(g);
}


public static void main(String[] args) {
//these lines do not work
Wall wall = new Wall();    
wall.makeBricks();
wall.draw();

}

}

Solution

  • JApplet's don't have a window of their own, they are embedded within a web page by a browser. It's possible to use the applet viewer to display them, but you'd need to do that from the command line

    Start by creating a custom class the extends from something like JPanel, override it's paintComponent and perform you custom painting there. See Performing Custom Painting for more details.

    In your main method, create a new JFrame and add your "game panel" to it...

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }
    
                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new GamePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
    

    If you MUST have a applet, you can now add the "game panel" to it as well

    See How to Make Frames (Main Windows) and Using Top-Level Containers for more details