Results 1 to 2 of 2
  1. #1
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default Image problems, what's wrong here.

    I am trying to make an applet with a ball that bounces around the screen, and switches between being a black square and an image (right0.gif)

    The square will bounce around just fine, but there isn't an image to go with it (I tested and it DOES throw an IOException in init() - ImageIO.read()).
    Java Code:
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.ImageIO;
    
    public class BouncingBall extends Applet implements Runnable {
    
    private BufferedImage img;
    private Image offscreenImage;
    private Graphics offscr;
    private Thread t;
    private boolean showImage;
    private Ball ball;
    private int width, height;
    
    public void init() {
    t = new Thread(this);
    t.start();
    showImage = true;
    try {
    URL imageLocation = new URL(getCodeBase(), "right0.gif");
    img = ImageIO.read(imageLocation);
    } catch (IOException e) {
    }
    width = getSize().width;
    height = getSize().height;
    offscreenImage = createImage(width, height);
    offscr = offscreenImage.getGraphics();
    ball = new Ball(0, 0, 5, 5);
    }
    
    public void run() {
    while (true) {
    repaint();
    try {
    t.sleep(30);
    } catch (InterruptedException e) {
    }
    }
    }
    
    public void paint(Graphics g) {
    ball.refresh();
    checkCollisions();
    offscr.setColor(Color.white);
    offscr.fillRect(0, 0, width, height);
    offscr.setColor(Color.black);
    offscr.drawRect(0, 0, width - 1, height - 1);
    if (showImage)
    offscr.drawImage(img, ball.getX(), ball.getY(), this);
    else
    offscr.fillRect(ball.getX(), ball.getY(), 20, 20);
    g.drawImage(offscreenImage, 0, 0, this);
    }
    
    public void update(Graphics g) {
    paint(g);
    }
    
    private void checkCollisions() {
    if (ball.getY() <= 0)
    ball.setSpeedY(5);
    if (ball.getY() + 20 >= height)
    ball.setSpeedY(-5);
    if (ball.getX() + 20 >= width)
    ball.setSpeedX(-5);
    if (ball.getX() <= 0)
    ball.setSpeedX(5);
    }
    
    @SuppressWarnings("deprecation")
    public boolean mouseDown(Event e, int x, int y) {
    showImage = !showImage;
    return true;
    }
    
    }
    And I know that mouseDown is deprecated, I just haven't taken the time to learn how to use the Listeners.

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

    Default

    Java Code:
    //  <applet code="BB" width="400" height="400"></applet>
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.*;
    import javax.imageio.ImageIO;
    
    public class BB extends Applet implements Runnable {
        private BufferedImage img;
        private Image offscreenImage;
        private Graphics offscr;
        private Thread t;
        private boolean showImage;
        int width;
        int height;
        boolean animate = false;
        // No Ball class -> improvise:
        private Point loc = new Point();
        int xSpeed = 3;
        int ySpeed = 2;
    
        public void init() {
            showImage = true;
            try {
                String path = "images/geek/geek-----.gif";
                              //"right0.gif"
                URL imageLocation = new URL(getCodeBase(), path);
                img = ImageIO.read(imageLocation);
            } catch (IOException e) {
                System.out.println("image loading failed: " + e.getMessage());
            }
            addMouseListener(ma);
        }
    
        public void run() {
            while (animate) {
                checkCollisions();
                loc.x += xSpeed;
                loc.y += ySpeed;
                offscr.setColor(Color.white);
                offscr.fillRect(0, 0, width, height);
                offscr.setColor(Color.black);
                offscr.drawRect(0, 0, width - 1, height - 1);
                if (showImage)
                    offscr.drawImage(img, loc.x, loc.y, this);
                else
                    offscr.fillRect(loc.x, loc.y, 20, 20);
                repaint();
                try {
                    t.sleep(100);
                } catch (InterruptedException e) {
                    animate = false;
                }
            }
        }
    
        public void start() {
            while(getWidth() <= 0 || getHeight() <= 0) {
                try {
                    Thread.sleep(25);
                } catch (InterruptedException e) {
                    System.out.println("startup error");;
                }
            }
            width = getSize().width;
            height = getSize().height;
            if(offscreenImage == null) {
                offscreenImage = createImage(width, height);
                offscr = offscreenImage.getGraphics();
            }
            animate = true;
            t = new Thread(this);
            t.start();
        }
    
        public void stop() {
            animate = false;
            if(t != null)
                t.interrupt();
            t = null;
        }
    
        public void paint(Graphics g) {
            g.drawImage(offscreenImage, 0, 0, this);
        }
    
        public void update(Graphics g) {
            paint(g);
        }
    
        private void checkCollisions() {
            int w = showImage ? img.getWidth() : 20;
            int h = showImage ? img.getHeight() : 20;
            if (loc.x + xSpeed <= 0 || loc.x + w + xSpeed >= width)
                xSpeed *= -1;
            if (loc.y + ySpeed < 0 || loc.y + h + ySpeed >= height)
                ySpeed *= -1;
        }
    
        private MouseListener ma = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                showImage = !showImage;
            }
        };
    }

Similar Threads

  1. I am Doing Something Wrong But Don't Know What?
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:16 PM
  2. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM
  3. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM
  4. Replies: 0
    Last Post: 01-08-2008, 06:29 PM
  5. problems when loading an image in servlet
    By oregon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 07:02 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
  •