Hallo! iCh habe einen KeyListener im Konstruktor geadded und sobald man nach links oder rechts am steuerkreuz klickt soll er einen balken nach links bzw nach rechts zeichnen!!ich hab ein bild als hintergrund eingefügt und jetzt ist er nciht ehr zu sehen! ich habe versucht den keylistener im Button aufzurufen aber nix geht! bitte helft mir will den balken vom Keylistener auf dem im
age wandern lassen!Danke!
edited by thE_29: code Tags!
Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;
public class Break extends Frame implements KeyListener,Runnable{
Canvas cv;
Graphics g;
boolean lol= true;
boolean weiter= true;
int x=500, y=400,y1=2,x1=2;
int yPos=680;
int xPos=350;
int hoehe=15;
int breite=150;
Color black = new Color(0,0,0);
Color c;
Thread th;
public Break(){
super("wallbreaker");
setSize(1000,800);
setLayout(new FlowLayout());
cv = new Canvas();
cv.setSize(900,750);
Button bt=new Button("lol");
add(cv);
add(bt);
addKeyListener(this);
th=new Thread(this);
bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
zeichnen();
th.start();
;
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0){
System.exit(0);
}
});
}
public void zeichnen() {
Graphics g;
g=cv.getGraphics();
Image img2 = getToolkit().getImage("back.jpg");
MediaTracker mt2 = new MediaTracker(this);
mt2.addImage(img2, 0);
try {
mt2.waitForAll();
} catch (InterruptedException e) {
}
g.drawImage(img2,0,0,this);
}
public void run(){
g=cv.getGraphics();
while(weiter){
if(x>=cv.getWidth()-10||x<=0){
x1=-x1;
}
if(y>=cv.getHeight()-10||y<=0){
y1=-y1;
}
x=x+x1;
y=y+y1;
g.fillOval(x,y,10,10);
try{
Thread.sleep(10);
}
catch(InterruptedException h){}
zeichnen();
treffen(x,y);
}
}
public void keyPressed(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.VK_LEFT) {
if(xPos<=15){
xPos=15;
}
g.setColor(new Color(200,200,255));
g.fillRect(0,680,1000,30);
g.setColor(Color.BLACK);
xPos=xPos-10;
g.fillRect(xPos,yPos,breite,hoehe);
}if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
if(xPos>=825){
xPos=825;}
g.setColor(new Color(200,200,255));
g.fillRect(0,680,1000,30);
g.setColor(Color.BLACK);
xPos=xPos+10;
g.fillRect(xPos,yPos,breite,hoehe);
}
}
public void keyReleased(KeyEvent event){
}
public void keyTyped(KeyEvent event){}
public void treffen(int x, int y){
Robot r;
try{
r = new Robot();
c = r.getPixelColor(x,y);
}catch(AWTException h){}
if(black.equals(c)){
y1=-y1;
}
}
public static void main(String[] args) {
Break f = new Break();
f.show();
}
}
age wandern lassen!Danke!
edited by thE_29: code Tags!