简体   繁体   中英

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();

}

}

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

As mentioned by others, applets don't normally have a standalone window. On the other hand, there are at least 3 ways in which an applet can be included in a free floating window:

  • Run the applet in applet viewer 1.1 . This should be seriously considered, since the applet viewer is designed to display applets. Better still, it will recreate most of the environment of an applet in a web page, including creating an applet context (from which to get the document or code base, or applet parameters) and a security sand-box.
  • If you mean 'free floating for the end user'. Launch the applet free floating using Java Web Start . JWS uses the applet viewer (again) to show the applet.
  • A hybrid application/applet 1.2 . This is more like you described, an applet with a main(String[]) that creates a wrapper frame for the applet and calls the init() method etc. This is handy for testing simpler (no parameters etc.) applets where the security sand-box just gets in the way.

I also had a site where I provided Appleteer, similar to applet viewer except it would launch multiple applets in a single HTML document, whereas the applet viewer would split them into separate free floating windows (and other slight differences - Appleteer had no security sandbox..). Unfortunately my free hosting for the sites stopped running the web hosting side of the business!

  1. This answer to How to call a paint method inside Applet extended class?
    1. Update 1 has an example of launching an applet in the applet viewer.
    2. Update 2 has an example of creating a hybrid application/applet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM