Search code examples
javaswinguser-interfacerandompaint

Java method supposed to check whether graphics touching not working


My "checkiftouching" method is not working. It is suppose to change the location of the circle and give you a point when the square and circle are touching. It senses when they are touching by checking when there locations are close enough together. The rest of the program runs smoothly. It has a square that moves with the arrows.

import java.awt.Graphics;

public class RunPaintGUI extends JFrame implements KeyListener{
int x = 30;
int y = 30;
Random randomgenerator = new Random();
int a = randomgenerator.nextInt(1220);
int b = randomgenerator.nextInt(700);
  public static void main(String[] args){
    RunPaintGUI RunPaintGUI = new RunPaintGUI();}

public RunPaintGUI(){
  this.setSize(1275, 775);
this.setResizable(false);
this.setVisible(true);
this.setTitle("game")
this.addKeyListener(this);
}

public void paint(Graphics g){
  super.paint(g);
  g.fill3DRect(x,y, 60, 60, true);
  g.fillOval(a, b, 50, 50);
  g.drawString("score: " + score, 600, 50);

 }
public void checkiftouching(){
   if ((a - x) < 70){
      if ((a -x) > -70){
         if ((b - y) < 70){
            if ((b - y) > -70){
          System.out.println("you win");
          a = randomgenerator.nextInt(1220);
          b = randomgenerator.nextInt(720);
          repaint();
          score = score + 1;
}}}}}
  public void keyPressed(KeyEvent e) {
     if (e.getKeyCode() == KeyEvent.VK_LEFT){
       x = x - 10;
       repaint();
     else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
       x = x + 10;
       repaint();
     else if (e.getKeyCode() == KeyEvent.VK_UP){
       y = y - 10;
       repaint();
     else if (e.getKeyCode() == KeyEvent.VK_Left){
       y = y + 10;
       repaint();
 }
 }

@Override
public void keyTyped(KeyEvent e) {
     // TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e {
     // TODO Auto-generated method stub
}

Solution

  • It sounds like you need a hitbox. A hitbox, which is commonly used in game to detect collisions, can be used in your scenario. A hitbox is simply a rectangle that you don't draw but still keep track of its position. You should use a Rectangle for a hitbox. If you would prefer to use a ellipse for your cirlces though you can use a Ellipse2D. Although if you do this you will have to use a Rectangle2D.

    Rectangle hitbox = new Rectangle(x,y,width,height);
    //...
     if (e.getKeyCode() == KeyEvent.VK_LEFT){
       x = x - 10;
       hitbox.x = x;
       repaint();
    // and so on for your various key events
    

    Your checkiftouching() method gets much simpler with hitboxes.

     if (hitbox1.intersects(hitbox)) {
          System.out.println("you win");
          a = randomgenerator.nextInt(1220);
          b = randomgenerator.nextInt(720);
          repaint();
          score = score + 1;
    }
    

    If you choose to use an Ellipse2D then you must declare it like this:

    Ellipse2D.Double hitbox = new Ellipse2D.Double(x,y,width,height);
    

    Then you set the x coordinate:

    hitbox.setFrame(newXCoordinate, hitbox.getY(), hitbox.getWidth(), hitbox.getHeight());
    

    Or the y coordinate:

    hitbox.setFrame(hitbox.getX(), newYCoordinate, hitbox.getWidth(), hitbox.getHeight());