Skip to content

Instantly share code, notes, and snippets.

@Announcement
Created May 14, 2014 20:46
Show Gist options
  • Save Announcement/2db43aecd10a8f96f3fa to your computer and use it in GitHub Desktop.
Save Announcement/2db43aecd10a8f96f3fa to your computer and use it in GitHub Desktop.
GUI Online.
import java.net.*;
import java.io.*;
public class Client {
public static void main(String[] args) throws IOException {
while(true)
{
String sentence;
String modifiedSentence;
BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
}
}
import java.io.*;
import java.text.*;
import java.lang.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class JFrameClickEventExample
extends JFrame
implements MouseListener, MouseMotionListener, KeyListener
{
public static BufferedImage scrnsht;
public static ArrayList<String> drawlog = new ArrayList<String>();
public BufferedImage ScreenShot() {
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();
Rectangle allScreenBounds = new Rectangle();
for (GraphicsDevice screen : screens) {
Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
allScreenBounds.width += screenBounds.width;
allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height);
}
Robot robot = new Robot();
return robot.createScreenCapture(allScreenBounds);
}catch(Exception ex){
BufferedImage bi = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.setColor(Color.red);
g.fillRect(300, 350, 100, 50);
return bi;
}
}
public class DrawPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
g.drawImage(scrnsht,0,0,this);
for (int i = drawlog.size();i-->0;){
g.drawString(drawlog.get(i),10,(i+1)*32);
}
/*Timer timer = new Timer("MyTimer");
timer.schedule(new TimerTask() {
@Override
public void run() {
again();
System.out.println("hello");
}
}, 1000);*/
}
}
public JLabel label;
public static void main(String args[])
{
new JFrameClickEventExample();
}
JFrameClickEventExample()
{
setSize(500, 400);
setTitle("Java Swing - JFrame Detect Mouse Events");
label = new JLabel("No Mouse Event Captured", JLabel.CENTER);
add(label);
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setVisible(true);
setAlwaysOnTop(true);
DrawPanel panel = new DrawPanel(); // window for drawing
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set frame to exit
// when it is closed
add(panel);
}
public void mousePressed(MouseEvent evt) {
}
public void mouseDragged(MouseEvent evt) {
System.out.println("Derp");
}
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { } // Other methods in the MouseListener interface
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseMoved(MouseEvent evt) { /*label.setText("Moving"); */} // Required by the MouseMotionListener interface.
String input = "";
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER){
drawlog.add(input);
input = "";
}else if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE&&input.length()>0){
input = input.substring(0,input.length()-1);
}else if(e.getKeyCode()==KeyEvent.VK_SHIFT){
scrnsht=ScreenShot();
}else if(e.getKeyChar()==KeyEvent.VK_PRINTSCREEN){
scrnsht=ScreenShot();
}else{
input += e.getKeyChar();
}
label.setText(input);
}
public void keyReleased(KeyEvent e) { }
}
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Received: " + clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment