java 图像渐变_Java基础之在窗口中绘图——渐变填充(GradientApplet 1)

Applet程序。

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文件:

code = "GradientApplet.class"

width = "400"

height = "280"

>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值