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-20-2008, 02:28 PM
Member
 
Join Date: Jan 2008
Posts: 5
Triss is on a distinguished road
image removing
Hi,
I have a little problem with my code. I created 20 little moving pictures (50x50 pixels) and I want them to disappear after clicking at them. I guess that I have to find the image coordinates, so I invented this:

Code:
public void mouseReleased(MouseEvent m){ int mX = m.getX(); int mY = m.getY(); for(int i = pictures.size() - 1; i >= 0; i--) { if( mX > (pictures.elementAt(i).x) && mX < (pictures.elementAt(i).x + 50) && mY > (pictures.elementAt(i).y) && mY < (pictures.elementAt(i).y + 50)) pictures.remove(i); }

But it doesn't work and I have no idea why. If any of you can help me, I'll be grateful.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-20-2008, 08:53 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Looks okay but it's hard to say. You could try using a rectangle.
Code:
// <applet code="MovingSelection" width="400" height="400"></applet> import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.util.*; import java.util.List; public class MovingSelection extends Applet implements Runnable { List<TokenStore> tokens = new ArrayList<TokenStore>(); BufferedImage buffer; Thread thread; boolean running = false; public void init() { tokens.add(new TokenStore(40, 40, Color.red, 1, 3)); tokens.add(new TokenStore(140, 100, Color.green, 2, 2)); tokens.add(new TokenStore(300, 200, Color.blue, 3, 2)); tokens.add(new TokenStore(100, 325, Color.cyan, 1, 2)); tokens.add(new TokenStore(220, 250, Color.orange, 2, 3)); TokenSelector selector = new TokenSelector(this); addMouseListener(selector); } public void start() { if(!running) { running = true; thread = new Thread(this); thread.start(); } } public void stop() { running = false; if(thread != null) thread.interrupt(); thread = null; } public void paint(Graphics g) { if(buffer == null) { int w = getWidth(); int h = getHeight(); int type = BufferedImage.TYPE_INT_RGB; buffer = new BufferedImage(w, h, type); erase(w, h); } g.drawImage(buffer, 0, 0, this); } public void update(Graphics g) { paint(g); } public void run() { while(running) { repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e); } updateBuffer(); repaint(); } } private void updateBuffer() { int w = buffer.getWidth(); int h = buffer.getHeight(); erase(w, h); Graphics2D g2 = buffer.createGraphics(); for(int j = 0; j < tokens.size(); j++) { TokenStore token = tokens.get(j); if(token.isVisible()) token.advance(w, h); token.draw(g2); } g2.dispose(); } private void erase(int w, int h) { Graphics2D g2 = buffer.createGraphics(); g2.setPaint(getBackground()); g2.fillRect(0,0,w,h); g2.dispose(); } } class TokenSelector extends MouseAdapter { MovingSelection component; public TokenSelector(MovingSelection ms) { component = ms; } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); List<TokenStore> tokens = component.tokens; for(int j = 0; j < tokens.size(); j++) { TokenStore token = tokens.get(j); if(token.contains(p)) { token.select(); break; } } } } class TokenStore { Rectangle r = new Rectangle(20,20); Color color; int dx; int dy; boolean isVisible = true; public TokenStore(int x, int y, Color color, int dx, int dy) { r.setLocation(x, y); this.color = color; this.dx = dx; this.dy = dy; } public void draw(Graphics2D g2) { g2.setPaint(color); if(isVisible) g2.fill(r); else g2.draw(r); } public void advance(int w, int h) { if(r.x + dx < 0 || r.x + r.width + dx > w) dx *= -1; if(r.y + dy < 0 || r.y + r.height + dy > h) dy *= -1; r.translate(dx, dy); } public void select() { isVisible = !isVisible; } public boolean isVisible() { return isVisible; } public boolean contains(Point p) { return r.contains(p); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-20-2008, 09:04 PM
Member
 
Join Date: Jan 2008
Posts: 5
Triss is on a distinguished road
Thank you very much for your help, but I'd prefer version without double buffering, because the code transforms into something I don't quite understand. I think there's another way to have the pictures removed, just within the code I showed...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-20-2008, 09:27 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
// <applet code="MovingSelection" width="400" height="400"></applet> import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.util.*; import java.util.List; public class MovingSelection extends Applet implements Runnable { List<TokenStore> tokens = new ArrayList<TokenStore>(); Thread thread; boolean running = false; public void init() { tokens.add(new TokenStore(40, 40, Color.red, 1, 3)); tokens.add(new TokenStore(140, 100, Color.green, 2, 2)); tokens.add(new TokenStore(300, 200, Color.blue, 3, 2)); tokens.add(new TokenStore(100, 325, Color.cyan, 1, 2)); tokens.add(new TokenStore(220, 250, Color.orange, 2, 3)); TokenSelector selector = new TokenSelector(this); addMouseListener(selector); } public void start() { if(!running) { running = true; thread = new Thread(this); thread.start(); } } public void stop() { running = false; if(thread != null) thread.interrupt(); thread = null; } public void paint(Graphics g) { g.setColor(getBackground()); g.fillRect(0,0,getWidth(), getHeight()); for(int j = 0; j < tokens.size(); j++) { TokenStore token = tokens.get(j); token.draw((Graphics2D)g); } } public void run() { while(running) { try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println(e); } for(int j = 0; j < tokens.size(); j++) { TokenStore token = tokens.get(j); token.advance(getWidth(), getHeight()); } repaint(); } } } class TokenSelector extends MouseAdapter { MovingSelection component; public TokenSelector(MovingSelection ms) { component = ms; } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); List<TokenStore> tokens = component.tokens; for(int j = 0; j < tokens.size(); j++) { TokenStore token = tokens.get(j); if(token.contains(p)) { tokens.remove(token); break; } } } } class TokenStore { Rectangle r = new Rectangle(20,20); Color color; int dx; int dy; public TokenStore(int x, int y, Color color, int dx, int dy) { r.setLocation(x, y); this.color = color; this.dx = dx; this.dy = dy; } public void draw(Graphics2D g2) { g2.setPaint(color); g2.fill(r); } public void advance(int w, int h) { if(r.x + dx < 0 || r.x + r.width + dx > w) dx *= -1; if(r.y + dy < 0 || r.y + r.height + dy > h) dy *= -1; r.translate(dx, dy); } public boolean contains(Point p) { // return r.contains(p); return p.x > r.x && p.x < (r.x + r.width) && p.y > r.y && p.y < (r.y + r.height); } }
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
Converting multiple banded image into single banded image... Image enhancement archanajathan Advanced Java 0 01-08-2008 06:29 PM
Removing components from JPanel Echilon New To Java 0 12-30-2007 05:05 PM
Removing characters kDude New To Java 3 12-03-2007 03:38 AM
removing duplicates from arrays bugger New To Java 3 11-13-2007 07:11 PM
removing an object from memory ravian New To Java 1 11-12-2007 10:23 PM


All times are GMT +3. The time now is 05:11 PM.


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