SHARE
TWEET

client

a guest Apr 21st, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Frame extends JFrame implements MouseListener, MouseMotionListener {
  2.  
  3.     /**
  4.      *
  5.      */
  6.     private static final long serialVersionUID = 1L;
  7.     int x = 200;
  8.     int x2 = 100;
  9.     int y = 200;
  10.     int y2 = 100;
  11.     int width = 50;
  12.  
  13.     int oldX, oldY;
  14.     int oldX2, oldY2;
  15.    
  16.     static Socket sock;
  17.     static BufferedReader in;
  18.     static PrintWriter out;
  19.    
  20.     boolean dragging = false;
  21.  
  22.     public static void main(String[] args) {
  23.         // TODO Auto-generated method stub
  24.         Frame mf = new Frame();
  25.         mf.setBounds(200,200,500,400);
  26.        
  27.         mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         mf.setVisible(true);
  29.         try {
  30.             sock = new Socket("localhost", 2000);
  31.             in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  32.             out = new PrintWriter(sock.getOutputStream(), true);
  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.         }
  36.         Thread severListener = new Thread(new Runnable() {
  37.            
  38.             @Override
  39.             public void run() {
  40.                 // TODO Auto-generated method stub
  41.                 try {
  42.                     String messageFromServer = in.readLine();
  43.                 } catch (IOException e) {
  44.                     // TODO Auto-generated catch block
  45.                     e.printStackTrace();
  46.                 }
  47.             }
  48.         });
  49.     }
  50.    
  51.     public Frame() {
  52.        
  53.         this.addMouseListener(this);
  54.         this.addMouseMotionListener(this);
  55.     }
  56.  
  57.     @Override
  58.     public void paint(Graphics g) {
  59.         // TODO Auto-generated method stub
  60.         super.paint(g);
  61.         g.setColor(Color.GREEN);
  62.         g.fillRect(x, y, width, width);
  63.  
  64.         g.setColor(Color.RED);
  65.         g.fillRect(x2, y2, width, width);
  66.     }
  67.    
  68.      
  69.  
  70.     @Override
  71.     public void mouseDragged(MouseEvent e) {
  72.         // TODO Auto-generated method stub
  73.         if (dragging) {
  74.             x = x + e.getX() - oldX;
  75.             y = y + e.getY() - oldY;
  76.  
  77.             oldX = e.getX();
  78.             oldY = e.getY();
  79.             repaint();
  80.         }
  81.  
  82.     }
  83.  
  84.     @Override
  85.     public void mouseMoved(MouseEvent e) {
  86.         // TODO Auto-generated method stub
  87.  
  88.     }
  89.  
  90.     @Override
  91.     public void mouseClicked(MouseEvent e) {
  92.         // TODO Auto-generated method stub
  93.  
  94.     }
  95.  
  96.     @Override
  97.     public void mouseEntered(MouseEvent e) {
  98.         // TODO Auto-generated method stub
  99.  
  100.     }
  101.  
  102.     @Override
  103.     public void mouseExited(MouseEvent e) {
  104.         // TODO Auto-generated method stub
  105.  
  106.     }
  107.  
  108.     @Override
  109.     public void mousePressed(MouseEvent e) {
  110.         // TODO Auto-generated method stub
  111.         int newX = e.getX();
  112.         int newY = e.getY();
  113.  
  114.         if (newX >= x && newX <= x + width && newY >= y && newY <= y + width) {
  115.             dragging = true;
  116.             oldX = newX;
  117.             oldY = newY;
  118.         }
  119.     }
  120.  
  121.     @Override
  122.     public void mouseReleased(MouseEvent e) {
  123.         // TODO Auto-generated method stub
  124.         dragging = false;
  125.  
  126.     }
  127.  
  128. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top