|
-
Aug 30th, 2007, 11:36 AM
#1
Thread Starter
New Member
Sprites & "Objects"
Hello everyone,
I'm new to this forum, but I'm hoping to find someone that can help me with a few classes I've been trying to write so that I can "emulate" some Game Maker-like stuff (that's why some variables have 'weird' names, though most of them are logical). I'm new to Java, and I'm trying to have several Sprite instances, that each contain an array of Images so that I can use animation. The "Objects" each can have a sprite object linked to them.
However, the image I'm trying to display isn't there? I can't figure out why either. Can somebody help me?
Test6-class
Code:
import javax.swing.*;
import java.awt.Graphics;
public class Test6{
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Graphics g = frame.getGraphics();
Sprite spr = new Sprite("lord",3,frame);
Object obj = new Object(150,150,spr,frame);
long ticks = System.currentTimeMillis();
long FPS = 30;
boolean done = false;
// Game-loop
while(!done){
if (System.currentTimeMillis() > ticks+(1000/FPS)){
ticks = System.currentTimeMillis();
obj.draw(g);
}
}
}
}
Sprite-class
Code:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
public class Sprite{
// Fields
private int image_number;
private Image[] image;
private int sprite_height;
private int sprite_width;
// Constructor
public Sprite(String file_name, int image_number, JFrame frame){
this.image_number = image_number;
image = new Image[image_number];
for (int i=0; i<image_number; i++){
image[i] = (new ImageIcon(file_name+
i+".BMP")).getImage();
}
sprite_height = image[image_number-1].getHeight(frame);
sprite_width = image[image_number-1].getWidth(frame);
}
// Methods
public Image getImage(int image_index){
return image[image_index];
}
}
Object-class
Code:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
public class Object{
// Fields
public Sprite sprite_index;
public float image_speed = 0;
public float image_index = 0;
public int x;
public int y;
public JFrame frame;
// Constructor
public Object(int x, int y, Sprite sprite, JFrame frame){
this.x = x;
this.y = y;
sprite_index = sprite;
this.frame = frame;
}
// Methods
public void draw(Graphics g){
Image image = sprite_index.getImage((int)image_index);
g.drawImage(image,x,y,frame);
}
}
Thanks in advance,
LordBob
-
Aug 30th, 2007, 02:00 PM
#2
Re: Sprites & "Objects"
make sure your images are stored in the path specified by:
Code:
System.out.println(System.getProperty("user.dir"));
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 30th, 2007, 02:48 PM
#3
Thread Starter
New Member
Re: Sprites & "Objects"
They are stored in the right folder. :/ I was able to load images from there using a more simple program, so I don't know why this program doesn't work. :\
Edit: I solved it. When I replaced ".BMP" by ".PNG" it suddenly worked (I had to change the images too, though). That's just weird; I expected BMP's to work with Java too... Anyway, how do you make images transparant other than to use PNG's transparecy stuff?
Last edited by LordBob; Aug 30th, 2007 at 07:10 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|