Advertisement
sergAccount

Untitled

Dec 6th, 2020
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 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.         g.drawRect(s1.getX(), s1.getY(), s1.getA(), s1.getA());
  31.     }  
  32.     @Override
  33.     public void mouseClicked(MouseEvent e) {
  34.     }
  35.     @Override
  36.     public void mousePressed(MouseEvent e) {
  37.         //(x,y)
  38.         System.out.println("mousePressed.X=" + e.getX());
  39.         System.out.println("mousePressed.Y=" + e.getY());
  40.         System.out.println("mousePressed.bNUMBER=" + e.getButton());
  41.         // обрабатываем нажатие на левую кнопку
  42.         if(e.getButton()==MouseEvent.BUTTON1){
  43.             s1.setX(e.getX());
  44.             s1.setY(e.getY());
  45.             // вызов метода repaint();            
  46.             repaint();                        
  47.         }
  48.     }
  49.     @Override
  50.     public void mouseReleased(MouseEvent e) {
  51.     }
  52.     @Override
  53.     public void mouseEntered(MouseEvent e) {
  54.     }
  55.     @Override
  56.     public void mouseExited(MouseEvent e) {
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement