PDA

View Full Version : Two applets in the same JFrame?



JPHamlett
05-05-2011, 09:31 PM
Is there anyway I could make a jframe load with two applets loaded into it?

Here's my code

Application class



package board;

public class Application{

/**
*
*/
private static final long serialVersionUID = -5637213202384388870L;

public static void main(String[] args){
new Game().setVisible(true);
}

}


Game class



package board;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame{

public Game(){
JPanel theGame = new JPanel(new BorderLayout());
// theGame.setPreferredSize(new Dimension(768, 560));
super.setDefaultCloseOperation(EXIT_ON_CLOSE);
Board game = new Board();
PlayingDie dice = new PlayingDie(600, 0);
game.init();
game.start();
dice.init();
dice.start();
theGame.add(game);
theGame.add(dice);
super.getContentPane().add(theGame, BorderLayout.CENTER);
pack();
}



}


PlayingDie class


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package board;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

/**
*
* @author 729484
*/
public class PlayingDie extends Applet{

int x, y;
final int size = 308;

public PlayingDie(int x, int y){
this.x = x;
this.y = y;
}

public void init(){
setBackground(Color.blue);
setBounds(x, y, size, size);
}

}


Board class



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package board;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author 729484
*/
public class Board extends Applet {


public void init(){
setBackground(Color.red);
setBounds(0, 0, 10, 10);

}

}


I know that they really dont do anything right now, but I want to try and get the two applet thing first.

This is what I would like the end result to look like

http://intramission.webs.com/test.png

I've been trying for a little while and everything I do just gives me a solid color....

n3ss3s
05-05-2011, 09:58 PM
I am unable to decipher what does the illustrated end result have to do with two applets.

Alek000
05-05-2011, 10:15 PM
The Red and Blue? That's how he wants the two applets set up with it's done.

JPHamlett
05-06-2011, 01:43 AM
The Red and Blue? That's how he wants the two applets set up with it's done.

This. I want A red applet behind a blue applet


Edit : Got it!