2014-01-24 42 views
1

我想做一個使用java的俄羅斯方塊遊戲,但我目前只是試圖讓我的遊戲網格上出現一個形狀...我看了看我的代碼,找不到任何錯誤或錯誤,這意味着這將無法正常工作?我的JPanel類不會在我的JFrame上畫一個矩形?

這是我的主類叫做Tetris6。

package tetris6; 


import java.awt.Graphics; 
import javax.swing.*; 


public class Tetris6 { 

    public static final int WIDTH = 400, HEIGHT = 825; 


    public static void main(String[] args) { 

    GamePanel gamePanel = new GamePanel(); 

    JFrame frame = new JFrame(); 
    frame.setLayout(null); 
    frame.setResizable(false); 
    frame.setSize(WIDTH,HEIGHT); 
    frame.setTitle("Tetris"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(MenuBar.MenuBar()); 
    frame.add(BoardGrid.GameGrid()); 
    frame.add(gamePanel); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 


    } 

} 

這是我的課,它使用JPanels創建網格。

public class BoardGrid {  


    public static JPanel GameGrid() { 


     JPanel Grid = new JPanel (new GridLayout(20,10)); 

     for (int i=0; i < 200; i++){ 

     Grid.add(createSquareJPanel(Color.white,30)); 


     } 

     Grid.setVisible(true); 
     Grid.setBorder(BorderFactory.createLineBorder(Color.white)); 
     Grid.setBounds(0,50,400,625); 

     return Grid; 

     } 

    private static JPanel createSquareJPanel(Color color, int size) 
    { 
     JPanel tempPanel = new JPanel(); 
     tempPanel.setBackground(color); 
     tempPanel.setMinimumSize(new Dimension(size, size)); 
     tempPanel.setMaximumSize(new Dimension(size, size)); 
     tempPanel.setPreferredSize(new Dimension(size, size)); 
     tempPanel.setBorder(BorderFactory.createLineBorder(Color.blue)); 
     return tempPanel; 
    } 

} 

這是I類創建菜單欄。

package tetris6; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import static tetris6.Tetris6.WIDTH; 

/** 
* 
*/ 
public class MenuBar { 

    public static JMenuBar MenuBar() { 



    JMenuBar bar = new JMenuBar(); 
    bar.setBounds(0,0, WIDTH, 25); 

    JMenu file = new JMenu("File"); 
    file.setBounds(0,0,45,24); 

    JMenuItem newGame = new JMenuItem("New Game"); 
    newGame.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent e){ 
      //code for new game 
      System.out.println("Starting new game..."); 
     } 
    }); 

    JMenuItem highScore = new JMenuItem("High Score"); 
    highScore.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent f){ 
      int highscore = 0; //replace this with getHighscoreMethod later 
      final JFrame alert = new JFrame("HighScore"); 
      alert.setSize(200,200); 
      alert.setLayout(null); 
      alert.setLocationRelativeTo(null); 

      JLabel score = new JLabel ("The highscore is " + highscore); 
      score.setBounds(35,0,200,50); 

      JButton okayButton = new JButton("Okay"); 
      okayButton.setBounds(50,120,100,30); 
      okayButton.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       alert.dispose(); 
      } 
      }); 
      alert.add(score); 
      alert.add(okayButton); 
      alert.setResizable(false); 
      alert.setVisible(true); 
     } 
    }); 

    JMenuItem exit = new JMenuItem("Exit"); 
    exit.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent g){ 
      System.out.println("Closing..."); 
      System.exit(0); 
     } 
    }); 


    file.add(newGame); 
    file.add(highScore); 
    file.add(exit); 
    bar.add(file); 

    return bar; 

} 

} 

這是我在創造我的俄羅斯方塊形狀的類。

package tetris6; 

import java.awt.Color; 
import java.awt.Graphics2D; 

public class Shape { 

    public static void main() { 


    } 

    //Creating the tetris shapes and the rotated shapes  

public static Graphics2D O(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.yellow); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D T_up(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.magenta); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(60,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D T_right(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.magenta); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(0,60,30,30); 
    tetrisShape.fillRect(30,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D T_left(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.magenta); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(30,60,30,30); 

    return tetrisShape; 
} 

public static Graphics2D T_down(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.magenta); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(60,0,30,30); 
    tetrisShape.fillRect(30,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D I_vertical(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.cyan); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(0,60,30,30); 
    tetrisShape.fillRect(0,90,30,30); 

    return tetrisShape; 
} 

public static Graphics2D I_horizontal(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.cyan); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(60,0,30,30); 
    tetrisShape.fillRect(90,0,30,30); 

    return tetrisShape; 
} 

public static Graphics2D S_horizontal(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.red); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(60,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D S_vertical(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.red); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(30,60,30,30); 

    return tetrisShape; 
} 

public static Graphics2D Z_horizontal(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.green); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(60,30,30,30); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 

    return tetrisShape; 
} 

public static Graphics2D Z_vertical(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.green); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(0,60,30,30); 

    return tetrisShape; 
} 

public static Graphics2D J_vertical_left(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.blue); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(30,60,30,30); 
    tetrisShape.fillRect(0,60,30,30); 

    return tetrisShape; 
} 

public static Graphics2D J_vertical_right(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.blue); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(0,60,30,30); 
    tetrisShape.fillRect(30,0,30,30); 

    return tetrisShape; 
} 

public static Graphics2D J_horizontal_up(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.blue); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(60,30,30,30); 
    tetrisShape.fillRect(0,0,30,30); 

    return tetrisShape; 
} 

public static Graphics2D J_horizontal_down(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.blue); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(60,0,30,30); 
    tetrisShape.fillRect(60,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D L_vertical_right(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.orange); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(0,60,30,30); 
    tetrisShape.fillRect(30,60,30,30); 

    return tetrisShape; 
} 

public static Graphics2D L_vertical_left(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.orange); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(30,60,30,30); 

    return tetrisShape; 
} 

public static Graphics2D L_horizontal_up(Graphics2D tetrisShape) 
{ 
    tetrisShape.setColor(Color.orange); 
    tetrisShape.fillRect(60,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 
    tetrisShape.fillRect(30,30,30,30); 
    tetrisShape.fillRect(60,30,30,30); 

    return tetrisShape; 
} 

public static Graphics2D L_horizontal_down(Graphics2D tetrisShape) 
{ 

    tetrisShape.setColor(Color.orange); 
    tetrisShape.fillRect(0,0,30,30); 
    tetrisShape.fillRect(30,0,30,30); 
    tetrisShape.fillRect(60,0,30,30); 
    tetrisShape.fillRect(0,30,30,30); 

    return tetrisShape; 
} 

//finished creating the tetrominoes 

} 

這是我想的類將矩形繪製到JPanel中。

package tetris6; 

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JPanel; 

/** 
* 
*/ 
public class GamePanel extends JPanel { 

    public void paintComponent (Graphics g) { 

     super.paintComponent(g); 

     //set the panel to overlap the game grid 
     setBounds(0,50,400,625); 
     //draw the tetris shape at the top middle of the grid 
     g.setColor(Color.yellow); 
     g.fillRect(160, 0, 80, 80); 

     setVisible(true); 



    } 

} 

回答

0

如果您沒有合適的理由,請避免使用setLayout(null)。刪除下面的代碼。矩形將顯示。

如果仍然必須使用空佈局,則必須設置組件的寬度和高度以及其x和y位置。在你的tetris6類中刪除setLayout(null);

JFrame frame = new JFrame(); 
    //frame.setLayout(null); // remove this line. 
+1

可能是很好的答案,通過加覆蓋的getPreferredSize的的GamePanel和JFrame.pack(),而不是任何大小的的setSize /隨時隨地的setBounds, – mKorbel

+0

@Rahul現在我已經取出來這行代碼,它已解決關於矩形不被繪製的問題。但是,現在我已經意識到在矩形的JPanel之前添加的Grid JPanel正在遮蓋黃色的矩形......關於如何解決這個問題的任何想法? – user3232215

相關問題