Line
マウスをクリックすると多角形になります。
/*
A basic extension of the java.applet.Applet class
*/
import java.awt.*; import java.applet.*;
public class Applet1 extends Applet
{
double x1,y1,x2,y2;
double rad=Math.PI/180,leng=50,decrese=10,ang,n,j;
int k=3;
public void init()
{
// Take out this line if you don't use symantec.itools.net.RelativeURL
or symantec.itools.awt.util.StatusScroller
symantec.itools.lang.Context.setApplet(this);
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setLayout(null);
setSize(756,478);
add(label1);
label1.setBackground(java.awt.Color.green);
label1.setBounds(492,312,24,24);
button1.setLabel("Clear");
add(button1);
button1.setBackground(java.awt.Color.lightGray);
button1.setBounds(492,348,60,24);
//}}
//{{REGISTER_LISTENERS
SymMouse aSymMouse = new SymMouse();
this.addMouseListener(aSymMouse);
SymAction lSymAction = new SymAction();
button1.addActionListener(lSymAction);
//}}
}
//{{DECLARE_CONTROLS
java.awt.Label label1 = new java.awt.Label();
java.awt.Button button1 = new java.awt.Button();
//}}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g) {
//以下のコードを自分が作ったのですが、、、、、
三角形の場合を考えるとこのコードがわかると思います。
x1=300;
y1=300;
for(j=3;j<=k;j=j+1) {
for(n=1;n<=j;n=n+1) {
ang=360/j*n;
x2=x1 +leng*Math.cos(rad*ang);
y2=y1 -leng*Math.sin(rad*ang);
g.setColor(Color.blue);
g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
x1=x2;
y1=y2;
}
}
class SymMouse extends java.awt.event.MouseAdapter
{
public void mouseClicked(java.awt.event.MouseEvent event)
{
Object object = event.getSource();
if (object == Applet1.this)
Applet1_MouseClicked(event);
}
}
void Applet1_MouseClicked(java.awt.event.MouseEvent event)
{
// to do: code goes here.
k=k+1;
label1.setText(" "+k);
repaint();
}
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == button1)
button1_ActionPerformed(event);
}
}
void button1_ActionPerformed(java.awt.event.ActionEvent event)
{
// to do: code goes here.
Graphics g=getGraphics();
g.clearRect(0,0,getSize().width,getSize().height);
k=2;
label1.setText("");
}
}
このアップレットは "技術評論社
Visual Basic 5.0 中級テクニック編" を参考にしました。