Results 1 to 2 of 2

Thread: Check if item.

  1. #1
    Flopet17 is offline Member
    Join Date
    Mar 2014
    Posts
    60
    Rep Power
    0

    Red face Check if item.

    Hello guys,
    I made a small aplication which draws 3 rectangles in different spots(0,0 , 50,50 , 100,100)
    Am I used an KeyListener + ActionListener, to make them move in the let's call it box.
    How can I do in order that those 3 elements do not cross, so you can always see them, they don't collapse into each other?
    Thank you
    Java Code:
    package matrixmoveelements;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import javax.swing.JComponent;
    import javax.swing.Timer;
    
    public class Box extends JComponent implements KeyListener,ActionListener{
        Timer t = new Timer(5,this);
        private int x1=0, y1=0, x2=50, y2=50, x3=100, y3=100;
        private int velX1=0, velY1=0, velX2=0, velY2=0, velX3=0, velY3=0;
        private int a1=0, b1=10, a2=50, b2=60, a3=100, b3=110;
        
        public Box() {
        t.start();
        addKeyListener(this);
        setFocusable(true);
    }
        
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.red);
            g.fillRect(x1, y1+10, 50, 50);
            g.setColor(Color.black);
            g.drawRect(x1, y1+10, 50, 50);
            g.setColor(Color.blue);
            g.fillRect(x2, y2+10, 50, 50);
            g.setColor(Color.black);
            g.drawRect(x2, y2+10, 50, 50);
            g.setColor(Color.green);
            g.fillRect(x3, y3+10, 50, 50);
            g.setColor(Color.black);
            g.drawRect(x3, y3+10, 50, 50);
            g.drawString("(0.0)", a1, b1);
            g.drawString("(0.50)", a2, b1);
            g.drawString("(0.100)", a3, b1);
            g.drawString("(50.0)", a1, b2);
            g.drawString("(50.50)", a2, b2);
            g.drawString("(50.100)", a3, b2);
            g.drawString("(100.0)", a1, b3);
            g.drawString("(100.50)", a2, b3);
            g.drawString("(100.100)", a3, b3);
        }
        
        @Override
        public void keyTyped(KeyEvent ke) {}
    
        @Override
        public void keyPressed(KeyEvent ke) {
            int code = ke.getKeyCode();
            if(code == KeyEvent.VK_UP){
                velX1 = 0;
                velY1 = -1;
                
                velX2 = 0;
                velY2 = -1;
                        
                velX3 = 0;
                velY3 = -1;
                System.out.println("UP");
            }
            if(code == KeyEvent.VK_DOWN){
                velX1 = 0;
                velY1 = +1;
                
                velX2 = 0;
                velY2 = +1;
                
                velX3 = 0;
                velY3 = +1;
                System.out.println("DOWN");
            }
        
            if(code == KeyEvent.VK_LEFT){
                velX1 = -1;
                velY1 = 0;
                
                velX2 = -1;
                velY2 = 0;
                
                velX3 = -1;
                velY3 = 0;
                System.out.println("LEFT");
            }
        
            if(code == KeyEvent.VK_RIGHT){
                velX1 = +1;
                velY1 = 0;
                
                velX2 = +1;
                velY2 = 0;
                
                velX3 = +1;
                velY3 = 0;
                System.out.println("RIGHT");
            }
        }
    
        @Override
        public void keyReleased(KeyEvent ke) 
        {
            
        }
        
        @Override
        public void actionPerformed(ActionEvent ae) 
        {   
            if(x1<0)
            {
                x1=0;
                velX1=0;
            }
            if(x1>100)
            {
                x1=100;
                velX1=0;
            }
            if(y1<0)
            {
                y1=0;
                velY1=0;
            }
            if(y1>100)
            {
                y1=100;
                velY1=0;
            }
            if(x2<0)
            {
                x2=0;
                velX2=0;
            }
            if(x2>100)
            {
                x2=100;
                velX2=0;
            }
            if(y2<0)
            {
                y2=0;
                velY2=0;
            }
            if(y2>100)
            {
                y2=100;
                velY2=0;
            }
            if(x3<0)
            {
                x3=0;
                velX3=0;
            }
            if(x3>100)
            {
                x3=100;
                velX3=0;
            }
            if(y3<0)
            {
                y3=0;
                velY3=0;
            }
            if(y3>100)
            {
                y3=100;
                velY3=0;
            }
            x1 += velX1;
            y1 += velY1;
            x2 += velX2;
            y2 += velY2;
            x3 += velX3;
            y3 += velY3;
            repaint();
        }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Check if item.

    Rectangles implement the Shape interface; the Shape interface defines the intersects( ... ) method; read the API documentation for those classes/interfaces as well as the API documentation for the Graphics2D class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 14
    Last Post: 03-28-2014, 09:46 AM
  2. Replies: 10
    Last Post: 09-04-2012, 08:12 AM
  3. How to add item on list
    By Aenohe in forum New To Java
    Replies: 4
    Last Post: 12-08-2011, 11:05 PM
  4. Item Flickering
    By vincy in forum CLDC and MIDP
    Replies: 0
    Last Post: 10-25-2011, 07:48 AM
  5. Getting an item from an array
    By ile4 in forum New To Java
    Replies: 9
    Last Post: 01-13-2011, 03:42 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
  •