Jump to content

g.drawImage() will not work

- - - - -

  • Please log in to reply
8 replies to this topic

#1
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
private class Painter extends JPanel {

		private Painter() {

			xloc = 300;

			yloc = 300;

			whatToChange = -1;

		}

		public void paint(Graphics g) {

			Image player;

			if (whatToChange == -1) {

				player = tools.getImage("pic/stickSprite/standing");

			} else if (whatToChange == 0) {

				player = tools.getImage("pic/stickSprite/usingleft");

			} else if (whatToChange == 1) {

				player = tools.getImage("pic/stickSprite/usingright");

			} else if (whatToChange == 2) {

				player = tools.getImage("pic/stickSprite/left");

				xloc += 20;

			} else if (whatToChange == 3) {

				player = tools.getImage("pic/stickSprite/right");

				xloc -= 20;

			} else if (whatToChange == 4) {

				player = tools.getImage("pic/stickSprite/climbingleft");

				xloc += 20;

				yloc += 20;

			} else {

				player = tools.getImage("pic/stickSprite/climbingright");

				xloc -= 20;

				yloc += 20;

			} //TODO falling

			g.drawImage(player, xloc, yloc, 60, 30, this);

		}

	}

}

WHen I create a new Painter object, add it to the JFrame, and call its repaint method, it doesn't show the image!

#2
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
For Swing compoents like JPanel you should override the paintComponent method, not the paint method.
The usual reason that images don't draw is because they are in different locations than the program is looking for them.

For testing, somewhere create a File object with the path to one of the image files and print out its full path to see where the program is looking for the file. Also try the File class's exists() method to see if the program can find it.

What class is the tools variable? Some of the older classes and methods do not wait for the image to load. They return immediately. Look at using the ImageIO class for reading the images.
I'd move the reading of the image outside of the painting method. Read all of the images one time and be done reading them. Put the image objects into an array and select the one you want in the paint class.

#3
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
tools is a Toolkit.
Overriding paintComponent didn't work.
I already used .exists() in the program.

---------- Post added at 07:54 PM ---------- Previous post was at 07:53 PM ----------

I added .png to all the file names (silly mistake!) but it still doesn't work! :confused:

---------- Post added at 07:55 PM ---------- Previous post was at 07:54 PM ----------

The class Painter is an inner class, if that helps at all.

#4
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
Show the code with paintComponent that did not work.
How did you use the File class to check that the image files are in the right location.
I don't understand what you used the exists method for and what results you got if the image files were incorrectly named???

Did you try ImageIO?

Make a small program that compiles, executes and shows your problem and post it here.

#5
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
I wrote a small program that tested to see if the images existed. It outputted true.
What is ImageIO?

#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
ImageIO is a class in the java SE classes that does image I/O
Java Platform SE 6

#7
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
Okay, how do I use it?

#8
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
There are many many code samples on the web or on the forum. Do a search.

#9
PicklishDoorknob

PicklishDoorknob

    Learning Programmer

  • Members
  • PipPipPip
  • 86 posts
ok I will. please hold.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users