|
|
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.
|
|

01-20-2008, 02:28 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 5
|
|
|
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:
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. 
|
|

01-20-2008, 08:53 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,124
|
|
Looks okay but it's hard to say. You could try using a rectangle.
// <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); }
}
|
|

01-20-2008, 09:04 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 5
|
|
|
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...
|
|

01-20-2008, 09:27 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,124
|
|
// <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);
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|