Advertisement
sergAccount

Untitled

Dec 6th, 2020
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package app8;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import javax.swing.JPanel;
  13.  
  14. public class DrawPanel extends JPanel implements MouseListener{
  15.     //
  16.     Square s1;
  17.    
  18.     public DrawPanel(){
  19.         s1 = new Square(50, 50, 100);
  20.         // подключаем обработчик событий для мыши        
  21.         addMouseListener(this);
  22.     }
  23.     //
  24.     public void paintComponent(Graphics g){
  25.         super.paintComponent(g);
  26.         // color
  27.         g.setColor(Color.red);
  28.         // draw objects
  29.         g.fillRect(s1.getX(), s1.getY(), s1.getA(), s1.getA());
  30.     }  
  31.     @Override
  32.     public void mouseClicked(MouseEvent e) {
  33.     }
  34.     @Override
  35.     public void mousePressed(MouseEvent e) {
  36.         //(x,y)
  37.         System.out.println("mousePressed.X=" + e.getX());
  38.         System.out.println("mousePressed.Y=" + e.getY());
  39.         System.out.println("mousePressed.bNUMBER=" + e.getButton());
  40.         // обрабатываем нажатие на левую кнопку
  41.         if(e.getButton()==MouseEvent.BUTTON1){
  42.             s1.setX(e.getX());
  43.             s1.setY(e.getY());
  44.             // вызов метода repaint();            
  45.             repaint();                        
  46.         }
  47.     }
  48.     @Override
  49.     public void mouseReleased(MouseEvent e) {
  50.     }
  51.     @Override
  52.     public void mouseEntered(MouseEvent e) {
  53.     }
  54.     @Override
  55.     public void mouseExited(MouseEvent e) {
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement