-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyJPanel.java
More file actions
41 lines (32 loc) · 927 Bytes
/
MyJPanel.java
File metadata and controls
41 lines (32 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class MyJPanel extends JPanel implements MouseMotionListener{
private Square square = new Square(100);
private int centerX;
private int centerY;
public MyJPanel(){
addMouseMotionListener(this);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
// TODO Draw square and fill it with random color decided by method getRandomColor()
super.paintComponent(g);
Color color=square.getRandomColor();
g.setColor(color);
// You should use fillRect()
g.fillRect(centerX-50,centerY-50,100,100);
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Get mouse dragged position and change suqare's position
centerX = e.getX();
centerY = e.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// No need to implement
}
}