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