Java基础之在窗口中绘图——渐变填充(GradientApplet 1)
2013-12-24 00:40
507 查看
Applet程序。
HTML文件:
import javax.swing.*; import java.awt.*; import java.awt.geom.*; @SuppressWarnings("serial") public class GradientApplet extends JApplet { // Initialize the applet @Override public void init() { GradientPane pane = new GradientPane(); // Pane containing filled rectangles getContentPane().add(pane); // BorderLayout.CENTER is default position } // Class defining a pane on which to draw class GradientPane extends JComponent { @Override public void paint(Graphics g) { Graphics2D g2D = (Graphics2D)g; Point2D.Float p1 = new Point2D.Float(150.f, 75.f); // Gradient line start Point2D.Float p2 = new Point2D.Float(250.f, 75.f); // Gradient line end float width = 300; float height = 50; GradientPaint g1 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, true); // Cyclic gradient Rectangle2D.Float rect1 = new Rectangle2D.Float(p1.x-100, p1.y-25, width,height); g2D.setPaint(g1); // Gradient color fill g2D.fill(rect1); // Fill the rectangle g2D.setPaint(Color.BLACK); // Outline in black g2D.draw(rect1); // Fill the rectangle g2D.draw(new Line2D.Float(p1, p2)); g2D.drawString("Cyclic Gradient Paint", p1.x-100, p1.y-50); g2D.drawString("p1", p1.x-20, p1.y); g2D.drawString("p2", p2.x+10, p2.y); p1.setLocation(150, 200); p2.setLocation(250, 200); GradientPaint g2 = new GradientPaint(p1, Color.WHITE, p2, Color.DARK_GRAY, false); // Acyclic gradient rect1.setRect(p1.x-100, p1.y-25, width, height); g2D.setPaint(g2); // Gradient color fill g2D.fill(rect1); // Fill the rectangle g2D.setPaint(Color.BLACK); // Outline in black g2D.draw(rect1); // Fill the rectangle g2D.draw(new Line2D.Float(p1, p2)); g2D.drawString("Acyclic Gradient Paint", p1.x-100, p1.y-50); g2D.drawString("p1", p1.x-20, p1.y); g2D.drawString("p2", p2.x+10, p2.y); } } }
HTML文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body bgcolor="000000"> <center> <applet code = "GradientApplet.class" width = "400" height = "280" > </applet> </center> </body> </html>
相关文章推荐
- Java基础之在窗口中绘图——填充星型(StarApplet 2 filled stars)
- Java基础之在窗口中绘图——绘制星星(StarApplet 1)
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
- Java基础之在窗口中绘图——显示曲线的控制点(CurveApplet 2 displaying control points)
- Java基础之在窗口中绘图——移动曲线的控制点(CurveApplet 3 moving the control points)
- Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
- Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
- Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)
- Java基础之创建窗口——创建应用程序窗口(TryWindow)
- Java基础之创建窗口——颜色和光标(TryWindow4)
- java入门教程-10.2Java框架窗口基础
- 黑马程序员_Java基础:多功能小窗口,swing,io,net综合应用
- Win32 绘图基础 -- 绘制直线、边框、贝塞尔曲线、填充、裁剪
- Java基础之处理事件——使窗口处理自己的事件(Skethcer 1 handing its own closing event)
- Java基础之创建窗口——使用GridBagLayout管理器(TryGridBagLayout)
- Java2实用教程(第二版)程序代码——第八章 Java Applet基础
- 2017/12/31Java基础学习——使用同一个值,填充整个数组のArrays.fill(a, number)方法
- Java Applet 基础
- Canvas基础2-自动填充浏览器窗口