Results 1 to 4 of 4
  1. #1
    Triss is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default 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:
    Java 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);
    	}
    }

  2. #2
    Triss is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

    Pleaaaase, help me :(

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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
    {
        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);
        }
    }

  4. #4
    Triss is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

Similar Threads

  1. moving slider with key event
    By adam405 in forum New To Java
    Replies: 1
    Last Post: 03-18-2008, 04:50 PM
  2. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  3. Moving icons on your desktop
    By Leprechaun in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 11:07 AM
  4. moving a file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-10-2007, 08:52 PM
  5. examples of moving objects
    By fred in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:06 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
  •