Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.image.BufferStrategy;
- import javax.swing.JFrame;
- public class Oving2 extends Canvas implements Runnable, MouseListener {
- /**
- * Mucahit
- */
- private static final long serialVersionUID = 1L;
- public static int width = 280;
- public static int height = width;
- public static int scale = 2;
- double posX = 290;
- double posY = 290;
- Rectangle rect = new Rectangle((int)posX, (int)posY, 30, 30);
- boolean mousePressed = false;
- //PointerInfo p = MouseInfo.getPointerInfo();
- private Thread thread;
- public boolean running = false;
- private JFrame frame;
- public Oving2() {
- Dimension fsize = new Dimension(width * scale, height * scale);
- this.addMouseListener(this);
- setPreferredSize(fsize);
- frame = new JFrame();
- }
- public synchronized void start() {
- running = true;
- thread = new Thread(this);
- thread.start();
- }
- public synchronized void stop() {
- running = false;
- try {
- thread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- while (running) {
- if(this.getMousePosition() != null) {
- update(); //SMOOTH MOVE
- }
- render(); //RENDER AS FAST AS POSSIBUL!
- }
- stop();
- }
- private void render() {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- Graphics g = bs.getDrawGraphics();
- //
- Graphics2D g2 = (Graphics2D) g;
- super.paint(g2);
- //
- g2.fillRect(200, 200, 200, 200);
- g2.setColor(Color.white);
- //X1, Y1, X2, Y2
- g2.drawLine(200, 300, (int)posX, (int)posY + 10);
- g2.drawLine((int) posX + 20, (int) posY + 10, 400, 300);
- g2.drawOval((int) posX, (int) posY, 20, 20);
- rect.setLocation((int)posX, (int)posY);
- //
- g.dispose();
- bs.show();
- }
- private void update() {
- // System.out.println("updating");
- int x = (int) this.getMousePosition().getX();
- int y = (int) this.getMousePosition().getY();
- if (rect.contains(x + 10, y + 10)) {
- setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- }
- else{
- setCursor(Cursor.getDefaultCursor());
- }
- if (mousePressed) {
- //System.out.println("x : " + x + " y :" + y);
- if (rect.contains(x + 10, y + 10)) {
- posY = y;
- posX = x;
- }
- } else {
- posY = 290;
- posX = 290;
- }
- }
- public static void main(String[] args) {
- Oving2 oving2 = new Oving2();
- oving2.frame.setResizable(false);
- oving2.frame.setTitle("Frame");
- oving2.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- oving2.frame.add(oving2);
- oving2.frame.pack();
- oving2.frame.setLocationRelativeTo(null);
- oving2.frame.setVisible(true);
- oving2.start();
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- System.out.println("true");
- mousePressed = true;
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- System.out.println("false");
- mousePressed = false;
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement