-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHopDemo.java
More file actions
152 lines (137 loc) · 4.14 KB
/
HopDemo.java
File metadata and controls
152 lines (137 loc) · 4.14 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.lang.Math;
import java.awt.*;
import java.applet.Applet;
/**
* This applet draws a random fractal image based on
* Barry Martin's "Hopalong" mapping.
* @author David Imai
* 98/11/14
*/
public class HopDemo extends Applet
{
public void init()
{
//String s = this.getParameter("scrWidthParam");
//public int scrWidth = Integer.parseInt(s);
//width of screen in pixels
setLayout(new BorderLayout());
// TextArea showParam = new TextArea();
PicCanvas picture = new PicCanvas();
ControlPanel hopControls = new ControlPanel(picture);
// add("North", showParam);
add("Center", picture);
add("South", hopControls);
picture.setparam();
}
} //end class HopDemo
class PicCanvas extends Canvas
{
int scrWidth = 600; //width of image in pixels
double a,b,c; //parameters that determine shape
double diam; //area displayed
double pixsize; //length & width of one pixel
double x0, y0; //bottom right corner
double x,y,xx; //
int u, v; //position of dot
float hue; //color of dot
int numDots = 2400; //number of dots of each color
int numColors = 100;
Color bgcolor = Color.black; // background color
public void setparam()
{
//choose values of parameters a,b,c
a = 8*(Math.random()-0.5);
b = 4*(Math.random()-0.5);
c = 8*(Math.random()-0.5);
//+++++++++++++++++++++++++++++++++++++++++++++++
diam = Math.sqrt(Math.abs(a*b*c))*32; //width of area displayed
pixsize = diam/((double)scrWidth); //size of 1 pixel
}
public void paint(Graphics g)
{
x = 0;
y = 0;
x0 = -0.5 * diam;
y0 = 0.5 * (a + diam); //center of screen is at (0,a).
u = 0;
v = 0;
g.setColor(bgcolor);
g.fillRect(0,0,scrWidth,scrWidth);
for(int colorNum=0; colorNum<numColors; colorNum++)
{
hue = ((float)colorNum)/(float)numColors;
g.setColor(Color.getHSBColor(hue,1f,1f));
//draw numDots of this color.
for(int i=1; i<numDots; i++)
{
xx = y - (this.sign(x) * Math.sqrt(Math.abs(b * x - c)));
y = a - x;
x = xx;
u = (int)((x - x0)/pixsize);
v = (int)((y0 - y)/pixsize);
g.drawLine(u,v,u,v); //one point
};
};
//g.setColor(Color.white);
//g.drawString(("a="+a+" b="+b+" c="+c), 8,10);
} //end paint
public void redraw(int buttonMsg)
{
if (buttonMsg == 1) // New image
{ this.setparam();
// showParam.redraw(a,b,c);
}
else if (buttonMsg == 2) // Zoom in
{ diam = diam/2;
pixsize = pixsize/2;
}
else if (buttonMsg == 3) // Zoom out
{ diam = diam*2;
pixsize = pixsize*2;
}
repaint();
} // end redraw
public double sign(double x1)
{
if (x1 >= 0.0) return 1.0;
else return -1.0;
} // end sign
} // end class HopCanvas
/* **********************
class TextArea extends Canvas
{
public void redraw(double a,double b, double c)
{
Graphics g;
g.setColor(Color.black);
g.drawString(("a="+a+" b="+b+" c="+c), 8,10);
}
} // end class TextArea
************************ */
class ControlPanel extends Panel // contains buttons
{
PicCanvas c;
public ControlPanel(PicCanvas c)
{
this.c = c;
add(new Button("New Image"));
add(new Button("Zoom In"));
add(new Button("Zoom Out"));
}
public boolean action(Event ev, Object arg)
// check which button is pressed
{ if (ev.target instanceof Button)
{
if ("New Image".equals((String)arg))
{
c.redraw(1);
}
if ("Zoom In".equals((String)arg))
c.redraw(2);
if ("Zoom Out".equals((String)arg))
c.redraw(3);
return true;
}
return false;
} // end action
} // end class ControlPanel