2013-07-09 79 views
2

我有以下代碼...(加配了getX,的getY,setX的,塞蒂,渲染(),勾選()等通用播放器類不包括在內)基於播放器X,Y切換jpanels?

我想球員ViewB加載和時它碰到了邊緣......我希望ViewB關閉並由ViewA或ViewC取代(取決於它的左邊緣是左邊緣還是右邊緣)......我似乎無法完成此操作, t知道我在做什麼錯誤.... 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的視圖:(帶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); 
     } 
    } 
} 

回答

5
  • 使用CardLayout用於切換中間人次(ViewA,ViewB, ViewC,ViewD),否則必須調用(re)validaterepaint作爲JFrame(作爲最後的代碼行,對已經可見的Swing GUI進行全部更改執行)

  • 風俗畫是由paintComponent()代替paint()

  • 進行刪除無效public void addNotify() {和代替這個空白覆蓋getPreferredSize,那麼所有在JPanelObjects座標基於getWeight/Height

+1

「CardLayout」的+1,我正要寫這個。 – Dahaka

+0

「remove void public void addNotify(){而不是此void來覆蓋getPreferredSize,那麼JPanel中的對象的所有座標都基於getWeight/Height」 - 因此完全刪除addNotify()?或者這樣做'public void addNotify(){super_addNotify();} B_WIDTH = getWidth(); B_HEIGHT = getHeight(); super.setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT)); }' – user2556304

+0

刪除整個代碼塊,沒有任何理由通知,請嘗試通過鼠標點擊鏈接代碼getPreferredSize,然後你可以比較,順便在這裏搜索paintComponent + getPreferredSize,按最新順序,然後你可以看到一些奇蹟 – mKorbel