Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package app8;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import javax.swing.JPanel;
- public class DrawPanel extends JPanel implements MouseListener{
- //
- Square s1;
- public DrawPanel(){
- s1 = new Square(50, 50, 100);
- // подключаем обработчик событий для мыши
- addMouseListener(this);
- }
- //
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- // color
- g.setColor(Color.red);
- // draw objects
- //g.fillRect(s1.getX(), s1.getY(), s1.getA(), s1.getA());
- g.drawRect(s1.getX(), s1.getY(), s1.getA(), s1.getA());
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- }
- @Override
- public void mousePressed(MouseEvent e) {
- //(x,y)
- System.out.println("mousePressed.X=" + e.getX());
- System.out.println("mousePressed.Y=" + e.getY());
- System.out.println("mousePressed.bNUMBER=" + e.getButton());
- // обрабатываем нажатие на левую кнопку
- if(e.getButton()==MouseEvent.BUTTON1){
- s1.setX(e.getX());
- s1.setY(e.getY());
- // вызов метода repaint();
- repaint();
- }
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- }
- @Override
- public void mouseExited(MouseEvent e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement