根据玩家X,Y切换jpanels?

时间:2013-07-09 06:29:11

标签: java swing jframe jpanel

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

我希望玩家在ViewB中加载并且当它到达边缘时...我希望ViewB关闭并被ViewA或ViewC取代(取决于它是左边缘还是右边缘)......我似乎无法完成这一切,我不知道我做错了什么.... Player类是在MainGameThread.java中创建的...并传递给新的JPanel,以便我仍然使用原始的Player对象创建的/ sprite .. 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);
        }
    }
}

1 个答案:

答案 0 :(得分:5)

  • 使用CardLayout切换视图(ViewA,ViewB,ViewC,ViewD),否则您必须为(re)validate调用repaintJFrame(如在执行了对已经可见的Swing GUI的所有更改之后的最后一行代码行

  • 自定义绘画由paintComponent()代替paint()

  • 完成
  • 删除无效public void addNotify() {而不是此空格来覆盖getPreferredSizeObjectsJPanel的所有坐标都基于getWeight/Height < / p>