Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseListener;
  5. import java.awt.image.BufferStrategy;
  6.  
  7. import javax.swing.JFrame;
  8.  
  9.  
  10. public class Oving2 extends Canvas implements Runnable, MouseListener {
  11.  
  12.     /**
  13.      * Mucahit
  14.      */
  15.     private static final long serialVersionUID = 1L;
  16.     public static int width = 280;
  17.     public static int height = width;
  18.     public static int scale = 2;
  19.  
  20.     double posX = 290;
  21.     double posY = 290;
  22.     Rectangle rect = new Rectangle((int)posX, (int)posY, 30, 30);
  23.     boolean mousePressed = false;
  24.     //PointerInfo p = MouseInfo.getPointerInfo();
  25.  
  26.     private Thread thread;
  27.     public boolean running = false;
  28.  
  29.     private JFrame frame;
  30.  
  31.  
  32.     public Oving2() {
  33.         Dimension fsize = new Dimension(width * scale, height * scale);
  34.         this.addMouseListener(this);
  35.         setPreferredSize(fsize);
  36.  
  37.         frame = new JFrame();
  38.     }
  39.  
  40.     public synchronized void start() {
  41.         running = true;
  42.         thread = new Thread(this);
  43.         thread.start();
  44.     }
  45.  
  46.     public synchronized void stop() {
  47.         running = false;
  48.         try {
  49.             thread.join();
  50.         } catch (InterruptedException e) {
  51.             e.printStackTrace();
  52.         }
  53.  
  54.     }
  55.  
  56.     @Override
  57.     public void run() {
  58.  
  59.         while (running) {
  60.             if(this.getMousePosition() != null) {
  61.                 update();      //SMOOTH MOVE
  62.             }
  63.             render();     //RENDER AS FAST AS POSSIBUL!
  64.         }
  65.         stop();
  66.     }
  67.  
  68.     private void render() {
  69.  
  70.         BufferStrategy bs = getBufferStrategy();
  71.  
  72.         if (bs == null) {
  73.             createBufferStrategy(3);
  74.             return;
  75.         }
  76.  
  77.  
  78.         Graphics g = bs.getDrawGraphics();
  79.         //
  80.         Graphics2D g2 = (Graphics2D) g;
  81.         super.paint(g2);
  82.         //
  83.         g2.fillRect(200, 200, 200, 200);
  84.         g2.setColor(Color.white);
  85.         //X1, Y1, X2, Y2
  86.         g2.drawLine(200, 300, (int)posX, (int)posY + 10);
  87.         g2.drawLine((int) posX + 20, (int) posY + 10, 400, 300);
  88.         g2.drawOval((int) posX, (int) posY, 20, 20);
  89.         rect.setLocation((int)posX, (int)posY);
  90.         //
  91.         g.dispose();
  92.         bs.show();
  93.     }
  94.  
  95.     private void update() {
  96.        // System.out.println("updating");
  97.  
  98.  
  99.             int x = (int) this.getMousePosition().getX();
  100.             int y = (int) this.getMousePosition().getY();
  101.  
  102.         if (rect.contains(x + 10, y + 10)) {
  103.             setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  104.         }
  105.         else{
  106.             setCursor(Cursor.getDefaultCursor());
  107.         }
  108.  
  109.             if (mousePressed) {
  110.                 //System.out.println("x : " + x + "   y :" + y);
  111.                 if (rect.contains(x + 10, y + 10)) {
  112.  
  113.                     posY = y;
  114.                     posX = x;
  115.  
  116.                 }
  117.             } else {
  118.                 posY = 290;
  119.                 posX = 290;
  120.             }
  121.     }
  122.  
  123.     public static void main(String[] args) {
  124.         Oving2 oving2 = new Oving2();
  125.         oving2.frame.setResizable(false);
  126.         oving2.frame.setTitle("Frame");
  127.         oving2.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  128.         oving2.frame.add(oving2);
  129.         oving2.frame.pack();
  130.         oving2.frame.setLocationRelativeTo(null);
  131.         oving2.frame.setVisible(true);
  132.  
  133.         oving2.start();
  134.     }
  135.  
  136.     @Override
  137.     public void mouseClicked(MouseEvent e) {
  138.  
  139.     }
  140.  
  141.     @Override
  142.     public void mousePressed(MouseEvent e) {
  143.         System.out.println("true");
  144.         mousePressed = true;
  145.     }
  146.  
  147.     @Override
  148.     public void mouseReleased(MouseEvent e) {
  149.         System.out.println("false");
  150.         mousePressed = false;
  151.     }
  152.  
  153.     @Override
  154.     public void mouseEntered(MouseEvent e) {
  155.  
  156.     }
  157.  
  158.     @Override
  159.     public void mouseExited(MouseEvent e) {
  160.  
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement