-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFractalGeneratorGuiViewerPanel.java
119 lines (99 loc) · 3.24 KB
/
FractalGeneratorGuiViewerPanel.java
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* The panel that shows Fractals.
*/
public class FractalGeneratorGuiViewerPanel extends JPanel implements Fractal.FractalListener, MouseWheelListener, MouseListener, MouseMotionListener {
private Fractal mFractal;
private MouseBehaviour mMouseBehaviour = MouseBehaviour.Hand;
FractalGeneratorGuiViewerPanel() {
super();
// Initialize with preferred size
setPreferredSize(new Dimension(FractalGeneratorGui.PREFERRED_HEIGHT, FractalGeneratorGui.PREFERRED_HEIGHT));
// Set background
setBackground(Color.BLACK);
// Set up listeners for mouse events
addMouseWheelListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the fractal on our canvas if we have a fractal
if (mFractal != null) {
mFractal.draw(g, getWidth(), getHeight());
}
}
void setFractal(Fractal fractal) {
if (mFractal != null) {
mFractal.setFractalListener(null);
}
mFractal = fractal;
mFractal.setFractalListener(this);
onFractalInvalidated();
}
void setMouseBehaviour(MouseBehaviour mouseBehaviour) {
mMouseBehaviour = mouseBehaviour;
}
enum MouseBehaviour {
ZoomIn, ZoomOut, Select, Hand
}
@Override
public void onFractalInvalidated() {
repaint();
}
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (mFractal != null) {
mFractal.zoom(e.getPreciseWheelRotation());
}
}
// Variables used for panning
private boolean mIsDragging;
private int mStartPanX;
private int mStartPanY;
private int mStartMouseX;
private int mStartMouseY;
// Zoom in and out
@Override
public void mouseClicked(MouseEvent e) {
if (mFractal != null) {
if (mMouseBehaviour == MouseBehaviour.ZoomIn) {
mFractal.zoom(1);
} else if (mMouseBehaviour == MouseBehaviour.ZoomOut) {
mFractal.zoom(-1);
}
}
}
// Start panning
@Override
public void mousePressed(MouseEvent e) {
if (mMouseBehaviour == MouseBehaviour.Hand && mFractal != null) {
mIsDragging = true;
mStartPanX = mFractal.getPanX();
mStartPanY = mFractal.getPanY();
mStartMouseX = e.getX();
mStartMouseY = e.getY();
}
}
// Stop panning
@Override
public void mouseReleased(MouseEvent e) {
mIsDragging = false;
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
// Panning
@Override
public void mouseDragged(MouseEvent e) {
if (mMouseBehaviour == MouseBehaviour.Hand && mIsDragging && mFractal != null) {
mFractal.pan(mStartPanX + e.getX() - mStartMouseX, mStartPanY + e.getY() - mStartMouseY);
}
}
@Override
public void mouseMoved(MouseEvent e) {}
}