Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. // imports not included
  2.  
  3. public class ReflectionExample {
  4. public static void main(String[] args) {
  5. JarClassLoader loader = new JarClassLoader(myJarPath);
  6. Class<?> clientMainClass = loader.loadClass("MAIN-CLIENT-CLASS-GOES-HERE");
  7. Method clientMainMethod = clientMainClass.getDeclaredMethod("main", new Class<?>[]{String[].class}); // get client main(String[] args) method
  8.  
  9. JFrame controlPanel = new JFrame("Control Panel");
  10. JButton b=new JButton("Click Me");
  11. b.setBounds(0, 0, 100, 100);
  12. b.addActionListener(new ActionListener() { // add click handler to button
  13. public void actionPerformed(ActionEvent e) {
  14. for (Field f : clientMainClass.getDeclaredFields()) { // loop over all fields
  15. if (f.getType().equals(JFrame.class)) { // check if field type is JFrame
  16. System.out.println("Found field: " + f.getName());
  17. JFrame gameFrame = f.get(null); // get static field
  18. Point screenLoc = gameFrame.getLocationOnScreen();
  19. Robot robot = new Robot();
  20. Color targetColor = new Color(255, 0, 0); // the color you are searching for
  21. for (int y = 0; y < gameFrame.getHeight(); y++) {
  22. for (int x = 0; x < gameFrame.getWidth(); x++) {
  23. int screenX = (int)(screenLoc.getX() + x);
  24. int screenY = (int)(screenLoc.getY() + y)
  25. Color c = robot.getPixelColor(screenX, screenY);
  26. if (c.equals(targetColor)) { // found target color so click
  27. robot.mouseMove(screenX, screenY);
  28. robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  29. robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  30.  
  31. Thread.sleep(1000); // Wait for 1 second (this has an uncaught exception, I just left it out to keep the example shorter)
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. });
  39.  
  40. frame.add(b);
  41.  
  42. frame.setSize(200, 200);
  43. frame.setLayout(null);
  44. frame.setVisible(true);
  45.  
  46. clientMainMethod.invoke(null, new Object[]{new String[0])); // start the client
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement