Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-18-2008, 08:44 AM
Member
 
Join Date: Feb 2008
Location: Oregon, USA
Posts: 24
Bluefox815 is on a distinguished road
Send a message via MSN to Bluefox815
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()).
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-07-2008, 03:45 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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; } }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am Doing Something Wrong But Don't Know What? BHCluster New To Java 3 04-16-2008 02:16 PM
what is wrong with this code masaka New To Java 5 04-16-2008 09:27 AM
What's wrong with this code? Wizard wusa New To Java 14 01-23-2008 12:55 AM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
problems when loading an image in servlet oregon Java Servlet 1 08-05-2007 07:02 PM


All times are GMT +3. The time now is 04:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org