Question about placing an image in swing
Hello,
now i' making one java swing window, where i draw some lines(they are for streets) and some circles (for cars) and after than i start a little animation. You can see how the cars are moving on the streets an soforth. I want to change the circles with an image from my pictures. That's why i want to ask you to help me how to draw this images, so that i see the animation with them. Please tell me how can i proceed ?
Thanks in advice
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
public class MovingImages extends JPanel
{
BufferedImage image;
Ellipse2D e;
Point2D.Double loc;
public MovingImages(BufferedImage bi)
{
image = bi;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
int diameter = Math.min(w,h)*3/4;
g2.setPaint(Color.blue);
e = new Ellipse2D.Double(w/2-diameter/2, h/2-diameter/2, diameter, diameter);
if(loc == null)
loc = new Point2D.Double(w/2, h/2+diameter/2);
g2.draw(e);
g2.draw(new Line2D.Double(w/2, 0, w/2, h/2-diameter/2));
g2.draw(new Line2D.Double(0, h/2, w/2-diameter/2, h/2));
g2.draw(new Line2D.Double(w/2, h, w/2, h/2+diameter/2));
g2.draw(new Line2D.Double(w, h/2, w/2+diameter/2, h/2));
int x = (int)(loc.x - image.getWidth()/2);
int y = (int)(loc.y - image.getHeight()/2);
g2.drawImage(image, x, y, this);
}
protected void stepAhead()
{
PathIterator pit = e.getPathIterator(null, 0.01); // 0.01 ~ 512
double[] coords = new double[6];
Point2D.Double[] marker = { new Point2D.Double(), new Point2D.Double() };
double minDistance = Double.MAX_VALUE;
boolean foundMinimum = false;
while(!pit.isDone())
{
pit.currentSegment(coords);
double distance = loc.distance(coords[0], coords[1]);
if(foundMinimum)
{
// saving this point following the last minimum
marker[1].x = coords[0];
marker[1].y = coords[1];
foundMinimum = false;
}
if(distance < minDistance)
{
// save the point closest to member "loc"
minDistance = distance;
marker[0].x = coords[0];
marker[0].y = coords[1];
foundMinimum = true;// record the next point
}
pit.next();
}
loc.x = marker[1].x;
loc.y = marker[1].y;
repaint();
}
public static void main(String[] args) throws IOException
{
ImageInputStream iis =
ImageIO.createImageInputStream(new File("images/Bird.gif"));
BufferedImage bi = ImageIO.read(iis);
MovingImages movingImages = new MovingImages(bi);
movingImages.addMouseListener(new Animator(movingImages));
JFrame f = new JFrame("click me");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(movingImages);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class Animator extends MouseAdapter implements Runnable
{
MovingImages movingImages;
Thread thread;
boolean keepGoing;
public Animator(MovingImages mi)
{
movingImages = mi;
keepGoing = false;
}
public void mousePressed(MouseEvent e)
{
if(!keepGoing)
{
keepGoing = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
else if(keepGoing)
{
keepGoing = false;
thread = null;
}
}
public void run()
{
while(keepGoing)
{
try
{
Thread.sleep(40);
}
catch(InterruptedException ie)
{
System.out.printf("interruptus %s\n", ie.getMessage());
keepGoing = false;
}
movingImages.stepAhead();
}
}
}