-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDrawPoints.java
More file actions
56 lines (46 loc) · 1.24 KB
/
DrawPoints.java
File metadata and controls
56 lines (46 loc) · 1.24 KB
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
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class DrawPoints extends JFrame
{
public DrawPoints()
{
// setup basic panel
DrawPanel pane = new DrawPanel();
// add components
add( pane );
// set frame attributes
setTitle( "Example" );
setSize( 300, 200 );
setLocationRelativeTo( null );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public static void main( String[] args )
{
DrawPoints ex = new DrawPoints();
ex.setVisible( true );
}
}
// make custom panel class
class DrawPanel extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// paint using the Graphics2D object
Graphics2D g2d = ( Graphics2D )g;
g2d.setColor( Color.blue );
Dimension size = getSize();
Insets insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
Random r = new Random();
// randomly paint a bunch of points within the window boundary
for (int i = 0; i<=1000; i++)
{
int x = Math.abs( r.nextInt() ) % w;
int y = Math.abs( r.nextInt() ) % h;
g2d.drawLine( x, y, x, y ); // effectively draws a point
}
}
}