Jump to content
  • Advertisement
Sign in to follow this  
wingerdinger

[java] Drawing objects on screen

This topic is 2884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, so I have this player class for a platformer with basic physics based horizontal movement. Now, what I'm trying to do is draw this guy on screen. I've tried using Graphics2D, but since I'm using floats/doubles, I can't display the object with drawImage() which is what all the tutorials tell me to use. Am I just approaching this all wrong? What other ways can I get images on screen?

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

public class Player {

private float x;
private float y;

private float xspeed;

private static final float ACCEL = .5f;
private static final float FRICTION = .95f;
private static final float MAX_XSPEED = 4;

private Image image;

public Player(float _x, float _y) {

x = _x;
y = _y;

xspeed = 0;

ImageIcon ii = new ImageIcon(this.getClass().getResource("sprites/run pieces/run1.png"));
image = ii.getImage();

}

public float getX() {return x;}

public float getY() {return y;}

public Image getImage() {return image;}

public void keyPressed(KeyEvent e) {

int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT) {

xspeed -= ACCEL;
if (xspeed < -MAX_XSPEED)
xspeed = -MAX_XSPEED;

}

if (key == KeyEvent.VK_RIGHT) {

xspeed += ACCEL;
if (xspeed > MAX_XSPEED)
xspeed = MAX_XSPEED;

}
}

public void update() {

xspeed *= FRICTION;
x += xspeed;

}

Share this post


Link to post
Share on other sites
Advertisement
All I did was change the way your code loaded an image and it works fine. I replaced:

ImageIcon ii = new ImageIcon(this.getClass().getResource("sprites/run pieces/run1.png"));
image = ii.getImage();

with:


try {
image = ImageIO.read(new File("com/nullbrain/rpg/Data/Toon/Toon_green.gif"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Of course you would change the file path to your image.

You should use try-catch statements whenever possible. It will diagnose why images don't show up or why things don;t work properly.

Also another way you could draw your player is buy creating a render method within the player Class which passes the Graphics2d class through it.
You can cast an float to an int by putting (int) before the variable.

public void Render(Graphics2D g2d){
g2d.drawImage(this.getImage(), (int) this.getX(), (int) this.getY(),null);


}


In your canvas or whatever your main render method is, just call

public void paint(Graphics g){
player.Render((Graphics2D) g);
}


I hope this answers your question.

Share this post


Link to post
Share on other sites
Use BufferedImages, not ImageIcons. For lots of reason, including performance.


String imageFileName = "sprites/run pieces/run1.png";

BufferedImage loadedImage = null;

try {
URL imageFileURL = ClassLoader.getSystemResource(imageFileName);
loadedImage = ImageIO.read(imageFileURL);

} catch (IOException e) {
e.printStackTrace();
}


Then use Graphics2D.drawImage()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!