2 Attachment(s)
Trying to put a background in a program with an animation.
I need help. I have the background, and I can see it in the program, but it refreshes at an ugly rate.
Here is the code.
Code:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;
import java.net.*;
public class AnimationTest extends JFrame implements Runnable {
Image img = Toolkit.getDefaultToolkit().createImage("Background.png");
static int ScreenWidth =640;
static int ScreenHeight = 480;
Thread gameloop;
Random rand = new Random();
//double buffer objects
BufferedImage backbuffer;
Graphics2D g2d;
//sprite variables
Image image;
Point pos = new Point(100,325);
//animation variables
int currentFrame = 0;
int totalFrames = 18;
int animationDirection = 1;
int frameCount = 0;
int frameDelay = 20;
public static void main(String[] args) {
new AnimationTest();
}
public AnimationTest() {
super("Animation Test");
setSize(ScreenWidth,ScreenHeight);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create the back buffer for smooth graphics
backbuffer = new BufferedImage(ScreenWidth, ScreenHeight, BufferedImage.TYPE_INT_RGB);
g2d = backbuffer.createGraphics();
//load the ryu animation strip
Toolkit tk = Toolkit.getDefaultToolkit();
image = tk.getImage(getURL("ryuPunch.png"));
gameloop = new Thread(this);
gameloop.start();
}
private URL getURL(String filename) {
URL url = null;
try {
url = this.getClass().getResource(filename);
}
catch (Exception e) {}
return url;
}
public void run() {
Thread t = Thread.currentThread();
while (t == gameloop) {
try {
Thread.sleep(5);
}
catch (InterruptedException e) {
e.printStackTrace();
}
gameUpdate();
}
}
public void gameUpdate() {
//clear the background
g2d.setColor(Color.WHITE);
g2d.fill( new Rectangle(0, 0, ScreenWidth-1, ScreenHeight-1) );
//draw the current frame of animation
drawFrame(image, g2d, pos.x, pos.y, 6, currentFrame, 128, 128);
// g2d.setColor(Color.WHITE);
g2d.drawString("Position: " + pos.x + "," + pos.y, 10, 50);
g2d.drawString("Animation: " + currentFrame, 10, 70);
//see if it's time to animate
frameCount++;
if (frameCount > frameDelay) {
frameCount=0;
//update the animation frame
currentFrame += animationDirection;
if (currentFrame > totalFrames - 1) {
currentFrame = 0;
// pos.x = rand.nextInt(ScreenWidth-128);
// pos.y = rand.nextInt(ScreenHeight-128);
}
else if (currentFrame < 0) {
currentFrame = totalFrames - 1;
}
}
repaint();
}
public void paint(Graphics g) {
g.drawImage(img,0,0,null);
//draw the back buffer to the screen
g.drawImage(backbuffer, 0, 0, this);
}
public void paintComponent(Graphics g)
{
// Draw the previously loaded image to Component.
g.drawImage(img, 0, 0, null);
}
//draw a single frame of animation
public void drawFrame(Image source, Graphics2D dest,
int x, int y, int cols, int frame,
int width, int height)
{
int fx = (frame % cols) * width;
int fy = (frame / cols) * height;
dest.drawImage(source, x, y, x+width, y+height,
fx, fy, fx+width, fy+height, this);
}
}
These are the two pictures you will need to make it work.
Attachment 3273Attachment 3274
Please help. I am asking for someone to suggest some code to have a background work while the animation runs. Thank you in advance.
Re: Trying to put a background in a program with an animation.
Your indentation, like your code, is all over the place. If you expect volunteers to read your code and try to help, the least you can do is to ensure a consistent formatting style throughout.
Wht are you overriding both paint(...) and paintComponent(...)? With an override to paint(...) that doesn't call into the super implementation, paintComponent(...) will never be called.
Go through this Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
Re: Trying to put a background in a program with an animation.
You can't learn by copying and badly modifying, without first understanding, code you pick up on the net. And are you sure that posting it on a public forum isn't a violation of copyright?
1435458087SE6
db
Re: Trying to put a background in a program with an animation.
Quote:
Originally Posted by
DarrylBurke
You can't learn by copying and badly modifying, without first understanding, code you pick up on the net. And are you sure that posting it on a public forum isn't a violation of copyright?
1435458087SE6
db
It's from a book.
Nevermind guys.