Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 06-30-09   #1 (permalink)
*cough* Stock *cough*
 
_Trev's Avatar
 
intel nvidia

Join Date: Jun 2008
Location: Flordia
Posts: 165

Rep: 12 _Trev Unknown
Unique Rep: 12
Trader Rating: 0
Default Java, improving my Mouse class.

I now have a successful program that just moves the mouse around Humanlike (does not 'teleport' around the screen) depending on what the user input is. No fancy forms or anything yet though, I don't know how to do those yet anyways

"Mouse" class (need the java file below to test this)
Code:
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.MouseInfo;


public class Mouse{
  
  int mouseSpeed, cX, cY;  //cX = current X location
  public Mouse(int mSpeed)
  {
      mouseSpeed = mSpeed;
      cX = 0;
      cY = 0;
  }

  public double hypot(double s1, double s2)
  {
      return Math.sqrt(Math.pow(s1, 2) + Math.pow(s2, 2)); 
  }
    
  public void windMouse(double xs, double ys, double xe, double ye, double gravity, double wind, double minWait, double maxWait, double maxStep, double targetArea) throws Exception
  {
    double veloX=0, veloY=0, windX=0, windY=0, veloMag, dist, randomDist, lastDist, step;
    int lastX, lastY;
    double sqrt2, sqrt3, sqrt5;
    Robot r = new Robot();
    
    sqrt2 = Math.sqrt(2);
    sqrt3 = Math.sqrt(3);
    sqrt5 = Math.sqrt(5);

    while(hypot(xs - xe, ys - ye) > 1)
    {
        dist = hypot(xs - xe, ys - ye);
        wind = Math.min(wind, dist);
        
        if(dist > targetArea)
        {
            windX = windX / sqrt3 + (((int)(Math.random() * Math.round(wind)) * 2 + 1)  - wind) / sqrt5;
            windY = windY / sqrt3 + (((int)(Math.random() * Math.round(wind)) * 2 + 1)  - wind) / sqrt5;
        }
          else
          {
            windX = windX / sqrt2;
            windY = windY / sqrt2;
            
            if(maxStep < 3)
            {
                maxStep = (int)(Math.random() * 3) + 3;
            }
              else
              {
                  maxStep = maxStep / sqrt5;
              }
          }
          
        veloX = veloX + windX;
        veloY = veloY + windY;
        veloX = veloX + gravity * (xe - xs) / dist;
        veloY = veloY + gravity * (ye - ys) / dist;
        
        if(hypot(veloX, veloY) > maxStep)
        {
            randomDist = maxStep / 2.0 + (int)(Math.random() * (Math.round(maxStep) / 2));
            veloMag = Math.sqrt(veloX * veloX + veloY * veloY);
            veloX = (veloX / veloMag) * randomDist;
            veloY = (veloY / veloMag) * randomDist;    
        }

        lastX = (int)Math.round(xs);
        lastY = (int)Math.round(ys);
        xs = (xs + veloX);
        ys = (ys + veloY);
    
        if((lastX != Math.round(xs)) || (lastY != Math.round(ys)))
        {
          r.mouseMove(Math.round((int)xs), Math.round((int)ys));
        }
        
        step = hypot(xs - lastX, ys - lastY);
        r.delay((int)Math.round((maxWait - minWait) * (step / maxStep) + minWait));
        lastDist = dist;
    }
  
    if((Math.round(xe) != Math.round(xs)) || (Math.round(ye) != Math.round(ys)))    
    {
      r.mouseMove(Math.round((int)xe), Math.round((int)ye));
    }
    
  }
  
  
  public int posX()
  {
      PointerInfo pInfo = MouseInfo.getPointerInfo();      
      Point mouseLocation = new Point(pInfo.getLocation());
      return (int)mouseLocation.getX();
  }
  
  public int posY()
  {
      PointerInfo pInfo = MouseInfo.getPointerInfo();      
      Point mouseLocation = new Point(pInfo.getLocation());
      return (int)mouseLocation.getY();
  }
  
