Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Images won't move

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Images won't move

    Hello all,

    I created a java program that simply drew rectangles on a JPanel and then they would move across the screen, and when they got to the end, they would reset at the other end with a random y position and random speed for moving across. This program worked. The issue I am having is that I replaced the rectangles with an image from a file and it will draw the image correctly, but the images don't move. Can someone point me to what I'm doing wrong that the rectangles would move but the images do not? Thanks.

    Below are the 2 class I have for this program. They are taken from an introduction to videogame programming tutorial I found online but have been modified from what the tutorial originally had happening.

    import java.util.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.imageio.*;
    import java.lang.Math;
    import java.io.*;
     
    public class VGKernel extends JPanel
    {
     
    public Rectangle screen, bounds;
    public JFrame frame;
    public VGTimerTask vgTask;
    public RedBloodCell[] redCellArray = new RedBloodCell[10];
    public Random RG = new Random();
     
      public VGKernel()
      {
        super();
        screen = new Rectangle(0, 0, 800, 600);
        bounds = new Rectangle(0, 0, 800, 600); 
        for(int i = 0; i < redCellArray.length; i++)
        {
            redCellArray[i] = new RedBloodCell(RG.nextInt(bounds.width+1),RG.nextInt(bounds.height+1),(RG.nextInt(5)+4),bounds.width,bounds.height);
        }
        frame = new JFrame("VGKernel");
        vgTask = new VGTimerTask();
      }
     
      class VGTimerTask extends TimerTask
      {
        public void run()
        {
          for(int j = 0; j < redCellArray.length; j++)
          {
              redCellArray[j].move();
          }
          frame.repaint();
        }
      }
     
      public void paintComponent(Graphics g)
      {
        bounds = g.getClipBounds();
        g.clearRect(screen.x, screen.y, screen.width, screen.height);
        for(int k = 0; k < redCellArray.length; k++)
        {
            g.drawImage(redCellArray[k].img, redCellArray[k].x, redCellArray[k].y, this);
        }
      }
     
      public static void main(String arg[])
      {
     
        java.util.Timer vgTimer = new java.util.Timer();
        VGKernel panel = new VGKernel();
     
        panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.frame.setSize(panel.screen.width, panel.screen.height);
     
        panel.frame.setContentPane(panel);
        panel.frame.setVisible(true);
     
        vgTimer.schedule(panel.vgTask, 0, 20);
      }
    }
     
    import java.util.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.imageio.*;
    import java.lang.Math;
    import java.io.*;
     
    public class RedBloodCell
    {
        public int x, y, width, height, Xstart, Yheight;
        int xVel;
        public Random RG = new Random();
        public BufferedImage img = null;
     
        public RedBloodCell(int tempX, int tempY, int tempSpeed, int tempWidth, int tempHeight)
        {
            x = tempX;
            y = tempY;
            try
            {
                img = ImageIO.read(new File("redcell1.png"));
            }
            catch(IOException e){}
            xVel = width/tempSpeed;
            Xstart = tempWidth;
            Yheight = tempHeight;
        }
     
        public void move()
        {
            x-=xVel;
     
            if(x < 0)
            {
                x = Xstart;
                y = RG.nextInt(Yheight);
                xVel = width/(RG.nextInt(5)+4);
            }
        }
    }


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Images won't move

    I found out where I was having the issue. The speed was dependent on the width of the rectangle and I forgot to change that part. Shows what coming back and looking at your code later and while not tired will do.

Similar Threads

  1. [SOLVED] Things Won't Move
    By zeraxis in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 2nd, 2011, 08:46 AM
  2. How to drag the shape to move?
    By ice in forum AWT / Java Swing
    Replies: 21
    Last Post: December 15th, 2010, 06:45 PM
  3. Node swap (move up)
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2010, 06:56 PM
  4. How to move an image (or how to delete one)
    By User in forum AWT / Java Swing
    Replies: 3
    Last Post: December 17th, 2009, 11:25 AM
  5. Want to move my button away from center.
    By Fendaril in forum AWT / Java Swing
    Replies: 2
    Last Post: November 6th, 2009, 10:05 PM