/*
* 以SwingConsole为参考创建一个程序,它可以在屏幕上显示旋转的正方形,并且有一个滑块
* 可以控制旋转的速度,还有一个滑块可以控制正方形的尺寸。
*/
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import static util.SwingConsole.*;
/**
*
* @author Robin Liu
*/
class FillPol extends JPanel{
private static final int POINTNUM = 4;
private int[] xPoint = new int[POINTNUM];
private int[] yPoint = new int[POINTNUM];
private int l;
private int d;
private double r ;
private double dr ;
public FillPol(){
super();
reSize(0);
reRota(0.001);
setBackground(new Color(0,127,127));
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.red);
int maxWidth = getWidth();
int maxHeight = getHeight();
int minL = (int)(Math.min(maxWidth,maxHeight)*.4);
l = minL;
for(int i = 0;i<POINTNUM;i++){
xPoint[i] = (int)(maxWidth/2+(d+l)*(Math.sin(Math.PI*i*2/POINTNUM+r)));
yPoint[i] = (int)(maxHeight/2+(d+l)*(Math.cos(Math.PI*i*2/POINTNUM+r)));
}
g.fillPolygon(xPoint,yPoint,POINTNUM);
r -= dr;
repaint();
}
public void reSize(int newD){
d = newD;
repaint();
}
public void reRota(double newR){
dr = newR;
repaint();
}
}
public class Polygon extends JFrame{
private FillPol p = new FillPol();
private JSlider ss = new JSlider(0,200,100);
private JSlider rs = new JSlider(1,100,1);
public Polygon(){
ss.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
p.reSize(((JSlider)e.getSource()).getValue()-100);
}
});
rs.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e){
p.reRota((double)((JSlider)e.getSource()).getValue()/1000);
}
});
add(BorderLayout.CENTER,p);
add(BorderLayout.SOUTH,ss);
add(BorderLayout.NORTH,rs);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
run(new Polygon(),300,300);
}
}