0

我对java很陌生,但这不是重点,我试图让我的开始按钮打开我的显示肉,但我不知道怎么做。我只是不知道如何编写可以做到这一点的代码,所以我只是把它放在油漆中作为测试,看看我的按钮是否工作。

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Game2 extends Applet implements KeyListener, ActionListener
{
Color color1 ;
Image theworld2, BOAT, life, life2,plane;
char ch='*';
int a,s,d,w,i,k,l,j,X,Y,X1,Y1 ,b,count, lifex ,lifey,planeX,planeY,planeX1,planeY1,planeX2,planeY2;
String sr="Hello";
Button Start ;
public void init ()
{
    setSize(1700,900);
    requestFocus (); //make applet the active component for key events
    addKeyListener (this); //listen to key events
    theworld2 = getImage(getDocumentBase(),"theworld2.jpg");
    BOAT = getImage(getDocumentBase(),  "BOAT.jpg");
    life2 = getImage(getDocumentBase(), "life2.jpg");
    plane = getImage(getDocumentBase(), "plane.jpg");
    color1 = new Color(255,255, 255);
    play (getCodeBase(), "sinking.aiff");
    Start = new Button("Click me to start!");
    Start.addActionListener(this);
    add (Start);

    lifex = (int) (Math.random () *1400 );
    lifey = (int) (Math.random () *800 ) ;
    X = (int) (Math.random () *1400 );
    Y = (int) (Math.random () *800 ) ;
    X1 = (int) (Math.random () *1200 );
    Y1 = (int) (Math.random () *800 ) ;
    planeX = (int) (Math.random () *1200 );
    planeY = (int) (Math.random () *800 ) ;
    planeX1 = (int) (Math.random () *1200 );
    planeY1 = (int) (Math.random () *800 ) ;
    planeX2 = (int) (Math.random () *1200 );
    planeY2 = (int) (Math.random () *800 ) ;

    if (X >lifex && X <lifex +30 && Y> lifey && Y < lifey +30)
        play (getCodeBase(), "saved.aiff");
    if (X1 >lifex && X1 <lifex +30 && Y1> lifey && Y1 < lifey +30)
       play (getCodeBase(), "saved.aiff");

}

public void paint (Graphics g )
{
    if (b==1)
         g.drawString("IT WORKS" ,25 ,50 );
}

public void Display (Graphics g) {
    int width = theworld2.getWidth(this);
    int height = theworld2.getHeight(this);
    g.drawImage(theworld2, 0, 0, width, height ,this);
    g.drawImage(life2, lifex, lifey, width/300, height/300 , this);
    g.drawImage(BOAT,X,Y, width/30 , height/30, this) ;
    g.drawImage(BOAT,X1,Y1, width/30 , height/30, this) ;
    g.drawImage(life2 ,lifex, lifey , this);
    g.drawImage(plane ,planeX ,planeY ,  this);
    g.drawImage(plane ,planeX1 ,planeY1 ,  this);
    g.drawImage(plane ,planeX2 ,planeY2 ,  this);

    if (ch=='w') Y=Y-10;
    if (ch=='s') Y=Y+10;
    if (ch=='a') X=X-10;
    if (ch=='d') X=X+10;
    if (ch=='i') Y1=Y1-10;
    if (ch=='k') Y1=Y1 +10;
    if (ch=='j') X1=X1-10;
    if (ch=='l') X1=X1+10;

    if (ch=='w') planeX=planeX-10;
    if (ch=='w') planeY=planeY-10;
    if (ch=='s') planeX=planeX-10;
    if (ch=='s') planeY=planeY-10;
    if (ch=='d') planeX=planeX-10;
    if (ch=='d') planeY=planeY-10;
    if (ch=='a') planeX=planeX-10;
    if (ch=='a') planeY=planeY-10;

    if (ch=='w') planeX1=planeX1-10;
    if (ch=='w') planeY1=planeY1-10;
    if (ch=='s') planeX1=planeX1-10;
    if (ch=='s') planeY1=planeY1-10;
    if (ch=='d') planeX1=planeX1-10;
    if (ch=='d') planeY1=planeY1-10;
    if (ch=='a') planeX1=planeX1-10;
    if (ch=='a') planeY1=planeY1-10;

    if (ch=='w') planeX2=planeX2+10;
    if (ch=='w') planeY2=planeY2+10;
    if (ch=='s') planeX2=planeX2+10;
    if (ch=='s') planeY2=planeY2+10;
    if (ch=='d') planeX2=planeX2+10;
    if (ch=='d') planeY2=planeY2+10;
    if (ch=='a') planeX2=planeX2+10;
    if (ch=='a') planeY2=planeY2+10;
    g.setFont(new Font("vain", Font.BOLD, 26));
    g.setColor(new Color(255,0,0));
    if(X >lifex && X <lifex +30 && Y> lifey && Y < lifey +30)
    g.drawString(" player 1 wins, congrats " ,750 ,400);
    if(X1 >lifex && X1 <lifex +30 && Y1> lifey && Y1 < lifey +30)
    g.drawString(" player 2 wins,congrats " ,750 ,400);


}

public void keyPressed (KeyEvent e) {
    sr = e.getKeyText (e.getKeyCode()); //return a string describing the key
    ch = ' '; //a convenient value if a character key is not pressed.
    repaint();
}

public void keyTyped (KeyEvent e) {
    ch = e.getKeyChar(); //character that is pressed
    repaint();
}

public void keyReleased (KeyEvent e) {} //key released

public void actionPerformed(ActionEvent e)
{
 if (e.getSource() == Start){
     b=1;
    }

 repaint();
}`enter code here`
}`enter code here`
4

0 回答 0