hi Leute,
bin ein ziemlicher nap in sachen html und websites.
Hab jetz aber schon ein paar Java Programme geschrieben und
gehört man kann die in websites einbinden indem man sie zu Applets macht.
Mit extends Applet irgendwie...
1. Muss ich da jetz meinen code komplett umwerfen oder geht das ganz leicht so ein programm hochzuladen,
dass sich in nem JFrame abspielt und noch kein Applet extended hat?
2. weiß jemand ein leicht verständliches Tutorial mit dem sogar ein 0-html-checker wie ich sowas machen kann?
(Das programm dass ich hochladen will is ein Spiel: Pong - 2 balken links und rechts, ein ball wird hin under hergeschossen. hab nur 2 Klassen : Main und TimerTask).
(mein JFrame MainFrame war 600x600)
Error: java.lang.reflect.invocationTargetException....
Website: http://hyperpong.bplaced.net/
Main:
TimerTask:
bin ein ziemlicher nap in sachen html und websites.
Hab jetz aber schon ein paar Java Programme geschrieben und
gehört man kann die in websites einbinden indem man sie zu Applets macht.
Mit extends Applet irgendwie...
1. Muss ich da jetz meinen code komplett umwerfen oder geht das ganz leicht so ein programm hochzuladen,
dass sich in nem JFrame abspielt und noch kein Applet extended hat?
2. weiß jemand ein leicht verständliches Tutorial mit dem sogar ein 0-html-checker wie ich sowas machen kann?
(Das programm dass ich hochladen will is ein Spiel: Pong - 2 balken links und rechts, ein ball wird hin under hergeschossen. hab nur 2 Klassen : Main und TimerTask).
Code:
<!DOCTYPE html>
<html>
<body>
<CENTER>
<h1>Let's play HyperPong!</h1>
<p>Here's the game:</p>
<applet code=Main.class width="600" height="600"></CENTER>
</body>
</html>
Error: java.lang.reflect.invocationTargetException....
Website: http://hyperpong.bplaced.net/
Main:
Java:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.applet.Applet;
import java.awt.Graphics;
public class Main extends JFrame implements KeyListener
{
private JFrame MainFrame;
private TimerTask balkenTask, ballTask, spinTask;
private ArrayList<String> pressedKeys;
private int BalkenSpeed, balken1y, balken2y, ballx, bally, ballSpeedx, ballSpeedy, hitCounter, p1points, p2points;
private BufferedImage Balken1, Balken2, Ball;
private ImageIcon BalkenIcon1, BalkenIcon2, BallIcon;
private JLabel BalkenLabel1, BalkenLabel2, BallLabel;
private JLabel textarea1, textarea2, pointWinner;
private boolean spinActive = true;
public static void main( String [] args ) {
new Main(true);
}
public Main(boolean spin)
{
spinActive = spin;
MainFrame = new JFrame("Spiel");
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainFrame.getContentPane().setLayout(null);
MainFrame.getContentPane().setBackground(Color.BLACK);
MainFrame.setSize(600, 600);
MainFrame.setResizable(false);
pressedKeys = new ArrayList<String>();
BalkenSpeed = 3;
balken1y = 250;
balken2y = 250;
Balken1 = ImageToBufferedImage("images/Balken");
Balken2 = ImageToBufferedImage("images/Balken");
BalkenIcon1 = new ImageIcon(Balken1);
BalkenIcon2 = new ImageIcon(Balken2);
BalkenLabel1 = new JLabel(BalkenIcon1);
BalkenLabel2 = new JLabel(BalkenIcon2);
BalkenLabel1.setBounds(10,balken1y,10,100);
BalkenLabel2.setBounds(580,balken2y,10,100);
MainFrame.getContentPane().add(BalkenLabel1);
MainFrame.getContentPane().add(BalkenLabel2);
hitCounter = 0;
textarea1 = new JLabel("0");
textarea2 = new JLabel("0");
textarea1.setBounds(10,500,100,100);
textarea2.setBounds(490,500,100,100);
textarea1.setForeground(Color.WHITE);
textarea2.setForeground(Color.WHITE);
textarea1.setHorizontalAlignment( SwingConstants.CENTER);
textarea2.setHorizontalAlignment( SwingConstants.CENTER);
MainFrame.getContentPane().add(textarea1);
MainFrame.getContentPane().add(textarea2);
pointWinner = new JLabel("Start mit SPACE.");
pointWinner.setBounds(100,250,400,200);
pointWinner.setForeground(Color.GREEN);
pointWinner.setHorizontalAlignment(SwingConstants.CENTER);
MainFrame.getContentPane().add(pointWinner);
ballSpeedx = 5;
ballSpeedy = 0;
bally = 300;
ballx = 300;
Ball = ImageToBufferedImage("images/Ball");
BallIcon = new ImageIcon(Ball);
BallLabel = new JLabel(BallIcon);
BallLabel.setBounds(ballx,bally,10,10);
MainFrame.getContentPane().add(BallLabel);
balkenTask = new TimerTask(this, 1);
ballTask = new TimerTask(this, 2);
spinTask = new TimerTask(this, 3);
MainFrame.addKeyListener(this);
MainFrame.setVisible(true);
MainFrame.requestFocus();
}
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_SPACE)
{
balkenTask.start();
ballTask.start();
if(spinActive == true)
spinTask.start();
MainFrame.getContentPane().remove(pointWinner);
MainFrame.repaint();
}
if(code == KeyEvent.VK_W)
{
addKey("W");
}
if(code == KeyEvent.VK_S)
{
addKey("S");
}
if(code == KeyEvent.VK_UP)
{
addKey("UP");
}
if(code == KeyEvent.VK_DOWN)
{
addKey("DOWN");
}
}
public void keyReleased(KeyEvent e)
{
int code = e.getKeyCode();
if(code == KeyEvent.VK_W)
{
removeKey("W");
}
if(code == KeyEvent.VK_S)
{
removeKey("S");
}
if(code == KeyEvent.VK_UP)
{
removeKey("UP");
}
if(code == KeyEvent.VK_DOWN)
{
removeKey("DOWN");
}
}
public void addKey(String key)
{
if(!pressedKeys.contains(key))
pressedKeys.add(key);
}
public void removeKey(String key)
{
pressedKeys.remove(pressedKeys.indexOf(key));
}
public void moveBalken()
{
if(pressedKeys.contains("W"))
{
if(balken1y > 5)
balken1y -= 10;
}
if(pressedKeys.contains("S"))
{
if(balken1y < 470)
balken1y += 10;
}
if(pressedKeys.contains("UP"))
{
if(balken2y > 5)
balken2y -= 10;
}
if(pressedKeys.contains("DOWN"))
{
if(balken2y < 470)
balken2y += 10;
}
BalkenLabel1.setBounds(10,balken1y,10,100);
BalkenLabel2.setBounds(580,balken2y,10,100);
}
public void moveBall()
{
ballx += ballSpeedx;
bally += ballSpeedy;
if((ballx >= 575) && (BallTrifftBalken(2)))
{
ballSpeedx = -ballSpeedx;
hitCounter++;
if(hitCounter%5 == 0)
increaseSpeed();
}
if((ballx <= 15) && (BallTrifftBalken(1)))
{
ballSpeedx = -ballSpeedx;
hitCounter++;
if(hitCounter%5 == 0)
increaseSpeed();
}
if(bally >= 560)
{
ballSpeedy = -ballSpeedy;
}
if(bally <= 0)
{
ballSpeedy = -ballSpeedy;
}
BallLabel.setBounds(ballx,bally,10,10);
}
public BufferedImage ImageToBufferedImage(String imagepath)
{
BufferedImage img = toBufferedImage(new ImageIcon(imagepath + ".png").getImage());
return img;
}
public BufferedImage toBufferedImage(Image image)
{
image = new ImageIcon(image).getImage();
BufferedImage bimage = new BufferedImage(image.getWidth(null), image
.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public boolean BallTrifftBalken(int BalkenNummer)
{
boolean trifft = false;
if(BalkenNummer == 1)
{
if((bally+10 > balken1y) && (bally < balken1y+100))
{
trifft = true;
if((bally+5 > balken1y) && (bally+5 <= balken1y+20))
ballSpeedy -= 4;
if((bally+5 > balken1y+20) && (bally+5 <= balken1y+40))
ballSpeedy-=2;
if((bally+5 > balken1y+60) && (bally+5 <= balken1y+80))
ballSpeedy+=2;
if((bally+5 > balken1y+80) && (bally+5 <= balken1y+100))
ballSpeedy += 4;
if(pressedKeys.contains("W"))
spinTask.setDirection("UP");
if(pressedKeys.contains("S"))
spinTask.setDirection("DOWN");
if((!pressedKeys.contains("W")) && (!pressedKeys.contains("S")))
spinTask.setDirection("Zero");
}
if(trifft == false)
givePoints(2);
}
else if(BalkenNummer == 2)
{
if((bally+10 > balken2y) && (bally < balken2y+100))
{
trifft = true;
if((bally+5 > balken2y) && (bally+5 <= balken2y+20))
ballSpeedy -= 4;
if((bally+5 > balken2y+20) && (bally+5 <= balken2y+40))
ballSpeedy-=2;
if((bally+5 > balken2y+60) && (bally+5 <= balken2y+80))
ballSpeedy+=2;
if((bally+5 > balken2y+80) && (bally+5 <= balken2y+100))
ballSpeedy += 4;
if(pressedKeys.contains("UP"))
spinTask.setDirection("UP");
if(pressedKeys.contains("DOWN"))
spinTask.setDirection("DOWN");
if((!pressedKeys.contains("UP")) && (!pressedKeys.contains("DOWN")))
spinTask.setDirection("Zero");
}
if(trifft == false)
givePoints(1);
}
return trifft;
}
public void increaseSpeed()
{
if((ballSpeedx < 20) && (ballSpeedx > 0))
ballSpeedx+=2;
if((ballSpeedx > -20) && (ballSpeedx < 0))
ballSpeedx-=2;
}
public void givePoints(int player)
{
if(player == 1)
p1points++;
else
p2points++;
balkenTask.stop();
ballTask.stop();
spinTask.stop();
spinTask.setDirection("Zero");
textarea1.setText("" + p1points);
textarea2.setText("" + p2points);
if(player == 1)
ballSpeedx = 5;
else
ballSpeedx = -5;
ballSpeedy = 0;
bally = 300;
ballx = 300;
BallLabel.setBounds(ballx,bally,10,10);
balken1y = 250;
balken2y = 250;
BalkenLabel1.setBounds(10,balken1y,10,100);
BalkenLabel2.setBounds(580,balken2y,10,100);
pointWinner.setText("Punkt fuer Spieler " + player + ". Weiter mit SPACE.");
MainFrame.getContentPane().add(pointWinner);
}
public void doSpin(String dir)
{
if(dir.equals("UP"))
if(ballSpeedy > -10)
ballSpeedy--;
if(dir.equals("DOWN"))
if(ballSpeedy < 15)
ballSpeedy++;
}
}
TimerTask:
Java:
import java.awt.event.*;
import javax.swing.*;
public class TimerTask implements ActionListener
{
private Timer t;
private Main main;
private int ID, speed, spinStaerke;
private String direction = "Zero";
public TimerTask(Main main, int id)
{
this.main = main;
ID = id;
speed = 20;
if(ID == 1)
t = new Timer(20, this);
if(ID == 2)
t = new Timer(20, this);
if(ID == 3)
t = new Timer(200, this);
}
public void actionPerformed(ActionEvent e)
{
if(ID == 1)
main.moveBalken();
if(ID == 2)
main.moveBall();
if(ID == 3)
{
main.doSpin(direction);
}
}
public void start()
{
t.start();
}
public void stop()
{
t.stop();
}
public void setDirection(String s)
{
direction = s;
}
}