Newer
Older
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
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.RenderingHints;
import java.awt.event.*;
// JComponent is a base class for custom components
public class SimpleDraw2 extends JComponent {
public static void main(String[] args) {
JFrame f = new JFrame("SimpleDraw"); // jframe is the app window
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 400); // window size
SimpleDraw2 canvas = new SimpleDraw2();
f.setContentPane(canvas); // add canvas to jframe
f.setVisible(true); // show the window
}
// Object properties
private int mouseX;
private int mouseY;
// Constructor for SimpleDraw
public SimpleDraw2() {
this.addMouseMotionListener( new MyMotionListener() );
}
// custom graphics drawing
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g; // cast to get 2D drawing methods
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, // antialiasing look nicer
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(32)); // 32 pixel thick stroke
g2.setColor(Color.BLUE); // make it blue
g2.drawLine(0, 0, getWidth(), getHeight()); // draw line
g2.setColor(Color.RED);
g2.drawLine(getWidth(), 0, 0, getHeight());
String label = "Mouse at (" + mouseX + ", " + mouseY + ")";
g2.setColor(Color.BLACK);
g2.drawString(label, 130, 40);
}
// Inner class for handling mouse motion events
private class MyMotionListener implements MouseMotionListener {
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
public void mouseDragged(MouseEvent e) {
// Do nothing.
// But we still need this method implemented because it's part of the interface
}
}
}