1

hey my question is how do i put a picture onto the ball object in the game Pong in java? below is my current code for an object called Ball which extends an abstract class Shape.

package Software;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Ball extends Shape implements ActionListener {
    public Image tennisBall;

    public Ball() {
        try {
            tennisBall = ImageIO.read(new File("tennisBall.jpg"));
        } catch (IOException ex) {
            // handle exception...
        }
        setHeight(12);
        setWidth(12);
        speed = 5;
    }

    @Override
    public void draw(Graphics g) {
        // g.fillOval(getPositionX(), getPositionY(), (int)getWidth(), (int)getHeight());
        g.drawImage(tennisBall, getPositionX(), getPositionY(), (int)getWidth(), (int)getHeight(), null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
KrazyKat89
  • 81
  • 1
  • 5
  • give details on the problem, what error do you see etc? – Juvanis Oct 20 '12 at 09:01
  • 2
    Can you describe what's happening, and what you expect to happen? Also, have you checked the image is actually being read in? You're discarding the exception that would tell you if it's not. – Zarkonnen Oct 20 '12 at 09:01
  • hi, well the ball is in the game apparently because it still hits the wall and gives the player a score increase but the image is not there so you cant actually see where the ball is. i believe the problem to be in the draw(graphics g) section but i dont really know – KrazyKat89 Oct 20 '12 at 09:34
  • In your `catch (IOException ex)` clause, put `ex.printStackTrace()` and run again. It should give more information. Also, I'd suggest setting `public Image tennisBall` to `private` – La bla bla Oct 20 '12 at 09:55
  • sweet well it came back and said javax.imageio.IIOException: Can't read input file! – KrazyKat89 Oct 20 '12 at 09:58
  • sorry entered too early but yeah it has that error message but i still dont no where i went wrong because the file exists in the right place – KrazyKat89 Oct 20 '12 at 09:59
  • Try the following line: System.out.println(new File("tennisBall.jpg").getAbsolutePath()); Does the printed path match the path of the file? Java might be using a different start point for relative paths than you're expecting. – Zarkonnen Oct 20 '12 at 11:21
  • omg that fixed it :) thank you so much.... muchly appreciated, i could kiss you lol. i did have the file placed in the wrong area. can i just ask one more question??? of what file type should i have the picture as so that i can have a transparent background. – KrazyKat89 Oct 20 '12 at 11:28
  • 1
    See also this [Q&A](http://stackoverflow.com/q/9849950/230513). – trashgod Oct 20 '12 at 12:55

1 Answers1

0

I read your question in the comments. I would use a Portable Network Graphics (PNG) image if you are using transparency.

In case this would be of use, you can create new colors in Java and you don't have to import pictures. If you want to paint a slightly transparent blue ball on the screen you do this.

g.setColor(new Color(0, 0, 255, 175);
g.fillOval(xPosition, yPosition, diameter, diameter);

I created a new Color and passed it the variables (int r, int g, int b, int alpha). So the first integer is the amount of red and the second is green and third blue. But the alpha variable is the opacity of the color (how transparent it is). Then you paint the "oval" but it has the same diagonal lengths so it comes out a circle.

Liam15
  • 75
  • 1
  • 6