Adding image to game

Why it doesnt run, but compiles..

Page 1 of 1

6 Replies - 613 Views - Last Post: 11 December 2008 - 04:23 AM Rate Topic: -----

#1 gawdlike  Icon User is offline

  • you can't fix stupid.

Reputation: -7
  • View blog
  • Posts: 647
  • Joined: 16-November 08

Adding image to game

Post icon  Posted 10 December 2008 - 02:54 PM

Ok, so heres the problem, i can compile the code and run it fine, but when i add the Bricks method to be able to put an image as the board, it compiles but i get an error when running the html.

//-------------
//import
//-------------
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
//-------------------------------------------------------------------------------
public class Bricks extends Applet implements Runnable, KeyListener{
	
//-------------------------------------------------------------------------------
// deklarationer
//-------------------------------------------------------------------------------	

	Rectangle[][] target = new Rectangle[5][10];
	boolean[][] flagga = new boolean[5][10];
	int targetBredd = 80;
	int targetHojd = 20;
	int width = 500;
	int height = 390;
	int spelareHojd = 20;
	int spelareBredd = 80;
	int radie = 7;
	int raknare = 0;
	double sidor = 0;
	double uppeNere = 0;
	double	spelareX = width/2-40;
	double	spelareY = 370;
	double hastBollX = 2;
	double hastBollY = 2;
	double bollY = 300;
	double bollX = 150;
	double spelareHast = 3;
	boolean leftDown=false;
	boolean rightDown=false;
	Image minnesbild;
	Graphics rityta;
	Rectangle spelare = new Rectangle();
	Graphics g;
	URL url1 = null;
	Image img1 = null;
	
	
//-----------------------------------------------------------------
//init
//-----------------------------------------------------------------		

	public void init(){
		
		addKeyListener(this);
		requestFocus();
		minnesbild = createImage(width,height);
		rityta = minnesbild.getGraphics();
		
		}//end init	
		
//-------------------------------------------------------------------------------
// tråden start
//-------------------------------------------------------------------------------    

	public void start(){
		
		Thread ny = new Thread(this);
		ny.start();
			
	}												//end start    
		
//-------------------------------------------------------------------------------
// skapa spelplanen forsta gangen
//-------------------------------------------------------------------------------
	
	public void paint(Graphics g) {
		    	    	
		for(int i=0; i<5; i=i+1){
			for(int j=0; j<10; j=j+1){
					g.setColor(Color.blue);
					target[i][j] = new Rectangle(); 
					target[i][j].setLocation( 40 + i*(1 + targetBredd), j * (1 +targetHojd)); 
	    			target[i][j].setSize( targetBredd, targetHojd );
	    			g.fillRect(40 + i*(targetBredd+1) , j * (targetHojd+1) , targetBredd, targetHojd);
	    			if (flagga[i][j]){
	    				g.setColor(Color.white);
						target[i][j] = new Rectangle(); 
						target[i][j].setLocation( 40 + i*(1 + targetBredd), j * (1 +targetHojd)); 
	    				target[i][j].setSize( targetBredd, targetHojd );
	    				g.fillRect(40 + i*(targetBredd+1) , j * (targetHojd+1) , targetBredd, targetHojd);
    				}								//end if
   			}										//end for j
   		}											//end for i	
   		
   		
   		
   		g.setColor(Color.black);
   		//g.drawImage(img1,(int)(spelareX), (int)(spelareY), this);	

   		
   		g.setColor(Color.black);
		g.fillOval((int)(bollX-radie),(int)(bollY-radie),
					(int)(2*radie),(int) (2*radie));
    } 												//end paint	
        
//-------------------------------------------------------------------------------
//fixa med bilderna
//-------------------------------------------------------------------------------	
	public Bricks(){
		try
		{
		url1 = this.getClass().getResource("spelare.jpg");		
		}   											//end try 
		catch(Exception e){}
	    img1 = Toolkit.getDefaultToolkit().getImage(url1); 
	}
//-------------------------------------------------------------------------------
// run metoden
//-------------------------------------------------------------------------------

	public void run(){
	
		while ( true){
							
			bollX = bollX + hastBollX;
			bollY = bollY + hastBollY;
			flytta();
			innanforRam();
			kollaTraff();
			kollaForlust();
			kollaKrock();
			showStatus(raknare + " poäng");
			repaint();
			
			try { Thread.sleep(10);} catch(InterruptedException ie){}
		}											//end while		
	}												//end run    
	
//-------------------------------------------------------------------------------
// keyevent
//-------------------------------------------------------------------------------

	public void keyPressed(KeyEvent ke){
		if(ke.getKeyCode() == KeyEvent.VK_LEFT) leftDown = true;
		if(ke.getKeyCode() == KeyEvent.VK_RIGHT) rightDown = true;	

	}												//end keypressed	
	public void keyReleased(KeyEvent ke){
		if(ke.getKeyCode() == KeyEvent.VK_LEFT) leftDown = false;
		if(ke.getKeyCode() == KeyEvent.VK_RIGHT) rightDown = false;

				
	}												//end key released
	public void keyTyped(KeyEvent ke){}				//end typed    
    	
//-----------------------------------------------------------------
//flimmerfritt
//-----------------------------------------------------------------	
	
	public void update(Graphics g){
		
		rityta.clearRect(0,0,width,height);
		paint(rityta);
		g.drawImage(minnesbild, 0, 0, this);
		
	}												//end update	
	
//-----------------------------------------------------------------
//hålla innanför ramen
//-----------------------------------------------------------------	

