Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // imports not included
- public class ReflectionExample {
- public static void main(String[] args) {
- JarClassLoader loader = new JarClassLoader(myJarPath);
- Class<?> clientMainClass = loader.loadClass("MAIN-CLIENT-CLASS-GOES-HERE");
- Method clientMainMethod = clientMainClass.getDeclaredMethod("main", new Class<?>[]{String[].class}); // get client main(String[] args) method
- JFrame controlPanel = new JFrame("Control Panel");
- JButton b=new JButton("Click Me");
- b.setBounds(0, 0, 100, 100);
- b.addActionListener(new ActionListener() { // add click handler to button
- public void actionPerformed(ActionEvent e) {
- for (Field f : clientMainClass.getDeclaredFields()) { // loop over all fields
- if (f.getType().equals(JFrame.class)) { // check if field type is JFrame
- System.out.println("Found field: " + f.getName());
- JFrame gameFrame = f.get(null); // get static field
- Point screenLoc = gameFrame.getLocationOnScreen();
- Robot robot = new Robot();
- Color targetColor = new Color(255, 0, 0); // the color you are searching for
- for (int y = 0; y < gameFrame.getHeight(); y++) {
- for (int x = 0; x < gameFrame.getWidth(); x++) {
- int screenX = (int)(screenLoc.getX() + x);
- int screenY = (int)(screenLoc.getY() + y)
- Color c = robot.getPixelColor(screenX, screenY);
- if (c.equals(targetColor)) { // found target color so click
- robot.mouseMove(screenX, screenY);
- robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
- robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
- Thread.sleep(1000); // Wait for 1 second (this has an uncaught exception, I just left it out to keep the example shorter)
- }
- }
- }
- }
- }
- }
- });
- frame.add(b);
- frame.setSize(200, 200);
- frame.setLayout(null);
- frame.setVisible(true);
- clientMainMethod.invoke(null, new Object[]{new String[0])); // start the client
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement