2013-03-12 27 views
0

我想在Swing中實現俄羅斯方塊,因爲我試圖繪製每個矩形相鄰的矩形,但第二個矩形不會與第一個矩形相鄰。如何在Swing中繪製相鄰的矩形?

此外,如果getPreferredSize()返回小於50,50的任何值,則屏幕上不顯示任何內容。 這段代碼有什麼問題,以及如何繪製相鄰的矩形。

public class Tetris extends JFrame 
{ 

    public Tetris(String string) 
    { 
     super(string); 
    } 
    public static void main(String[] args) 
    { 
     Tetris tetris = new Tetris("Tetris"); 
     tetris.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel mainPanel = new JPanel(); 
     tetris.getContentPane().add(mainPanel); 
     tetris.setVisible(true); 
     tetris.setLocationRelativeTo(null); 
     tetris.pack(); 
     tetris.setSize(500, 500); 
     tetris.setResizable(false); 
     tetris.paintBoard(mainPanel); 
    } 

    public void paintBoard(JPanel mainPanel) 
    { 
     Piece p1 = new Piece(10,10,50,50,Color.GREEN); 
     Piece p2 = new Piece(60,10,50,50,Color.RED); 
     mainPanel.add(p1); 
     mainPanel.add(p2); 
    } 
} 

public class Piece extends JComponent 
{ 
    private int X = 0; 
    private int Y = 0; 
    private int H = 0; 
    private int W = 0; 
    private Color c; 

    public Piece(int X, int Y, int W, int H, Color c) 
    { 
     this.X = X; 
     this.Y = Y; 
     this.W = W; 
     this.H = H; 
     this.c = c; 
    } 

    @Override 
    public void paintComponent(Graphics g) 
    { 
     g.setColor(c); 
     g.drawRect(X, Y, W, H); 
     g.fillRect(X, Y, W, H); 
     System.out.println("g.drawRect("+X+", "+Y+", "+W+", "+H+")"); 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(50, 50); 
    } 
} 

回答

2

如果的getPreferredSize()返回任何小於50,50則沒有在屏幕

顯示出來。這是因爲你已經設置了一個60 x座標爲「紅「Piece,但組件的首選大小寬度爲50,因此該組件從屏幕上拉出。

要添加組件,您可以簡單地在0, 0處繪製所有Piece組件,並讓佈局管理器負責定位。除此之外,鑑於這是一個俄羅斯方塊的遊戲,您應該考慮在單個組件上繪製所有組件,並使用Swing Timer來操作這些組件。

0

如果您希望添加的Pieces顯示在您指定的座標上,則需要將JPanel的LayoutManager設置爲null。

public void paintBoard(JPanel mainPanel) 
{ 
    Piece p1 = new Piece(10,10,50,50,Color.GREEN); 
    Piece p2 = new Piece(60,10,50,50,Color.RED); 
    mainPanel.setLayout(null) 
    mainPanel.add(p1); 
    mainPanel.add(p2); 
} 
+0

無論什麼問題,不使用LayoutManager是_解決方案 – kleopatra 2013-03-12 11:30:56

1
  • 覆蓋getPreferredSizeJPanel,否則JPanel返回零Dimension

  • 把所有Objects到陣列

  • paintComponent僅環陣

  • 使用Swing Timer(只有一個實例),用於動畫

  • example about all a.m. points

+1

+1爲Swing Timer – Reimeus 2013-03-12 11:14:24

1

總的問題是一個Swing佈局問題。你的mainPanel JPanel有一個默認的佈局,FlowLayout,試圖安排你添加到它的每個東西(Piece)。每件作品的paintComponent將僅控制自己的繪畫,而不是整個mainPanel。所以你的小塊沒有被繪製相對於整個mainPanel。

類似:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.util.ArrayList; 
import java.util.List; 

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

public class Tetris extends JFrame 
{ 

    public Tetris(String string) 
    { 
     super(string); 
    } 
    public static void main(String[] args) 
    { 
     Tetris tetris = new Tetris("Tetris"); 
     tetris.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel mainPanel = new JPanel(); 
     mainPanel.setLayout(new BorderLayout()); 
     tetris.getContentPane().setLayout(new BorderLayout()); 
     tetris.getContentPane().add(mainPanel, BorderLayout.CENTER); 
     tetris.setLocationRelativeTo(null); 
     tetris.pack(); 
     tetris.setSize(500, 500); 
     tetris.setResizable(false); 
     tetris.setVisible(true); 
     tetris.paintBoard(mainPanel); 
    } 

    public void paintBoard(JPanel mainPanel) 
    { 
     Piece p1 = new Piece(10,10,50,50,Color.GREEN); 
     Piece p2 = new Piece(60,10,50,50,Color.RED); 

     Board board = new Board(); 

     board.addPiece(p1); 
     board.addPiece(p2); 

     mainPanel.add(board, BorderLayout.CENTER); 
    } 

    private class Board extends JComponent 
    { 
     private List<Piece> pieces = new ArrayList<Piece>(); 

     public void addPiece(Piece piece) 
     { 
      pieces.add(piece); 
     } 

     @Override 
     public void paintComponent(Graphics g) 
     { 
      for(Piece piece : pieces) 
      { 
       g.setColor(piece.c); 
       g.drawRect(piece.X, piece.Y, piece.W, piece.H); 
       g.fillRect(piece.X, piece.Y, piece.W, piece.H); 
      } 
     } 
    } 

    private class Piece 
    { 
     private int X = 0; 
     private int Y = 0; 
     private int H = 0; 
     private int W = 0; 
     private Color c; 

     public Piece(int X, int Y, int W, int H, Color c) 
     { 
      this.X = X; 
      this.Y = Y; 
      this.W = W; 
      this.H = H; 
      this.c = c; 
     } 
    } 
} 
1

這裏有一個邏輯上的錯誤。您正在使用paintComponent()方法繪製Piece,但您要給出的起始x和y座標在mainPanel(10,10或60,10)的上下文中是可縮放的,而您需要在當前組件Piece的上下文中給出它們,例如試試這個:

@Override 
public void paintComponent(Graphics g) 
{ 
    super.paintComponent(g); 
    g.setColor(c); 
    g.drawRect(0, 0, W, H); 
    g.fillRect(0, 0, W, H); 
    System.out.println("g.drawRect("+X+", "+Y+", "+W+", "+H+")"); 
} 

它正確地顯示你想要的。

它也解釋了爲什麼較小的大小不起作用,在當前組件的情況下不是父組件。