Home > Enterprise >  Pushing another object in java.awt.Graphics
Pushing another object in java.awt.Graphics

Time:03-25

I have managed to push the other object but not the way I want it to, what I was trying to do is to push the other object towards the direction where I am pushing to other object like when you push a box or something. But in this case, I can't quite get how to push the other object to the same direction I am pushing it.

In this case: Box 1 pushing Box 2 from the -x(left) direction so the Box 2 will go towards the x(right) direction and same with box 1 pushing box 2 from the -y(down) direction then box 2 will go towards y(up) direction.

Here is the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Main2 extends JFrame implements KeyListener, ActionListener {

    public static int x = 0;
    public static int y = 0;
    public static int x2 = 100;
    public static int y2 = 100;

    public Main2() {
        add(new panel());

    }

    public static void main(String[] args) {
        Main2 test = new Main2();
        test.setTitle("TEST");
        test.setSize(Toolkit.getDefaultToolkit().getScreenSize());
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setVisible(true);
        test.addKeyListener(test);
    }

    public class panel extends JPanel {

        public panel() {
            Container c = getContentPane();
            c.setBackground(Color.white);

        }

        public void paint(Graphics g) {
            super.paint(g);

            object1(g, x, y);
            g.setColor(Color.RED);
            object2(g, x2, y2);
            
            Rectangle object1 = new Rectangle(x, y, 25, 25);
            Rectangle object2 = new Rectangle(x2, y2, 50, 50);
            object1.contains(x, y);
            object2.contains(x2, y2);

            
            if (object1.intersects(object2.getX(), object2.getX(), object2.getX(), object2.getX())) {
                x2  = 50;
                y2  = 0;
            }
            pause(1);

            repaint();
       
        }
    }

    public static void pause(int time) {

        try 
        {
            Thread.sleep(time);
        } catch (InterruptedException e) {
        }  
    }

    public void actionPerformed(ActionEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {

        if (e.getKeyCode() == e.VK_RIGHT) {
            x  = 20;
            repaint();
        }
        if (e.getKeyCode() == e.VK_LEFT) {
            x -= 20;
            repaint();
        }
        if (e.getKeyCode() == e.VK_UP) {
            y -= 20;
            repaint();
        }
        if (e.getKeyCode() == e.VK_DOWN) {
            y  = 20;
            repaint();
        }
    }

    public void object1(Graphics g, int x, int y) {

        g.fillRect(x, y, 30, 30);
    }
    
    public void object2(Graphics g, int x, int y) {
        g.fillRect(x2, y2, 50, 50);
    }
}

CodePudding user response:

One option would be to keep track of the direction the box is moving:

enum Direction {NONE, LEFT, RIGHT, UP, DOWN};
Direction dir = Direction.NONE;

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == e.VK_RIGHT) {
        x  = 20;
        dir = Direction.RIGHT;
        repaint();
    }
    if (e.getKeyCode() == e.VK_LEFT) {
        x -= 20;
        dir = Direction.LEFT;
        repaint();
    }
    if (e.getKeyCode() == e.VK_UP) {
        y -= 20;
        dir = Direction.UP;
        repaint();
    }
    if (e.getKeyCode() == e.VK_DOWN) {
        y  = 20;
        dir = Direction.DOWN;
        repaint();
    }
}

Then, when you intersect with the other rectangle, move accordingly:

if (object1.intersects(object2)) {
    if (dir == Direction.RIGHT) x2  = 50;
    else if (dir == Direction.LEFT) x2 -= 50;
    else if (dir == Direction.DOWN) y2  = 50;
    else if (dir == Direction.UP) y2 -= 50;
}
  •  Tags:  
  • java
  • Related