cycletest.java
来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 80 行
JAVA
80 行
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class CycleTest extends Applet implements ItemListener
{
// 要绘制的矩形
private Rectangle2D rect;
// 包含两个点的直线,它将确定绘制的端点
private Line2D line;
// 选择循环类型的单选框
private Checkbox cyclic;
private Checkbox acyclic;
public void init()
{
// 创建一个单位正方形
rect = new Rectangle2D.Float(-0.5f, -0.5f, 1.0f, 1.0f);
// 设置终点.
line = new Line2D.Float(-0.25f, 0.0f, 0.25f, 0.0f);
setBackground(Color.ORANGE);
// 创建选择渐变循环类型的单选框
CheckboxGroup cbg = new CheckboxGroup();
setLayout(new BorderLayout());
Panel p = new Panel();
p.setBackground(Color.GREEN);
cyclic = new Checkbox("cyclic", cbg, true);
cyclic.addItemListener(this);
p.add(cyclic);
acyclic = new Checkbox("acyclic", cbg, false);
acyclic.addItemListener(this);
p.add(acyclic);
add(p, BorderLayout.SOUTH);
}
public void paint(Graphics g)
{
// 缩放后的矩形宽度
final double scaleWidth = 100.0f;
// 把传入的Graphics容器转换为一个可用的 Graphics2D 对象
Graphics2D g2d = (Graphics2D)g;
// 平移
g2d.translate(100,100);
g2d.scale(scaleWidth, 50);
// 绘制
g2d.setPaint(new GradientPaint(
line.getP1(), Color.BLACK, line.getP2(), Color.WHITE, cyclic.getState()));
g2d.fill(rect);
// 画出端点处的垂线
g2d.setPaint(Color.RED);
g2d.setTransform(new AffineTransform());
g2d.translate(100-0.25*scaleWidth, 100);
g2d.rotate(Math.PI/2);
g2d.scale(scaleWidth/2, 1);
g2d.draw(line);
g2d.setTransform(new AffineTransform());
g2d.translate(100+0.25*scaleWidth, 100);
g2d.rotate(Math.PI/2);
g2d.scale(scaleWidth/2, 1);
g2d.draw(line);
}
public void itemStateChanged(ItemEvent e)
{
// 更新!
repaint();
}
} // CycleTest
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?