# Thread: [SOLVED] Magic Points Game

1. Member
Join Date
Apr 2009
Posts
5
Rep Power
0

## [SOLVED] Magic Points Game

Hi I am trying to make a program like the one i attached it (below link).

It is a game that randomly selects point and then user clicks on screen and short lines appear which shows direction to the those point. User keep trying until he finds those point.

I can't able to figure out how to draw just short line between two points. I don't want to draw full line from x1, y1, to x2, y2. I have user clicked point and points that randomly selected. thank you.

2shared.com/file/5497766/c8f01c68/Magic_Points.html
Last edited by jimmy7; 04-26-2009 at 11:19 PM.

2. I'm reluctant to go to a random site, but I think that your problem should be solvable with use of simple trigonometry. I don't know if this is the simplest solution, but it works.

For eg,
get the slope of the line from point a to point b via delta y / delta x.
use Math.atan(...) to get the angle of the slope, theta.
use (LINE_SEGMENT_LENGTH) * Math.cos(theta) + pointA.x to get the x position of the line segment's end point.
Similarly use Math.sin(...) to get the y position of the line segment's end point.
Draw a line from pointA to your line segment end points, and you're done.
Last edited by Fubarable; 04-26-2009 at 05:13 PM.

3. Member
Join Date
Apr 2009
Posts
5
Rep Power
0
thank you very much for replying. I added your suggestion in my code. It works. I added (.1 in delta x) so denominator does not become 0. Thank you very much for solving it.
Last edited by jimmy7; 04-26-2009 at 11:17 PM.

4. I suspect that your problem may be one of int division. Your slope needs to be a double, not an int, and in your division where you calculate the slope, you should cast either the numerator or the denominator to (double). If not, you'll be dividing an int by an int which has to give an int result. If the result is < 1, it will default to the next lower int or 0, giving you these flat lines.

5. LOL, I see that you posted as I was posting. I'm not sure that I'd add .1 to deltaX, but rather I'd cast it as I suggested above. Again, good luck.

6. For any interested, here is Jimmy's recent code:
Java Code:
```import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class HintsFor extends JApplet implements MouseListener
{

Point point[] = new Point[1000];
int desX[] = new int[1000];
int desY[] = new int[1000];
double theta[] = new double[1000];
double slope[] = new double[1000];

int count = 0;
int distance;
Random generator = new Random();
int randomX;
int randomY;

public void init()
{
randomX = generator.nextInt(getWidth());
randomY = generator.nextInt(getHeight());
}

public void mousePressed(MouseEvent e)
{

point[count] = new Point(e.getX(), e.getY());
distance = (int) Math.sqrt(Math.pow((randomX - e.getX()), 2)
+ Math.pow((randomY - e.getY()), 2));

slope[count] = (e.getY() - randomY) / (e.getX() - randomX);
theta[count] = Math.atan(slope[count]);
desX[count] = (int) (25 * Math.cos(theta[count]) + e.getX());
desY[count] = (int) (25 * Math.sin(theta[count]) + e.getY());

count++;
repaint();
if (distance <= 10)
{
"Do you want to play again?", "You Fount It in " + count
+ " Guesses!", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
{
randomX = generator.nextInt(getWidth());
randomY = generator.nextInt(getHeight());
count = 0;
repaint();
}
}
}

public void mouseReleased(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{
}

public void paint(Graphics g)
{
super.paint(g);

g.setColor(Color.red);
for (int n = 0; n < count; n++)
{
g.drawLine(point[n].x, point[n].y, desX[n], desY[n]);
}
}

public static void main(String[] args)
{

JFrame frame = new JFrame("Applet is in the frame");

HintsFor applet = new HintsFor();
applet.init();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);

}
}```
I would ask that he post his corrected code at some point for others to see.

7. Member
Join Date
Apr 2009
Posts
5
Rep Power
0
Thanks Fuberable for all help of yours. Anyway ...Here's my final code if any one interested.

Java Code:
```import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Hints extends JApplet implements MouseListener{

Point point[] = new Point[1000];
int desX[] = new int[1000];
int desY[] = new int[1000];
double theta[] = new double[1000];
double slope[] = new double[1000];
Object[] option = {"OK"};

int count = 0;
int distance;
Random generator = new Random();
int randomX;
int randomY;
boolean found=false, yes=false;

public void init() {
int ans1 = JOptionPane.showConfirmDialog(null,
"Would you like an explanation of this game",
"Welcome to MagicSpot", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (ans1 == JOptionPane.YES_OPTION) {
int ans2 =  JOptionPane.showOptionDialog(null, "Somewhere in this window there is a hidden" +
"magic spot.\nYour job is to find it. \n \nEach time you click the mouse, a short line segment" +
"\nwill be drawn at that point on the screen pointing \nto the hidden magic spot. When you click on" +
" the magic \nspot itself, the word \"*Magic!\" will be printed and \nyou will asked if you want to play again.",
"Explanation of the Game", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, option, option[0]);
yes = true;
} else yes = true;

if (yes) {
randomX = generator.nextInt(getWidth());
randomY = generator.nextInt(getHeight());
}
}

public void mousePressed(MouseEvent e) {

point[count] = new Point(e.getX(), e.getY());
distance = (int) Math.sqrt(Math.pow((randomX - e.getX()), 2) + Math.pow((randomY - e.getY()), 2));

slope[count] = (e.getY()-randomY)/((double)e.getX()-randomX);
theta[count] = Math.atan(slope[count]);
desX[count] = (int) (25 * Math.cos(theta[count]) + e.getX());
desY[count] = (int) (25 * Math.sin(theta[count]) + e.getY());
count++;

if (distance <= 10) {  //should change it to 10 from 5 so its easy to find magic spot.
found = true;
repaint();
int answer = JOptionPane.showConfirmDialog(null, "Do you want to play again?",
"You Fount It in " + count + " Guesses!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
randomX = generator.nextInt(getWidth());
randomY = generator.nextInt(getHeight());
count = 0;
repaint();
}
} else repaint();
}

public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
if (found) {
for (int n = 0; n < count-1; n++) {
g.drawLine(point[n].x, point[n].y, desX[n], desY[n]);
}
g.setColor(Color.blue);
g.drawString("*Magic!", point[count-1].x, point[count-1].y);
found = false;
g.setColor(Color.red);
} else {
for (int n = 0; n < count; n++) {
g.drawLine(point[n].x, point[n].y, desX[n], desY[n]);
}
}
}
}```
Last edited by jimmy7; 04-27-2009 at 01:39 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•