Note that there are some explanatory texts on larger screens.

plurals
  1. PODetect overlapping objects in java Swing
    primarykey
    data
    text
    <p>I am trying to make a program that has a moving ball and a platform that it will sit on. I am new too java and I cant figure out how to detect when 2 swing objects are overlapping. My code it below and I am wondering what the best way is to detect overlapping objects.</p> <p>KeyDemo.java:</p> <pre><code> import javax.swing.*; import java.awt.*; import java.awt.event.*; public class KeyDemo { public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); final int FRAME_WIDTH = 800; final int FRAME_HEIGHT = 600; frame.setSize(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("Move the Ball"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final WallComponent wc1 = new WallComponent(400, 400); final BallComponent bc = new BallComponent(400, 300); panel.add(wc1); panel.add(bc); frame.add(panel); KeyboardController kc = new KeyboardController(bc); frame.addKeyListener(kc); frame.setVisible(true); class AnimationListener implements ActionListener{ public void actionPerformed(ActionEvent event){ bc.tick(); //wc1.checkOverlap(bc); } } ActionListener aListener = new AnimationListener(); final Timer timer = new Timer(1, aListener); timer.start(); } } </code></pre> <p>KeyboardController.java:</p> <pre><code> import java.awt.*; import java.awt.event.*; public class KeyboardController implements KeyListener { BallComponent bComp; public KeyboardController(BallComponent t) { bComp = t; } /** Handle the key pressed event from the text field. */ public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == 38) { System.out.println("Pressed Up!"); bComp.moveUp(); } if(keyCode == 37) { System.out.println("Pressed Left!"); bComp.moveLeft(); } if(keyCode == 39) { System.out.println("Pressed Right!"); bComp.moveRight(); } if(keyCode == 40) { System.out.println("Pressed Down!"); bComp.moveDown(); } } /** Handle the key released event from the text field. */ public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == 38) { System.out.println("Released Up!"); bComp.stopY(); } if(keyCode == 37) { System.out.println("Released Left!"); bComp.stopX(); } if(keyCode == 39) { System.out.println("Released Right!"); bComp.stopX(); } if(keyCode == 40) { System.out.println("Pressed Down!"); bComp.stopY(); } } public void keyTyped(KeyEvent e) { } } </code></pre> <p>BallComponent.java:</p> <pre><code> import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class BallComponent extends JComponent { int xSpeed; int ySpeed; int x; int y; public BallComponent(int x, int y) { super(); this.x = x; this.y = y; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Double ball = new Ellipse2D.Double(x-10,y-10,10,10); g2.setColor(Color.RED); g2.fill(ball); g2.draw(ball); } public void moveLeft() { xSpeed=-1; } public void moveRight() { xSpeed=1; } public void moveUp() { ySpeed=-1; } public void moveDown() { ySpeed=1; } public void tick() { x=x+xSpeed; y=y+ySpeed; repaint(); } public void stopY() { ySpeed=0; } public void stopX() { xSpeed=0; } } </code></pre> <p>WallComponent.java:</p> <pre><code> import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.geom.*; public class WallComponent extends JComponent { int x; int y; public WallComponent(int x, int y) { super(); this.x = x; this.y = y; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g; Rectangle wall = new Rectangle(x-40,y-40,40,40); g2.setColor(Color.YELLOW); g2.fill(wall); g2.draw(wall); } public void checkOverlap(BallComponent bc){ if (this.contains(bc.getLocation())){ bc.stopY(); bc.stopX(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload