Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-26-2009, 07:05 AM
Member
 
Join Date: Apr 2009
Posts: 5
Rep Power: 0
jimmy7 is on a distinguished road
Default [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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-26-2009, 04:53 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-26-2009, 07:00 PM
Member
 
Join Date: Apr 2009
Posts: 5
Rep Power: 0
jimmy7 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-26-2009, 11:23 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-26-2009, 11:25 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-27-2009, 12:16 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
For any interested, here is Jimmy's recent code:
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);

  }
}
I would ask that he post his corrected code at some point for others to see.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-27-2009, 12:40 AM
Member
 
Join Date: Apr 2009
Posts: 5
Rep Power: 0
jimmy7 is on a distinguished road
Default
Thanks Fuberable for all help of yours. Anyway ...Here's my final code if any one interested.

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 < 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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Drawing points on a JPanel josephdcoleman New To Java 6 02-25-2009 04:47 PM
Percentage/decimal points? Exhonour New To Java 6 01-16-2009 11:35 PM
given number of points(cordinates) , find max points lie on the same line ? Hayzam New To Java 2 08-24-2008 01:30 AM
Problem using buttons to creat a magic square game goldman New To Java 5 05-05-2008 05:04 AM
Incompatible magic value 1008821359 willemjav Java Applets 2 03-21-2008 10:41 AM


All times are GMT +2. The time now is 07:13 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org