Results 1 to 7 of 7
Thread: [SOLVED] Magic Points Game
 04262009, 06:05 AM #1Member
 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.htmlLast edited by jimmy7; 04262009 at 10:19 PM.

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; 04262009 at 04:13 PM.
 04262009, 06:00 PM #3Member
 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; 04262009 at 10:17 PM.

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.

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.

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()); getContentPane().addMouseListener(this); } 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) { int answer = JOptionPane.showConfirmDialog(null, "Do you want to play again?", "You Fount It in " + count + " Guesses!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (answer == JOptionPane.YES_OPTION) { 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(); frame.getContentPane().add(applet, BorderLayout.CENTER); applet.init(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } }
 04262009, 11:40 PM #7Member
 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()); getContentPane().addMouseListener(this); } } 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); if (answer == JOptionPane.YES_OPTION) { 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 < count1; n++) { g.drawLine(point[n].x, point[n].y, desX[n], desY[n]); } g.setColor(Color.blue); g.drawString("*Magic!", point[count1].x, point[count1].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; 04272009 at 12:39 AM.
Similar Threads

Drawing points on a JPanel
By josephdcoleman in forum New To JavaReplies: 6Last Post: 02252009, 03:47 PM 
Percentage/decimal points?
By Exhonour in forum New To JavaReplies: 6Last Post: 01162009, 10:35 PM 
given number of points(cordinates) , find max points lie on the same line ?
By Hayzam in forum New To JavaReplies: 2Last Post: 08242008, 12:30 AM 
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05052008, 04:04 AM 
Incompatible magic value 1008821359
By willemjav in forum Java AppletsReplies: 2Last Post: 03212008, 09:41 AM
Bookmarks