[java] drawImage() - Having problems drawing image through passing paramaters and two files

Started by
2 comments, last by Rastur 16 years, 11 months ago
I've gotten image to load up properly if I do it all in one file, and it showed up properly. Now I'm trying to implement it into a class, so I can create multiple objects of it. I've "highlighted" important code by surrounding it with comments. I'm pretty sure I'm using "this" wrongly, but I don't know anything about "this". Here is my error: cannot find symbol symbol : method drawImage(java.awt.Image,int,int,Player) location: class java.awt.Graphics window.drawImage(img, xPOS, yPOS, this); Thanks in advance, Adamski PlanetEdge.java

/*
 *	PlanetEdge
 *	Paul Adamski
 *
 *	PlanetEdge.java
 */
 
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
import javax.swing.JFrame;
import java.util.ArrayList;
import java.lang.Character.*;
import java.awt.event.KeyListener;

import java.awt.Image;

public class PlanetEdge extends JFrame implements KeyListener
{
	private Timer timer;
	private int team = 2;
	private Graphics dbg;			// Double buffering
	private Image dbImage;			// Double buffering
	private int SLEEP = 50;			// Higher SLEEP results in slower animation
	private Image playerIMG;
	private int playerXLOC = 50;
	private int playerYLOC = 300;		
	private int playerXSPEED = 1;
	private int playerYSPEED = 1;
	private boolean playerGUN = false;
	
	public PlanetEdge()
	{
		super("PlanetEdge");
		
		setSize(800,600);
		setVisible(true);
		
		ActionListener paintCaller = new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				repaint();
			}
		};
		
		timer = new Timer(SLEEP, paintCaller);
		timer.start();
		
		this.addKeyListener(this);
	}
	
	public void init()
	{
/////////////////////////////////////////////////////////////////////////////
		Toolkit.getDefaultToolkit();
		playerIMG = Toolkit.getDefaultToolkit().getImage("PIC.gif");
/////////////////////////////////////////////////////////////////////////////
	}
	
	public void update (Graphics window)
	{
		// Initialize the buffer
		if (dbImage == null)
		{
			dbImage = createImage (this.getSize().width, this.getSize().height);
			dbg = dbImage.getGraphics ();
		}
		
		// Clear the background image
		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
			
		// Draw the frame in the background
		dbg.setColor (getForeground());
		paint (dbg);
			
		// Bring it to screen
		window.drawImage (dbImage, 0, 0, this);
	}

	public void keyPressed(KeyEvent e)
	{
		if (e.getKeyChar() == 'd')
			playerXSPEED += 10;
			
		if (e.getKeyChar() == 'a')
			playerXSPEED -= 10;
			
		if (e.getKeyChar() == 'w')
			playerYSPEED -= 20;
			
		if (e.getKeyChar() == 's')
			playerYSPEED += 20;
			
		if (e.getKeyChar() == 32)
			playerGUN = true;
			
		System.out.println("Key hit detected");
	}

	public void keyReleased(KeyEvent e)
	{

	}

	public void keyTyped(KeyEvent e)
	{

    }
    
	public void paint(Graphics window) 
	{		
		// Set up the border and background
		window.drawRect(0,0,800,600);
		
		window.setColor(new Color(0,100,0));
		window.fillRect(1,1,799,599);
		
		System.out.println("Background drawn");
		
		// Set up the team and player
		Team VANU1 = new Team(team, 25, 100, window);
		Team VANU2 = new Team(team, 25, 200, window);
		Team VANU3 = new Team(team, 25, 400, window);
		Team VANU4 = new Team(team, 25, 500, window);
		
//////////////////////////////////////////////////////////////////////////
		Player PLAYER = new Player(team, playerIMG, 50+playerXSPEED, 300+playerYSPEED, window);
//////////////////////////////////////////////////////////////////////////
				
		Enemy TR1 = new Enemy(2, 900, 100, window);
		Enemy TR2 = new Enemy(2, 900, 200, window);
		Enemy TR3 = new Enemy(2, 900, 300, window);
		Enemy NC1 = new Enemy(3, 900, 400, window);
		Enemy NC2 = new Enemy(3, 900, 500, window);
		
		System.out.println("Objects constructed");
		
		// Shooting
		if (playerGUN == true)
		{	
			while (playerXLOC < 800)
			{
				window.drawOval(playerXLOC+25,playerYLOC-30,10,2);
				playerXLOC += 25;
				
				/*if ((playerXLOC > VS6.getX()-25) && (playerYLOC > TR1.getY() - 20) && (playerYLOC < TR1.getY() + 55))
				{
					System.out.println("HIT");
				}*/
			}
			
			playerGUN = false;
		}
		
		playerXLOC = PLAYER.getX();
		playerYLOC = PLAYER.getY();
		/////////////////////////////////
	}
	
	public static void main(String args[])
	{
		PlanetEdge GAME = new PlanetEdge();
	}
}

