Click here to Skip to main content
15,891,903 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Java
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(){//main 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));//set a window inside our main frame
		
		Screen screen = new Screen();	
	        add(screen);  //******update*******
		setVisible(true);
	}

	public static void main(String[] args){
		Frame frame = new Frame();
	}

}


sub's:
Java
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);//drawing the room
	}
	
	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 );
	}
}
Posted
Updated 8-Aug-14 1:59am
v2
Comments
Maciej Los 8-Aug-14 5:40am    
Any specific problem?
idobry 8-Aug-14 5:46am    
i dont get any error, the problem is that when i run the code nothing showing up,just blank screen.
at this point it should show a board.
Richard MacCutchan 8-Aug-14 6:07am    
You need to use your debugger to step through the code to see why it is not creating or displaying the information.
idobry 8-Aug-14 8:01am    
ok i've found only one mistake, i added "add(screen);", but still when i run the code i get blank window
Richard MacCutchan 8-Aug-14 8:11am    
That hardly counts as debugging. You need to do the hard work and find out what is happening or not happening throughout the program. People here will try to help you to fix problems but you need to give us the clues to work with.

1 solution

Shame on me, the problem was in Screen Class, insted "public void paintComponenr(Graphics g)"
it should have "public void paintComponent(Graphics g)"

sorry for all the trouble
 
Share this answer
 
Comments
Richard MacCutchan 8-Aug-14 9:38am    
I just discovered that also.
idobry 8-Aug-14 9:59am    
thanks any way!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900