	public void innanforRam(){
													//hall bollen inne
		if (bollX+radie > width)
			hastBollX = -hastBollX;
		if (bollX-radie < 0)
			hastBollX = -hastBollX;
		if (bollY+radie > height)
			hastBollY = -hastBollY;	
		if (bollY-radie < 0)
			hastBollY = -hastBollY;	
													//hall spelaren inne
		if (spelareX+spelareBredd > width)
			spelareHast = 0;
		if (spelareX < 0)
			spelareHast = 0;
		if (spelareX + spelareBredd >  width && leftDown)
			spelareHast = 3;
		if (spelareX <  0 && rightDown)
			spelareHast = 3;		
			
	}												//end ram		

//-----------------------------------------------------------------
//flytta spelare
//-----------------------------------------------------------------

	public void flytta(){
	
		if(leftDown){
			spelareX = spelareX - spelareHast;
		}											//end if	
		if(rightDown){
			spelareX = spelareX + spelareHast;
		}											//end if
	}												//end flytta	

//-----------------------------------------------------------------
//kolla krock
//-----------------------------------------------------------------

	public void kollaKrock(){
		
		if(bollX + radie + 1 > spelareX && bollX < spelareX + spelareBredd && bollY + radie + 1 == spelareY){
			hastBollY = -hastBollY;
			if (leftDown)
				hastBollX = -2;	
			else if (rightDown)
				hastBollX = 2;			
		}											//end if
	}												//end krock	

//-----------------------------------------------------------------
//kolla forlust
//-----------------------------------------------------------------/*

	public void kollaForlust(){
		
		if ( bollY + radie + 5 == height){
		
			JOptionPane.showMessageDialog(null, "Du fick " + raknare + " poang");
			bollY = 300;
			bollX = 150;
			spelareX = width/2-40;
			spelareY = 370;
			hastBollY = 2;
			hastBollX = 2;
			resett();


		}											//end if
	}												//end kollaforlust
	
//-----------------------------------------------------------------
//kolla traff
//-----------------------------------------------------------------/*

	public void kollaTraff(){
//kolla traff vanster													
		for(int i=0; i<5; i=i+1){						
			for(int j=0; j<10; j=j+1){
				if (bollX + radie + 5 > 40 + i*(1 + targetBredd) 
				&& bollX - radie - 5 < 40 + (i * (1 +targetBredd) + targetBredd) 
				&& bollY + radie +5 > j * (1 +targetHojd) &&  bollY - radie - 5 < j * 
				(1 +targetHojd) + targetHojd && flagga[i][j] == false){					
					flagga[i][j] = true;	
					raknare = raknare + 20;					
 //avstand hoger				
				sidor = Math.abs(bollX - 40 + i * (1 + targetBredd));
					if(Math.abs(bollX - 40 + (i * (1 + targetBredd) + targetBredd)) < sidor){
						sidor = Math.abs(bollX - 40 + i * (1 + targetBredd) + targetBredd); 
					}											
				uppeNere = Math.abs(bollY - j * (1 + targetHojd));
					if(Math.abs(bollY - (j * (1 + targetHojd) + targetHojd)) < uppeNere){
						uppeNere = Math.abs(j * (1 + targetHojd) + targetHojd);
					}	
					if (sidor < uppeNere) 
						hastBollX = -hastBollX;
					else			
						hastBollY = -hastBollY;
				}									//end if kolla traffen forst
   			}										//end for j
   		}											//end for i	   													   	    				   		
	}												//end kollatraff		

//-------------------------------------------------------------------------------
//resett
//-------------------------------------------------------------------------------	

	public void resett(){
	
		for(int i=0; i<5; i=i+1){
			for(int j=0; j<10; j=j+1){
				
				flagga[i][j] = false;
				leftDown = false;
				rightDown = false;
				raknare = 0;
   			}										//end for j
   		}											//end for i		
	}
		
}//end class
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------



Figuered it out, stupid naming the image wrong, but i still got a problem.. the image doesnt show up now :P

This post has been edited by gawdlike: 10 December 2008 - 02:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding image to game

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Adding image to game

Posted 11 December 2008 - 03:09 AM

Check the Java applet console for errors. Please repost the current code otherwise

This post has been edited by g00se: 11 December 2008 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 gawdlike  Icon User is offline

  • you can't fix stupid.

Reputation: -7
  • View blog
  • Posts: 647
  • Joined: 16-November 08

Re: Adding image to game

Posted 11 December 2008 - 03:54 AM

This is then current code, i just renamed my image to .img :)

The problem is that now it runs, but there is no picture displayed..
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Adding image to game

Posted 11 December 2008 - 03:58 AM

Did you check the content of the applet console as i suggested?

Please post your source as an attachment
Was This Post Helpful? 0
  • +
  • -

#5 gawdlike  Icon User is offline

  • you can't fix stupid.

Reputation: -7
  • View blog
  • Posts: 647
  • Joined: 16-November 08

Re: Adding image to game

Posted 11 December 2008 - 04:13 AM

View Postg00se, on 11 Dec, 2008 - 02:58 AM, said:

Did you check the content of the applet console as i suggested?

Please post your source as an attachment


I am in school now, tried it out here, and it works. The only thing wrong now, even though it is way off topic, is that my collision detection is somewhat faulty. Is there any error in the logic that i can't see?
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Adding image to game

Posted 11 December 2008 - 04:15 AM

Best post new question
Was This Post Helpful? 0
  • +
  • -

#7 gawdlike  Icon User is offline

  • you can't fix stupid.

Reputation: -7
  • View blog
  • Posts: 647
  • Joined: 16-November 08

Re: Adding image to game

Posted 11 December 2008 - 04:23 AM

Ill do that :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1