I wrote code for the window of the specific game. And, for the some reason, my board cannot be shown on the window, unless I make the window resizable. And every time I run my programm, I have to resize my window a little bit for my board to be shown. Hope you understood and hope you can help me.
CODE:
import javax.swing.JFrame;
public class Window
{
public static final int WIDTH = 307, HEIGHT = 637;
private JFrame window;
private Board board;
public Window()
{
window = new JFrame("TETRIS");
window.setSize(WIDTH, HEIGHT);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
window.setLocationRelativeTo(null);
window.setVisible(true);
board = new Board();
window.add(board);
window.addKeyListener(board);
}
public static void main(String[] args)
{
new Window();
}
}
I was not sure if I need to write code of the board here, so I just wrote the first lines.
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.event.ActionListener;
public class Board extends JPanel implements KeyListener
{
private static final long serialVersionUID = 1L;
private BufferedImage blocks;
private Shape[] shapes = new Shape[7];
private Shape currentShape;
private Timer timer;
private final int blockSize = 30;
private final int boardWidth = 10, boardHeight = 20;
private int [][] board = new int [boardHeight][boardWidth];
private final int FBS = 60;
private final int delay = 1000/60;
private boolean gameOver = false;
...
Thanks in advance!
Java has a thread responsible for view events so you should there change the view properties:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Window();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.