2

我有以下代码...(加上一个通用的 Player 类,不包括 getX、getY、setX、setY、render()、tick() 等)

我希望播放器在 ViewB 中加载,当它到达边缘时。我希望 ViewB 关闭并被 ViewA 或 ViewC 替换(取决于它的左边缘命中还是右边缘)......我不能似乎完成了这项工作,但我不知道自己做错了什么...... Player 类是在 MainGameThread.java 中创建的......并被传递到新的 JPanel 上,这样我仍然可以使用原始的 Player 对象/精灵已创建..该
System.out.println("Should change screens here to ViewA");命令在到达边缘时似乎没有触发..我在这里做错了什么????我要修什么课?

MainGameThread 类:

import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainGameThread extends JFrame {

    private LinkedList<JPanel> screens = new LinkedList<JPanel>();
    private ViewA va;
    private ViewB vb;
    private ViewC vc;
    private ViewD vd;
    private Player player;

    public MainGameThread() {
        player = new Player();
        // load player settings from server
        //  ..
        //
        //  va = new ViewA(player);
        //vc = new ViewC(player);
        //vd = new ViewD(Player)

        vb = new ViewB(player);

        screens.add(0,ViewB);
        screens.add(1,ViewA);
        screens.add(2,ViewB);
        screens.add(3,ViewC);

        add(screens.get(0)); // starting from view B

        //go to end of view B (x=0), change to View A, close view B

        if(player.getX()<=5){
            System.out.println("Should change screens here to ViewA");
            vb = null;
            va = new ViewA(player);
            remove(screens.get(0)); 

            add(screens.get(1));
        } 

        if(player.getX()>=615){
            System.out.println("Should change screens here to ViewC");
            vb = null;
            vc = new ViewC(player);
            remove(screens.get(0)); 

            add(screens.get(2));
        } 
        setSize(652, 480);
        setLocationRelativeTo(null);
        setTitle("GAME");
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MainGameThread();
    }
}

JPanel View:(用 ViewB 等替换 ViewA 到 D)

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.Timer;


public class ViewA extends JPanel implements ActionListener {

    private Player player;
    private Timer timer;
    private int B_WIDTH;
    private int B_HEIGHT;
    private boolean ingame;

    public ViewA(Player player) {   
        this.player = player;

        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.RED);
        setDoubleBuffered(true);
        setSize(652, 480);
        ingame = true;


        timer = new Timer(5, this);
        timer.start();
    }

    public void addNotify() {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();   

    }

    public void paint(Graphics g) {
        super.paint(g);

        if (ingame) {

            Graphics2D g2d = (Graphics2D)g;

            if (player.isVisible())
                g2d.drawImage(player.getImage()
                    , player.getX(), player.getY(), this);

            g2d.setColor(Color.BLACK);
            g2d.drawString("ViewA: ", 5, 15);           
        } 

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        player.move();
        checkCollisions();
        repaint();  
    }

    public void checkCollisions() {
    }

    private class TAdapter extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            player.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            player.keyPressed(e);
        }
    }
}
4

1 回答 1

5
  • 用于CardLayout在视图(ViewA、ViewB、ViewC、ViewD)之间切换,否则您必须调用(re)validaterepaintfor JFrame(作为最后的代码行,在对已经可见的 Swing GUI 执行所有更改之后)

  • 自定义绘画由paintComponent()而不是paint()

  • 删除 voidpublic void addNotify() {而不是这个 void 来覆盖,然后in的getPreferredSize所有坐标都基于ObjectsJPanelgetWeight/Height

于 2013-07-09T06:35:30.023 回答