# Thread: moving image - PROBLEM

1. Member
Join Date
Jan 2008
Posts
5
Rep Power
0

## moving image - PROBLEM

Hello guys,

I have a big problem. I'm a total beginner to Java, and I have to solve it!
I have a code of a moving square. What I want to do, is to change it into an image. I know how to place images in applets, but I have no idea how to use it here. So if you could, please tell me the code of moving image, instead of this ugly square.

Here's the square code:
Java Code:
```import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;

public class watek extends Applet implements Runnable
{
public void init()
{
resize(500, 500);
watek.start();
}
public void destroy()
{
watek = null;
}
public void paint(Graphics g)
{
for(int i = 0; i < kwadraty.size(); i++)
}

public void run()
{
while (true)
{
repaint();
try
{
watek.sleep(10);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
}
{
int x;
int y;
int d;
int vx;
kwadrat(int X, int Y, int D, int VX)
{
x = X;
y = Y;
d = D;
vx = VX;
}
public void paint(Graphics g)
{
x += vx;
if(x >= 500)
vx=-Math.abs(vx);
if(x <= 0)
vx=Math.abs(vx);
g.drawLine(x - d/2, y - d/2, x + d/2, y -d/2);
g.drawLine(x - d/2, y + d/2, x + d/2, y +d/2);
g.drawLine(x - d/2, y - d/2, x - d/2, y +d/2);
g.drawLine(x + d/2, y - d/2, x + d/2, y +d/2);
}
}```

2. Member
Join Date
Jan 2008
Posts
5
Rep Power
0
Pleaaaase, help me :(

3. Java Code:
```//  <applet code="WatekRx" width="500" height="500"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.List;
import javax.imageio.ImageIO;

public class WatekRx extends Applet implements Runnable
{
List<BufferedImage> images = new ArrayList<BufferedImage>();
BufferedImage backBuffer;
boolean animating = false;

public void init()
{
resize(500, 500);
}

public void start()
{
if(!animating)
{
animating = true;
watek.start();
}
}

public void stop()
{
animating = false;
if(watek != null)
watek.interrupt();
watek = null;
}

public void paint(Graphics g)
{
// Use double-buffering for smooth animation in AWT.
if(backBuffer == null) {
int w = getWidth();
int h = getHeight();
int type = BufferedImage.TYPE_INT_RGB;
backBuffer = new BufferedImage(w, h, type);
Graphics2D g2 = backBuffer.createGraphics();
g2.setPaint(getBackground());
g2.fillRect(0,0,w,h);
g2.dispose();
}
g.drawImage(backBuffer, 0, 0, this);
}

public void update(Graphics g)
{
// This helps to eliminate flicker.
// Use in AWT components only, ie, not in Swing.
paint(g);
}

public void run()
{
while (animating)
{
repaint();
try
{
watek.sleep(100);
}
catch (InterruptedException e)
{
System.out.println(e);
}
// Update the backBuffer.
Graphics2D g2 = backBuffer.createGraphics();
g2.setPaint(getBackground());
g2.fillRect(0,0,backBuffer.getWidth(), backBuffer.getHeight());
for(int i = 0; i < kwadraty.size(); i++)
{
BufferedImage image = images.get(i);
g2.drawImage(images.get(i), kw.x, kw.y, this);
}
g2.dispose();
}
}

{
String[] ids = { "images/hawk.jpg" };
for(int j = 0; j < ids.length; j++)
{
try
{
// to locate resource.
URL url = getClass().getResource(ids[j]);
}
catch(IOException e)
{
System.out.println("Read error at " + j +
": " + e.getMessage());
}
}
}
}

{
int x;
int y;
int d;
int vx;

Kwadrat(int X, int Y, int D, int VX)
{
x = X;
y = Y;
d = D;
vx = VX;
}

public void draw(Graphics g)
{
g.drawLine(x - d/2, y - d/2, x + d/2, y -d/2);
g.drawLine(x - d/2, y + d/2, x + d/2, y +d/2);
g.drawLine(x - d/2, y - d/2, x - d/2, y +d/2);
g.drawLine(x + d/2, y - d/2, x + d/2, y +d/2);
}

{
x += vx;
if(x >= 500)
vx=-Math.abs(vx);
if(x <= 0)
vx=Math.abs(vx);
}
}```

4. Member
Join Date
Jan 2008
Posts
5
Rep Power
0
Wow, thanks a lot!! it works :D you made my day ^^

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•