Results 1 to 2 of 2
  1. #1
    lawlawlaws is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default problem about Unknown Black Screen

    There is no exception run in netbean below and only show black screen. I need you. Thank you


    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.net.*;
    import java.lang.System;

    public class XO extends JFrame implements Runnable,KeyListener{
    private Image img;
    public BufferedImage backbuffer;
    public Graphics2D g2d;
    Thread t;

    //definition
    final int STATE_NOTHING = 0;
    final int STATE_CROSS = 1;
    final int STATE_CIRCLE = 2;
    int[] metric;
    public XO(){

    metric = new int[9];
    setSize(600,600);
    setVisible(true);
    backbuffer = new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
    g2d = backbuffer.createGraphics();
    addKeyListener(this);
    init();
    gameStartup();
    Thread t = new Thread(this);
    }

    public void run(){
    while(t==Thread.currentThread()){
    try{
    Thread.sleep(1000/30);
    }catch(InterruptedException e){e.printStackTrace();}
    update();
    repaint();
    }
    }
    public void update(){
    //g2d.drawString("fassssssssssssss",50,50);
    g2d.drawImage(img, 50,50,this);
    }
    public void init(){
    gameStartup();
    }
    public void paint(Graphics g){
    g.drawImage(backbuffer,0,0,this);
    }
    private URL getURL(String fname){
    URL url = null;
    try{
    url = this.getClass().getResource(fname);
    }catch(Exception e){}
    return url;
    }
    public void load(Image img,String fname){
    Toolkit tk = Toolkit.getDefaultToolkit();
    img = tk.getImage(getURL(fname));
    }
    public void gameStartup(){
    load(img,"cross.png");

    }
    public void keyTyped(KeyEvent k){}
    public void keyPressed(KeyEvent k){
    }
    public void keyReleased(KeyEvent k){
    }
    public static void main(String[] argv){
    new XO();
    }

    }

  2. #2
    zirbinator is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default Re: problem about Unknown Black Screen

    There are a couple things that I found about your code. Here they are:

    First, in the constructor you initialize a Thread t like this:
    Java Code:
    Thread t = new Thread();
    , which is fine except that you're not initializing the Thread that you use later in the program. All you need to do to fix this is make the statement
    Java Code:
    t = new Thread();
    . This initializes the Thread you declared before, instead of a random new Thread that's also called t but never used.

    I didn't look at your code for loading/drawing an image, but when I added code to the paint() method to draw some random stuff, it worked. I don't see why you would want a BufferedImage that you draw stuff to, only to draw the BufferedImage back onto the screen. You might as well put the code for drawing the image that you load from file in the paint() method. For example, with my changes, your program became:

    Java Code:
    package forums;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.net.*;
    
    public class XO extends JFrame implements Runnable,KeyListener
    {
    	private static final long serialVersionUID = 1L;
    	private Image img;
    	public BufferedImage backbuffer;
    	public Graphics2D g2d;
    	Thread t;
    	
    	final int STATE_NOTHING = 0;
    	final int STATE_CROSS = 1;
    	final int STATE_CIRCLE = 2;
    	int[] metric;
    	
    	public XO()
    	{
    		metric = new int[9];
    		setSize(600,600);
    		setVisible(true);
    		backbuffer = new BufferedImage(600,600,BufferedImage.TYPE_INT_RGB);
    		g2d = backbuffer.createGraphics();
    		addKeyListener(this);
    		init();
    		gameStartup();
    		t = new Thread(this);
    	}
    	
    	public void run()
    	{
    		while(t==Thread.currentThread())
    		{
    			try
    			{
    				Thread.sleep(1000/30);
    			}
    			catch(InterruptedException e) { e.printStackTrace(); }
    			update();
    			repaint();
    		}
    	}
    	
    	public void update()
    	{
    		//g2d.drawString("fassssssssssssss",50,50);
    		g2d.drawImage(img, 50,50,this);
    	}
    	
    	public void init()
    	{
    		gameStartup();
    	}
    	
    	public void paint(Graphics g)
    	{
                    g.drawImage(img, 50,50,this);
    		g.setColor(Color.white);
    		g.fillRect(0, 50, 10, 10);
    	}
    	
    	private URL getURL(String fname) {
    		URL url = null;
    		try
    		{
    			url = this.getClass().getResource(fname);
    		}
    		catch(Exception e){}
    		return url;
    	}
    	
    	public void load(Image img, String fname)
    	{
    		Toolkit tk = Toolkit.getDefaultToolkit();
    		img = tk.getImage(getURL(fname));
    	}
    	
    	public void gameStartup()
    	{
    		load(img,"cross.png");
    	}
    	
    	public void keyTyped(KeyEvent k){ }
    	
    	public void keyPressed(KeyEvent k){ }
    	
    	public void keyReleased(KeyEvent k){ }
    	
    	public static void main(String[] argv){
    		new XO();
    	}
    
    }
    With this, all you need is the paint() method and the update() method is useless. If you want to use a BufferedImage for some other reason (like drawing it with transparency), that will work as well. In any case, this code gave my a window with a little white box, so I suppose it's not entirely black anymore :)

Similar Threads

  1. Problem about Black Screen
    By lawlawlaw in forum New To Java
    Replies: 2
    Last Post: 01-26-2012, 05:18 PM
  2. Problem with splash screen
    By frintocf in forum AWT / Swing
    Replies: 1
    Last Post: 09-07-2011, 12:42 AM
  3. Replies: 0
    Last Post: 04-02-2011, 11:22 AM
  4. Unknown problem
    By sanchir0805 in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 04:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •