Results 1 to 4 of 4

Thread: image removing

  1. #1
    Triss is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default 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:

    Java 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. :)

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

    Default

    Looks okay but it's hard to say. You could try using a rectangle.
    Java 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); }
    }

  3. #3
    Triss is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default

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

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

    Default

    Java 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);
        }
    }

Similar Threads

  1. Replies: 0
    Last Post: 01-08-2008, 05:29 PM
  2. Removing components from JPanel
    By Echilon in forum New To Java
    Replies: 0
    Last Post: 12-30-2007, 04:05 PM
  3. Removing characters
    By kDude in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 02:38 AM
  4. removing duplicates from arrays
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 06:11 PM
  5. removing an object from memory
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-12-2007, 09:23 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
  •