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
}