Hi,
ich versuche gerade ein Pong spiel anzufangen,aber es kommt schon das erste problem!
Das applet ist 800x600 Groß, mein PC hat ~500mhz.
Und es laagt sehr stark, wenn man die maus bewegt!
Kann da mir jemand helfen?
Sobald man auf Hosten klickt sieht man das "laagige (oO?)" rechteck!
ich versuche gerade ein Pong spiel anzufangen,aber es kommt schon das erste problem!
Das applet ist 800x600 Groß, mein PC hat ~500mhz.
Und es laagt sehr stark, wenn man die maus bewegt!
Kann da mir jemand helfen?
Code:
//import java.awt.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class main extends Applet implements Runnable,ActionListener,MouseMotionListener
{
/**
*
*/
private static final long serialVersionUID = -5881627292884828042L;
Socket s;
ServerSocket server;
int Port = 1337;
//Buttons
Button BHost;
Button BClient;
TextField TIP;
int x = 0;
int y = 0;
boolean ingame = false;
//Double-Buffering
Graphics bufferGraphics;
Image offscreen;
Dimension applet;
public void init()
{
setLayout(null);
setBackground(Color.BLACK);
BHost = new Button("Hosten");
BClient = new Button("Joinen");
TIP = new TextField("127.0.0.1",100 );
BHost.setBounds(20,20,50,20);
BClient.setBounds(20,60,50,20);
TIP.setBounds(20,90,200,20);
add(BHost);
add(BClient);
add(TIP);
BHost.addActionListener(this);
BClient.addActionListener(this);
addMouseMotionListener(this);
//Double-Buffering
applet = getSize();
offscreen = createImage(applet.width,applet.height);
bufferGraphics = offscreen.getGraphics();
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == BHost)
{
//Hosten
System.out.println("Bin Hoster!");
remove(BHost);
remove(BClient);
remove(TIP);
try
{
Server();
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
ingame = true;
}
else if(evt.getSource() == BClient)
{
//Joinen
System.out.println("Bin Joiner!");
remove(BHost);
remove(BClient);
try
{
join(TIP.getText());
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
remove(TIP);
ingame = true;
}
}
//Server
public void Server() throws UnknownHostException, IOException
{
/*server = new ServerSocket(Port);
s = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String text = in.readLine();
System.out.println(text);
in.close();
server.close();
*/
}
//Joinen
public void join(String IP) throws UnknownHostException, IOException
{
s = new Socket(IP,Port);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("Gast joint!");
out.newLine();
out.flush();
out.close();
}
public void mouseMoved(MouseEvent mouseevt)
{
x = mouseevt.getX();
y = mouseevt.getY();
repaint();
}
public void mouseDragged(MouseEvent me)
{
}
public void paint(Graphics g)
{
if(ingame==true)
{
bufferGraphics.clearRect(0,0,applet.width,applet.width);
bufferGraphics.setColor(new Color(255,255,255));
bufferGraphics.fillRect(x,y,80,40);
bufferGraphics.setColor(new Color(0,0,0));
bufferGraphics.drawRect(x,y,80,40);
g.drawImage(offscreen,0,0,this);
}
}
public void update(Graphics g)
{
paint(g);
}
//Tread
public void run()
{
// TODO Auto-generated method stub
}
}
Sobald man auf Hosten klickt sieht man das "laagige (oO?)" rechteck!