  public void getPos()
  {
      cX = posX();
      cY = posY();
  }
  
  public void move(int x, int y, int rx, int ry) throws Exception
  {
      
      int cx, cy;
      double randSpeed = ((Math.random() * mouseSpeed) / 2 + mouseSpeed) / 10;
      
      if(randSpeed == 0)
        randSpeed = 0.1;
      
      getPos();
      
      x = x + (int)(Math.random() * rx);
      y = y + (int)(Math.random() * ry);
      
      windMouse(cX, cY, x, y, 9.0, 3.0, (10.0/randSpeed), (15.0/randSpeed), (10.0*randSpeed), (10.0*randSpeed));
                  
  }
  
  public void click(boolean left)throws Exception
  {
    int counter = 0;
      
    Robot r = new Robot();
    if(left)
    {
      r.mousePress(InputEvent.BUTTON1_MASK);
      
      do
      {
        r.delay((int)(Math.random() * 10) + 5);
        counter ++;
      }
      while(counter < 4);
      
      r.mouseRelease(InputEvent.BUTTON1_MASK);
    }else{
        
      r.mousePress(InputEvent.BUTTON3_MASK);
      
      do
      {
        r.delay((int)(Math.random() * 10) + 50);
        counter ++;
      }
      while(counter < 4);
      
      r.mouseRelease(InputEvent.BUTTON3_MASK);    
        
    }   
  }
  
  
}
and the tester for the "Mouse" class.

Code:
/**
 * Write a description of class Tester here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tester
{
    public static void main(String[] args)  throws Exception
    {
        Mouse mouse = new Mouse(15);
        
        for(int x = 0; x <= 30; x ++)
        {
           mouse.move(0, 0, 500, 600);
        }
    }
}

I just want to know, is there a simple way for me to move the mouse in the background? and make it to where I can still use OCN while java paints me a beautiful picture on MS paint?

And if it is not simple, but there is a way that you know of. I am willing to learn!

Thank you
__________________
"_Trev"

System: Dell w/ 8800
CPU
Intel Pentium (R)
Motherboard
A Dell MoBo
Memory
2GB
Graphics Card
XFX GeForce 8800 GS
Hard Drive
250GB samsung
Sound Card
on board.
Power Supply
Dell stock
Case
Dimension 5150/E510 - stock case.
CPU cooling
stock heatsink / fan
GPU cooling
Stock FAN
OS
Windows XP Media Center.
Monitor
Dual 22 inch Dell monitors
_Trev is offline   Reply With Quote
Old 06-30-09   #2 (permalink)
Say what?
 
r34p3rex's Avatar
 
intel ati

Join Date: Jun 2007
Location: Boston/Long Island, NY
Posts: 3,533
Blog Entries: 1

Rep: 332 r34p3rex is a proven memberr34p3rex is a proven memberr34p3rex is a proven memberr34p3rex is a proven member
Unique Rep: 165
Folding Team Rank: 1240
Trader Rating: 27
Default

By moving in the background, do you mean having it draw in the background while OCN is open full screen? I don't know much about java, but maybe you can make it so that half you screen is a browser window and the other half is paint. Whenever the script detects mouse movement (you moving the mouse to click on something) it will pause until the mouse stops moving for 2 seconds. It will then resume the drawing - moving the mouse back to mspaint and continuing on.

How to do that? I have no idea

Good luck!

System: Project Epic
CPU
Core i7 920 D0 3943A529 @ 4.0 | 1.23v
Motherboard
Rampage II GENE
Memory
12GB OCZ Gold 1600
Graphics Card
GF100 or HD5870
Hard Drive
2x 60GB OCZ Vertex RAID 0 / 1TB Black
Sound Card
-Undecided-
Power Supply
Corsair TX750w
Case
P180 Mini
CPU cooling
Corsair H50
OS
Windows 7 Ultimate x64
Monitor
-Undecided-
r34p3rex is online now I fold for Overclock.net Overclocked Account   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 08:06 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.13877 seconds with 8 queries