Hey guys,
as i mentioned in the title i'm trying to make a simple game from a public guide.
i dont know why but all i can get is a blank grey screen.
hope to get some guidance from you!
here is my code:
main
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
public class Frame extends JFrame{
public static String title = "Tower Defence ";
public static Dimension size = new Dimension(700,550);
public Frame(){
setTitle(title);
setSize(size);
setResizable(false);
setBackground(null);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init(){
setLayout(new GridLayout(1, 1, 0, 0));
Screen screen = new Screen();
add(screen);
setVisible(true);
}
public static void main(String[] args){
Frame frame = new Frame();
}
}
sub's:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
public class Screen extends JPanel implements Runnable {
public Thread t1 = new Thread(this);
public static boolean isFirst = true;
public static int myWitdh,myHeight;
public static Room room;
public Screen(){
t1.start();
}
public void paintComponenr(Graphics g){
if(isFirst){
define();
isFirst = false;
}
g.clearRect(0, 0, getWidth(), getHeight());
room.draw(g);
}
private void define() {
room = new Room();
}
public void run(){
while(true){
if(!isFirst){
room.physic();
}
repaint();
try {
Thread.sleep(1);
} catch (InterruptedException e) {}
}
}
}
import java.awt.Graphics;
public class Room {
public int worldWidth = 10;
public int worldheight = 6;
public int blockSize = 32;
public Block[][]block;
public Room(){
define();
}
private void define() {
block = new Block[worldWidth][worldheight];
for( int y=0 ; y<block.length ; y++ ){
for( int x=0 ; x<block[0].length ; x++ ){
block[y][x] = new Block( x*blockSize, y*blockSize, blockSize, blockSize, 0, 0);
}
}
}
public void physic(){
}
public void draw(Graphics g){
for( int y=0 ; y<block.length ; y++ ){
for( int x=0 ; x<block[0].length ; x++ ){
block[y][x].draw(g);
}
}
}
}
import java.awt.Graphics;
import java.awt.Rectangle;
public class Block extends Rectangle {
public int groundID;
public int airID;
public Block(int x, int y, int w, int h,int ground ,int air){
setBounds(x,y,w,h);
this.groundID = ground;
this.airID = air;
}
public void draw(Graphics g){
g.drawRect(x, y, width,height );
}
}