java绘图小程序

来源:百度知道 编辑:UC知道 时间:2024/05/30 06:37:28
能实现画直线,曲线.简单的颜色调换(3种就够).
越简单越好....
需要详细` ` 注释.万分感谢

import java.awt.Graphics;//调用awt中的Graphics类
import javax.swing.JApplet;//调用swing中的JApplet类
public class Test extends JApplet//Test类继承于JApplet
{
public void paint(Graphics g)//绘图方法
{
g.drawLine(40,30,100,30);//绘制一条直线,括号中的是直线坐标
}
}

然后在网页中加入<Applet Code=“Test.class”Width=250 Height=100></Applet>
打开网页就能看到直线了。。。

其他的,请查看Graphics的相关资料

public static void main(String[] args) {
new JFrame() {
{
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(0, 0, 100, 100);
g2.setColor(Color.red);
GeneralPath gp = new GeneralPath();
gp.moveTo(200, 100);
gp.quadTo(200, 200, 300, 300);
gp.curveTo(100, 100, 200, 200, 300, 300);
gp.closePath();
g2.draw(gp);

}
}.setVi