Hi,
bevor mir unterstellt wir, ich hätte nicht gegooglet - das habe ich, aber nichts Brauchbares gefunden.
Ich möchte ein Applet in einen JFrame einbinden, damit es auch außerhalb des Browsers (als .jar) gestartet werden kann.
Der Frame existiert, aber anstatt das Applets gibts nur eine transparente Fläche.
Hier der Code:
(Ich weiß, kein besonders tolles Spiel, aber eine bessere Idee hatte ich nicht xD)
Ich hoffe, ihr könnt mir helfen (bin am verzweifeln), schonmal danke und Gruß
shadow
bevor mir unterstellt wir, ich hätte nicht gegooglet - das habe ich, aber nichts Brauchbares gefunden.
Ich möchte ein Applet in einen JFrame einbinden, damit es auch außerhalb des Browsers (als .jar) gestartet werden kann.
Der Frame existiert, aber anstatt das Applets gibts nur eine transparente Fläche.
Hier der Code:
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Timer;
public class Shooting extends JApplet implements ActionListener, MouseListener
{
/**
* Serial Version
*/
public static final long serialVersionUID = 2011-01-27/01;
/**
* Declarations
*/
int controller, xco, yco, xpos ,ypos, score, time;
boolean hit;
Image welcome_screen, bg, snowman, finish_screen;
MediaTracker MT;
Button start, exit;
Random R;
Timer T;
Font standard, special;
/**
* Initializations
*/
public void init()
{
setLayout(null);
R = new Random();
standard = new Font("Arial", Font.BOLD, 16);
special = new Font("Arial", Font.BOLD, 23);
start = new Button("Start");
exit = new Button("Exit");
start.setBounds(280, 440, 100, 20);
exit.setBounds(280, 440, 100, 20);
add(start);
start.addActionListener(this);
MT = new MediaTracker(this);
welcome_screen = getImage(getDocumentBase(), "Shooting_WelcomeScreen.png");
bg = getImage(getDocumentBase(), "Shooting_BackgroundImage.png");
snowman = getImage(getDocumentBase(), "Shooting_Snowman.png");
finish_screen = getImage(getDocumentBase(), "Shooting_FinishScreen.png");
MT.addImage(welcome_screen, 1);
MT.addImage(bg, 2);
MT.addImage(snowman, 3);
MT.addImage(finish_screen, 4);
try
{
MT.waitForAll();
}
catch(InterruptedException IE)
{
JOptionPane.showMessageDialog(null, "Fehler:\n"+IE, "Fehlermeldung", JOptionPane.ERROR_MESSAGE);
}
controller = 0;
T = new Timer();
addMouseListener(this);
}
/**
* Paint JApplet
*/
public void paint(Graphics g)
{
if(controller == 1)
{
g.setColor(Color.white);
g.fillRect(0, 0, 640, 480);
g.drawImage(bg, 0, 0, this);
g.drawImage(snowman, xco, yco, this);
if(hit)
{
score++;
}
g.setColor(Color.black);
g.setFont(standard);
g.drawString("Score: "+score+" Time: "+time, 0, 460);
}
else if(controller == 2)
{
g.setColor(Color.white);
g.fillRect(0, 0, 640, 480);
g.drawImage(finish_screen, 0, 0, this);
g.setColor(Color.black);
g.setFont(special);
g.drawString("Your Score: "+score, 60, 240);
T.cancel();
exit.setVisible(true);
exit.addActionListener(this);
add(exit);
}
else
{
g.drawImage(welcome_screen, 0, 0, this);
}
}
public void actionPerformed(ActionEvent AE)
{
if(AE.getSource() == start)
{
score = 0;
time = 60;
start.setVisible(false);
exit.setVisible(false);
controller = 1;
T.schedule(new TimerTask()
{
public void run()
{
xco = R.nextInt(540);
yco = R.nextInt(380);
time--;
if(time == 0)
{
controller = 2;
}
hit = false;
repaint();
}
}, 0, 1000);
repaint();
}
else if(AE.getSource() == exit)
{
System.exit(0);
}
}
public void mouseClicked(MouseEvent ME)
{
xpos = ME.getX();
ypos = ME.getY();
if (xpos > xco && xpos < xco+100 && ypos > yco && ypos < yco+100)
{
xco = R.nextInt(540);
yco = R.nextInt(380);
hit = true;
repaint();
}
else
{
hit = false;
}
}
public void mouseEntered(MouseEvent ME) {}
public void mouseExited(MouseEvent ME) {}
public void mousePressed(MouseEvent ME) {}
public void mouseReleased(MouseEvent ME) {}
public static void main(String[] args)
{
Shooting game = new Shooting();
JFrame main = new JFrame();
JLabel head = new JLabel("Instructions: Click to shoot!");
main.setLayout(new BorderLayout());
main.setTitle("Shoot The Evil Snowman!");
main.setSize(new Dimension(640, 480));
main.getContentPane().add(head, BorderLayout.NORTH);
main.getContentPane().add(game, BorderLayout.CENTER);
main.setVisible(true);
}
}
(Ich weiß, kein besonders tolles Spiel, aber eine bessere Idee hatte ich nicht xD)
Ich hoffe, ihr könnt mir helfen (bin am verzweifeln), schonmal danke und Gruß
shadow