Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All Swing components have a concept of "bounds". This is a rectangular area within which they are "drawn".</p> <p>If you are controlling the size and position correctly, you should be able to use the <code>contains</code> method of the <code>Rectangle</code> which is returned from calling <a href="http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#getBounds%28%29" rel="nofollow"><code>Component#getBounds</code></a></p> <p>So you <code>checkOverlap</code> method could look like...</p> <pre><code>public void checkOverlap(BallComponent bc){ if (getBounds().intersects(bc.getBounds())){ bc.stopY(); bc.stopX(); } } </code></pre> <p>You will also want to make sure that you are calling <code>super.paintComponent</code> before performing any custom painting, espeically when using components that extend from <code>JComponent</code>. This will ensure that the <code>Graphics</code> context is prepared for painting correctly...</p> <p><strong>Updated</strong></p> <p>There's a cascade of issues. Basically, instead of positing the components within the parent container yourself (which is how I thought you had done it), you've laid each component out to fill the parent container and only "painted" the objects...This makes life more difficult</p> <p>Instead, if you are going to use components, I would use a <code>null</code> layout (or even possibly use a <code>JLayeredPane</code> as the parent container).</p> <p>I would then change the physical position of the components, for example...</p> <pre><code>import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.LayoutManager; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.OverlayLayout; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class TestGame { public static void main(String[] args) { new TestGame(); } public TestGame() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(null); 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(); } }); } 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) { } } public class BallComponent extends JComponent { int xSpeed; int ySpeed; public BallComponent(int x, int y) { super(); setBounds(x, y, 10, 10); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double ball = new Ellipse2D.Double(0, 0, 9, 9); 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() { int x = getX() + xSpeed; int y = getY() + ySpeed; setLocation(x, y); repaint(); } public void stopY() { ySpeed = 0; } public void stopX() { xSpeed = 0; } } public class WallComponent extends JComponent { public WallComponent(int x, int y) { super(); setBounds(x, y, 40, 40); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; Rectangle wall = new Rectangle(0, 0, 40, 40); g2.setColor(Color.YELLOW); g2.fill(wall); g2.draw(wall); } public void checkOverlap(BallComponent bc) { System.out.println(" me: " + getBounds()); System.out.println("you: " + bc.getBounds()); if (getBounds().intersects(bc.getBounds())) { bc.stopY(); bc.stopX(); } } } } </code></pre> <p>Now, you could use "painted" objects, but in that case I would have a virtual concept of a <code>Ball</code> and <code>Wall</code> which you could paint within a single component. These objects would need to provide information about there position and size, which you could, again, check using <code>Rectangle#intersects</code>...</p>
 

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