0

好的,所以我想做乒乓球,我有基本代码,但我想添加一个选项来更改桨的颜色

    Main.Java: 
        import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Main extends JFrame {

    int x, y;

    //Double buffering
    Image dbImage;
    Graphics dbg;
    Image Ball;

    boolean GameStarted = false;
    boolean options = false;
    boolean p1colouroption = false;
    boolean p2colouroption = false;
    boolean p1red = false;
    boolean p2red = false;
    boolean p1white = true;
    boolean p2white = true;
    boolean p1yellow = false;
    boolean p2yellow = false;
    boolean p1green = false;
    boolean p2green = false;

    //Ball objects
    static Ball b = new Ball(193, 143);
    static Ball b1 = new Ball(193, 143);

    Thread ball = new Thread(b);
    Thread p1 = new Thread(b.p1);
    Thread p2 = new Thread(b.p2);




    //Menu Buttons
    Rectangle Options = new Rectangle (150, 200, 100, 25);
    Rectangle Player1colour = new Rectangle (150,150,100,25);
    Rectangle Player2colour = new Rectangle (150,100,100,25);
    Rectangle startButton = new Rectangle(150, 150, 100, 25);
    Rectangle HBstartButton = new Rectangle(368, 200, 50, 100);
    Rectangle P1red = new Rectangle (150,75,100,25);
    Rectangle P2red = new Rectangle (150,75,100,25);
    Rectangle P1white = new Rectangle (150,125,100,25);
    Rectangle P2white = new Rectangle (150,125,100,25);
    Rectangle P1yellow  = new Rectangle (150,175,100,25);
    Rectangle P2yellow = new Rectangle (150,175,100,25);
    Rectangle P1green = new Rectangle (150,225,100,25);
    Rectangle P2green = new Rectangle (150,225,100,25);
    //Variables for screen size
    int
    GWIDTH = 400,
    GHEIGHT = 300;
    //Dimension of GWIDTH*GHEIGHT
    Dimension screenSize = new Dimension(GWIDTH, GHEIGHT);
    int setY;

    //Create constructor to spawn window
    public Main(){
        ImageIcon i = new ImageIcon("F:/pong/src/ballpic.png");
        Ball = i.getImage();
        this.setTitle("Pong");
        this.setSize(screenSize);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBackground(Color.BLACK);
        this.addKeyListener(new KeyHandler());
        this.addMouseListener(new MouseHandler());
        this.addMouseMotionListener(new MouseHandler());
        this.setLocationRelativeTo(null);


        x= 370;
        y = 140;
    }

    public static void main(String[] args){
        Main m = new Main();
    }

    public void StartGame(){
        GameStarted = true;

        ball.start();
        p1.start();
        p2.start();

    }
    public void PauseGame(){
        GameStarted = false;
        options = false;
        p1colouroption = false;
        p2colouroption = false;
    }
    public void Buttonpressed(){
        options = false;
        p1colouroption = false;
        p2colouroption = false;
    }

    @Override
    public void paint(Graphics g){
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        draw(dbg);
        g.drawImage(dbImage, 0, 0, this);

    }
    public void draw(Graphics g){
        //Menu

        if(!GameStarted){
            if(p1colouroption){
                g.setColor(Color.RED);
                g.fillRect(P1red.x, P1red.y, P1red.width, P1red.height);
                g.setFont(new Font("Arial", Font.BOLD, 12));
                g.setColor(Color.BLACK);
                g.drawString("Red", P1red.x+10, P1red.y+15);
                g.setColor(Color.WHITE);
                g.fillRect(P1white.x,P1white.y,P1white.width,P1white.height);
                g.setColor(Color.BLACK);
                g.drawString("White",P1white.x+10, P1white.y+15);
                g.setColor(Color.YELLOW);
                g.fillRect(P1yellow.x,P1yellow.y,P1yellow.width,P1yellow.height);
                g.setColor(Color.BLACK);
                g.drawString("Yellow", P1yellow.x+10, P1yellow.y+15);
                g.setColor(Color.GREEN);
                g.fillRect(P1green.x, P1green.y, P1green.width, P1green.height);
                g.setColor(Color.BLACK);
                g.drawString("Green", P1green.x+10, P1green.y+15);
            }
            else{
              if(options){
            g.setColor(Color.white);
            g.fillRect(Player1colour.x, Player1colour.y, Player1colour.width, Player1colour.height);
            g.setFont(new Font("Arial", Font.BOLD, 12));
            g.setColor(Color.BLACK);
            g.drawString("Player 1 Colour", Player1colour.x+10, Player1colour.y+15);
            g.setColor(Color.WHITE);
            g.fillRect(Player2colour.x, Player2colour.y, Player2colour.width,          Player2colour.height);
            g.setColor(Color.BLACK);
            g.drawString("Player 2 Colour", Player2colour.x+10, Player2colour.y+15);
        }
              else{
        g.setFont(new Font("Arial", Font.BOLD, 26));
        g.setColor(Color.WHITE);
        g.drawString("Pong", 165, 75);
        g.setColor(Color.WHITE);
        g.fillRect(startButton.x, startButton.y, startButton.width, startButton.height);
        g.setFont(new Font("Arial", Font.BOLD, 12));
        g.setColor(Color.BLACK);
        g.drawString("Start Game", startButton.x+20, startButton.y+17);
        g.setColor(Color.WHITE);
        g.fillRect(Options.x, Options.y, Options.width, Options.height);
        g.setColor(Color.BLACK);
        g.drawString("Options", Options.x+17, Options.y+20);   

              }
              }
            }

        else{
        //Game drawings

        b.draw(g);
           if(p1red){
        g.setColor(Color.red);       
        b.p1.draw(g);
           }
           else{
               b.p1.draw(g);
           }

        b.p2.draw(g);



        //Scores
        g.setColor(Color.WHITE);
        g.drawString(""+b.p1Score, 125, 50);
        g.drawString(""+b.p2Score, 300, 50);

        }
                repaint();
    }




    ////////EVENT LISTENER CLASSES/////////
    public class KeyHandler extends KeyAdapter {
        int y;
        @Override
        public void keyPressed(KeyEvent e){
            b.p1.keyPressed(e);
            b.p2.keyPressed(e);

        if(e.getKeyCode() == e.VK_P)
           PauseGame();
        }






        public void keyReleased(KeyEvent e){
            b.p1.keyReleased(e);
            b.p2.keyReleased(e);



    }
    }
    ///////END EVENT LISTENER CLASSES/////


public class MouseHandler extends MouseAdapter {
        @Override
        public void mouseMoved(MouseEvent e){

        }
        @Override
        public void mousePressed(MouseEvent e){
             int mx = e.getX();
            int my = e.getY();         
            if(!options){
            if(mx > startButton.x && mx < startButton.x+startButton.width &&
               my > startButton.y && my < startButton.y+startButton.height){
            StartGame();
            }
            if(mx > Options.x && mx < Options.x+Options.width &&
                    my > Options.y && my < Options.y+Options.height){
                options = true;
            }
            }
            else{
            if(mx > Player1colour.x && mx < Player1colour.x+Player1colour.width &&
               my > Player1colour.y && my < Player1colour.y+Player1colour.height){
                p1colouroption = true;
            }    
            }
            if(p1colouroption){
            if(mx > P1red.x && mx < P1red.x+P1red.width &&
               my > P1red.y && my < P1red.y+P1red.height){
                p1red = true;
                p1green = false;
                p1yellow = false;
                p1white = false;
                Buttonpressed();
            }    
            }

            }
           }
            }

