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 01-17-2008, 04:39 PM
Member
 
Join Date: Jan 2008
Posts: 5
Triss is on a distinguished road
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:
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); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-17-2008, 06:41 PM
Member
 
Join Date: Jan 2008
Posts: 5
Triss is on a distinguished road
Pleaaaase, help me
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-17-2008, 07:37 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-17-2008, 07:52 PM
Member
 
Join Date: Jan 2008
Posts: 5
Triss is on a distinguished road
Wow, thanks a lot!! it works you made my day ^^
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
moving slider with key event adam405 New To Java 1 03-18-2008 04:50 PM
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
Moving icons on your desktop Leprechaun New To Java 3 12-14-2007 11:07 AM
moving a file Java Tip Java Tips 0 11-10-2007 08:52 PM
examples of moving objects fred New To Java 1 08-07-2007 07:06 PM


All times are GMT +3. The time now is 02:49 PM.


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