What I want is that when I press the arc shape(in this case the pacman)and collided with the oval shape(its food) the oval shape will randomly go display to another location in the frame.
-
1So, just like your previous question, you need to detect when the position of the player intersects with your food. When that happens, you need to (among other things) generate a new `xrandomLocation` and `yrandomLocation` position, probably using `Random`... – MadProgrammer Feb 12 '16 at 02:21
-
`if(true){` ... which is always... – MadProgrammer Feb 12 '16 at 05:55
-
@MadProgrammer ah okay yeah it's always true. What will I put then if it's not true. – Jay Gorio Feb 12 '16 at 05:59
-
Do you understand the basic concept of calling a method? Do you understand how to deal with the return result of a method? The `intersect` method will return `true` or `false` depending on whether the player intersects the food (although, `contains` might be more accurate). You need to use the return value from this method to make decisions about what should be done. – MadProgrammer Feb 12 '16 at 06:02
-
@MadProgrammer yeah. Which I did right. – Jay Gorio Feb 12 '16 at 06:09
-
Nope, you ignored the return result from the `intersect` method – MadProgrammer Feb 12 '16 at 06:10
-
@MadProgrammer Okay I tried printing out the result when calling the intersect and it returns false. But how come the food randomly changed its position even theirs no collission happenedd. – Jay Gorio Feb 12 '16 at 06:15
-
Because `if (true)` is always `true` not matter what – MadProgrammer Feb 12 '16 at 06:16
-
@MadProgrammer okay git it – Jay Gorio Feb 12 '16 at 06:16
-
1@MadProgrammer if(intersects()){//actin here}. – Jay Gorio Feb 12 '16 at 06:17
-
@MadProgrammer thank you so much – Jay Gorio Feb 12 '16 at 06:17
-
@MadProgrammer can you please offer suggestion for the affline. We are not allowed to used those. Are their any soulutions for that? – Jay Gorio Feb 12 '16 at 06:23
-
Not really, you'll have to calculate the offset from left and right yourself – MadProgrammer Feb 12 '16 at 06:34
2 Answers
The basic idea is to determine if the player intersects the food, something like...
public boolean intersects() {
int tx = xLocation;
int ty = yLocation;
int rx = xrandomLocation;
int ry = yrandomLocation;
int rw = 20 + rx;
int rh = 20 + ry;
int tw = 100 + tx;
int th = 100 + ty;
// overflow || intersect
return ((rw < rx || rw > tx)
&& (rh < ry || rh > ty)
&& (tw < tx || tw > rx)
&& (th < ty || th > ry));
}
When the method returns true, you want to calculate a new location off the food
Maybe something like...
private Random random = new Random();
//...
xrandomLocation = random.nextInt(getWidth()) - 20;
yrandomLocation = random.nextInt(getHeight()) - 20;
Java's 2D Graphics API has a really powerful shapes API which allows you to check for collisions more simply
You should also stop using KeyListener
API and start using the Key Bindings API, which help solve the focus related issues the KeyListener
suffers from.
Also, all the suggestions I made in your last question still stand

- 1
- 1

- 343,457
- 22
- 230
- 366
-
Thank you for the suggestions. My question is how will I call the instesects? I tried calling it in the keypressed but seems not working. – Jay Gorio Feb 12 '16 at 05:31
-
We are just limited to use KeyListener that's why I cannot use key bindings as for now. – Jay Gorio Feb 12 '16 at 05:32
-
-
it's working now but when I just press a key it randomly change. See edited answer on top. – Jay Gorio Feb 12 '16 at 05:51
Your structure is a bit off, There is no need for the PacmanObject to extend JPannel or implement KeyListener. Wherever your JPannel is, it will need to use the method addKeyListener, the KeyListener is usually anonymous. Yes you will need to check whether the food and the pacman are in the same position after every time the pacman moves. You will also need to generate a random position but neither of these things are hard. I have written a frame for you to write your code into, of course the nested classes should probably be moved to their own files.
package pacman;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Pacman extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Pacman frame = new Pacman();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Pacman() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new View();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
}
public class View extends JPanel {
PacmanObject p;
FoodObject f;
/**
* Create the panel.
*/
public View() {
super();
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
onKeyEvent(e);
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
public void onKeyEvent(KeyEvent keyPress) {
p.move(keyPress);
if (/* check if f and p colide*/true) {
f.reposition();
}
p.repaint(this.getGraphics());
f.repaint(this.getGraphics());
}
}
public class FoodObject {
// Fields
FoodObject() {
// init fields
}
public void repaint(Graphics graphics) {
// repaint
}
public void reposition() {
// generate random position
}
}
public class PacmanObject {
// Fields
PacmanObject() {
// init fields
}
public void repaint(Graphics graphics) {
// repaint
}
public void move(KeyEvent keyPress) {
/// Move pacman
}
}
}
Hope that helps

- 310
- 2
- 10
-
Mistake number one- propagating the use of `KeyListener` when better APIs exist; Mistake number two- using `getGraphics` – MadProgrammer Feb 12 '16 at 02:38
-
No mistake one would be trying to use swing in the first place. I'm just trying to help the guy get a working program. So what if its not using the most efficient API? – Devman Feb 12 '16 at 02:46
-
2*"So what if its not using the most efficient API?"* - Because it's propagating bad ideas and a solution which is going to, in the short and long term, cause more issues to the OP - a mess the rest of us we are going to have clean up. As professional forum, we should present professional solutions to people's problems, but of course, if you want to post rubbish, that's your choice – MadProgrammer Feb 12 '16 at 02:51
-
So tell us which API to use instead. Don't just go tell everyone they are wrong. Feel free to edit my answer until you are happy with it. – Devman Feb 12 '16 at 03:11
-
Well, start by taking a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) to see how painting should be done (ps- the OP's on the right track) and [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for a replacement for `KeyListener` and all it's problems – MadProgrammer Feb 12 '16 at 03:18
-
There you go, go ahead and use my answer if you like but please have a look at the documentation the @MadProgrammer suggested as it may save you some troubles down the road. – Devman Feb 12 '16 at 03:30