Player.java

/**
 *	PlanetEdge
 *	Paul Adamski
 *
 *	Player.java
 */

import java.awt.*;

import java.awt.Image;

import java.applet.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
import javax.swing.JFrame;
import java.util.ArrayList;
import java.lang.Character.*;
import java.awt.event.KeyListener;

public class Player
{
	private int playerXLOC;
	private int playerYLOC;
	
	public Player(int teamChoice, Image img, int xPOS, int yPOS, Graphics window)
	{	
		playerXLOC = xPOS;
		playerYLOC = yPOS;

////////////////////////////////////////////////////////////////////////
		window.drawImage(img, xPOS, yPOS, this);
////////////////////////////////////////////////////////////////////////
				
		/*
		window.setColor(Color.black);
		
		// Legs
		window.drawLine(xPOS,yPOS,xPOS+9,yPOS+30);
		window.drawLine(xPOS,yPOS,xPOS-9,yPOS+30);
		// Body
		window.drawLine(xPOS,yPOS,xPOS,yPOS-35);
		// Arms
		window.drawLine(xPOS,yPOS-25,xPOS+10,yPOS-19);
		window.drawLine(xPOS+12,yPOS-19,xPOS+17,yPOS-25);
		
		// VANU SOVEREIGNTY
		if (teamChoice == 1)
			window.setColor(new Color(138,43,226));
			
		// TERRAN REPUBLIC
		if (teamChoice == 2)
			window.setColor(Color.black);
			
		// NEW CONGLOMERATE
		if (teamChoice == 3)
			window.setColor(Color.blue);
			
		// Helmet
		window.fillOval(xPOS-10,yPOS-55,20,20);
			
		// Gun
		window.fillRect(xPOS+1,yPOS-30,5,10);
		window.fillRect(xPOS+1,yPOS-30,20,5);
		
		// VANU SOVEREIGNTY
		if (teamChoice == 1)
			window.setColor(new Color(143,188,143));
			
		// TERRAN REPUBLIC
		if (teamChoice == 2)
			window.setColor(Color.red);
			
		// NEW CONGLOMERATE
		if (teamChoice == 3)
			window.setColor(Color.yellow);
			
		// Gun Highlights
		window.fillRect(xPOS+7,yPOS-30,1,5);
		window.fillRect(xPOS+10,yPOS-30,1,5);
		window.fillRect(xPOS+13,yPOS-30,1,5);
		window.fillRect(xPOS+16,yPOS-30,1,5);
		window.fillRect(xPOS+19,yPOS-30,1,5);
		
		// Helmet Visor
		window.fillRect(xPOS-10,yPOS-50,20,5);*/
	}
	
	public int getX()
	{
		return playerXLOC;
	}
		
	public int getY()
	{
		return playerYLOC;
	}
}

Advertisement
The last parameter you are passing is your own Player class. It should be a class that implements ImageObserver if I'm not mistaken.

shmoove
You're correct - in the context you are using it, "this" is your Player instance. The method is expecting an ImageObserver reference, which would be the frame or panel that contains your graphics context. You might be able to pass a null for that parameter, but if you get flaky painting results you will probably need to supply a reference to the component that supplied your Graphics context. Since drawImage() returns immediately, perhaps before the image is completely redendered, the ImageObserver allows components to monitor the completion of that image.

Short answer, try using null for that parameter and see if everything looks fine =)

Or you could joust add a need of a JFrame, set it to be the observer and call it with "this" in the main program (since you are using a JFrame).

This topic is closed to new replies.

Advertisement