0

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
}
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40
Amit
  • 157
  • 8
  • 2
    What does it do now? You say it doesn't work, but what happens instead of what you want to happen? – krodmannix Jul 07 '14 at 20:32
  • What kind of graphics are they (circles, squares, lines, etc.)? – BitNinja Jul 07 '14 at 20:34
  • right now the rectangle moves when you click the arrows on your keyboard and I want the circle to change locations when the rectangle is touching it. @kevinrmannix – Amit Jul 07 '14 at 20:35
  • circles and rectangles for the paint method in swing @BitNinja – Amit Jul 07 '14 at 20:35
  • Have you considered using a hitbox? – BitNinja Jul 07 '14 at 21:02
  • 1
    You should avoid painting directly to top level containers like JFrame, you should consider using a JPanel and overriding its paintComponent method instead. This adds automatic double buffering and re-usability. You should avoid using KeyListener as it tends to be to low level and suffers from focus related issues. You should consider using the [key bindings api](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) instead – MadProgrammer Jul 07 '14 at 21:06
  • If you make use of the available [Shapes](http://docs.oracle.com/javase/tutorial/2d/geometry/index.html), this functionality is built in... – MadProgrammer Jul 07 '14 at 21:08
  • what is a hitbox? @BitNinja – Amit Jul 07 '14 at 21:15
  • See [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example. – Andrew Thompson Jul 07 '14 at 23:57

1 Answers1

0

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());
BitNinja
  • 1,477
  • 1
  • 19
  • 25
  • I set up the hotbox but it is not working and when I make the line Eclipse2D = new Eclipse(x,y,50,50); then the program does not work. I've tried replacing Eclipse with Eclipse2D but the second Eclipse has an error and says "cannot instantiate the type – Amit Jul 08 '14 at 06:43
  • @user3782258 It's a bit different if you use an Eclipse, I edited the question to include using one. – BitNinja Jul 08 '14 at 18:34