桨.java:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Paddle implements Runnable {

    int x, y, yDirection, id;
        boolean p1red = false;
    boolean p2red = false;
    boolean p1white = true;
    boolean p2white = true;
    boolean p1yellow = false;
    boolean p2yellow = false;
    boolean p1green = false;
    boolean p2green = false;

    Rectangle paddle;

    public Paddle(int x, int y, int id){
        this.x = x;
        this.y = y;
        this.id = id;
        paddle = new Rectangle(x, y, 10, 50);
    }

    public void keyPressed(KeyEvent e){
        switch(id){
            default:
                System.out.println("Please enter a valid ID in Paddle constructor");
                break;
            case 1:
                if(e.getKeyCode() == e.VK_W){
                    setYDirection(-1);
                }
                if(e.getKeyCode() == e.VK_S){
                    setYDirection(1);
                }
                break;
            case 2:
                if(e.getKeyCode() == e.VK_UP){
                    setYDirection(-1);
                }
                if(e.getKeyCode() == e.VK_DOWN){
                    setYDirection(+1);
                }
                break;            
        }
    }
    public void keyReleased(KeyEvent e){
        switch(id){
            default:
                System.out.println("Please enter a valid ID in Paddle constructor");
                break;
            case 1:
                if(e.getKeyCode() == e.VK_W){
                    setYDirection(0);
                }
                if(e.getKeyCode() == e.VK_S){
                    setYDirection(0);
                }
                break;
            case 2:
                if(e.getKeyCode() == e.VK_UP){
                    setYDirection(0);
                }
                if(e.getKeyCode() == e.VK_DOWN){
                    setYDirection(0);
                }
                break;            
        }
    }

    public void setYDirection(int ydir){
        yDirection = ydir;
    }

    public void move(){
        paddle.y += yDirection;
        if(paddle.y <= 15)
            paddle.y = 225;
        if(paddle.y >= 250)
            paddle.y = 25;
    }

    public void draw(Graphics g){
        switch(id){
            default:
                System.out.println("Please enter a valid ID in Paddle constructor");
                break;
            case 1:
                if(p1white){
                g.setColor(Color.white);
                }
                if(p1green){
                    g.setColor(Color.green);
                }
                if(p1red){
                    g.setColor(Color.red);
                }
                if(p1yellow){
                    g.setColor(Color.yellow);
                }
                g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
                break;
            case 2:
                if(p2white){
                g.setColor(Color.white);
                }
                if(p2green){
                    g.setColor(Color.green);
                }
                if(p2red){
                    g.setColor(Color.red);
                }
                if(p2yellow){
                    g.setColor(Color.yellow);
                }
                g.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
                break;
        }
    }

    @Override
    public void run() {
        try{
            while(true){
                move();
                Thread.sleep(8);
            }
        }catch(Exception e){System.err.println(e.getMessage());}
    }

}

所以无论如何我可以得到 main.java 中的布尔值来触发 paddle.java 中的布尔值

4

1 回答 1

0

公开布尔值,然后简单地访问它。例如:

public class Example{
    public boolean test;
    public Example(){
        test = false;
    }
}
public class Main{
    public static void main(){
            Example = new Example();
            example.test = true;
    }
}
于 2013-05-09T15:54:07.553 回答