|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

01-17-2008, 04:39 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 5
|
|
|
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.
Thank you in advance!!
Here's the square code:
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
public class watek extends Applet implements Runnable
{
Vector <kwadrat> kwadraty;
Thread watek;
public void init()
{
kwadraty = new Vector<kwadrat>();
resize(500, 500);
watek= new Thread(this);
watek.start();
kwadrat kw = new kwadrat(100, 100, 10, 1);
kwadraty.addElement(kw);
}
public void destroy()
{
watek = null;
}
public void paint(Graphics g)
{
for(int i = 0; i < kwadraty.size(); i++)
kwadraty.elementAt(i).paint(g);
}
public void run()
{
while (true)
{
repaint();
try
{
watek.sleep(10);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
}
class kwadrat
{
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);
}
}
|
|

01-17-2008, 06:41 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 5
|
|
Pleaaaase, help me 
|
|

01-17-2008, 07:37 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
// <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
{
Vector <Kwadrat> kwadraty;
List<BufferedImage> images = new ArrayList<BufferedImage>();
BufferedImage backBuffer;
Thread watek;
boolean animating = false;
public void init()
{
loadImages();
kwadraty = new Vector<Kwadrat>();
Kwadrat kw = new Kwadrat(100, 100, 10, 1);
kwadraty.addElement(kw);
resize(500, 500);
}
public void start()
{
if(!animating)
{
animating = true;
watek = new Thread(this);
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++)
{
Kwadrat kw = kwadraty.elementAt(i);
kw.advance();
BufferedImage image = images.get(i);
g2.drawImage(images.get(i), kw.x, kw.y, this);
}
g2.dispose();
}
}
private void loadImages()
{
String[] ids = { "images/hawk.jpg" };
for(int j = 0; j < ids.length; j++)
{
try
{
// ClassLoader searches classpath
// to locate resource.
URL url = getClass().getResource(ids[j]);
images.add(ImageIO.read(url));
}
catch(IOException e)
{
System.out.println("Read error at " + j +
": " + e.getMessage());
}
}
}
}
class Kwadrat
{
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);
}
public void advance()
{
x += vx;
if(x >= 500)
vx=-Math.abs(vx);
if(x <= 0)
vx=Math.abs(vx);
}
}
|
|

01-17-2008, 07:52 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 5
|
|
Wow, thanks a lot!! it works you made my day ^^
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|