高级绘制

import javax.swing.*;
public class Swing2 {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createGUI();
}
});
}
private static void createGUI() {
JFrame frame = new MyFrame("Swing Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 300);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
public class MyFrame extends JFrame {
public MyFrame(String title) {
super(title);
JPanel root = new MyPanel();
this.setContentPane(root);
root.setLayout(new BorderLayout());
}
}
import javax.swing.*;
import java.awt.*;
public class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.BLUE);
g2d.drawLine(0, 0, width, height);
g2d.drawLine(width, 0, 0, height);
}
}
Graphics2D



import javax.swing.*;
import java.awt.*;
public class MyPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D)g;
// 平滑绘制(反锯齿)
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Stroke stroke = new BasicStroke(2,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND,
1,
new float[] {20, 4},
0);
g2d.setStroke(stroke);
Paint color = Color.BLUE;
g2d.setPaint(color);
g2d.drawLine(0, 0, width, height);
g2d.drawLine(width, 0, 0, height);
}
}
填充

package Swing2_02;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class MyPanel_swing_02 extends JPanel {
BufferedImage textureImage;
public MyPanel_swing_02()
{
// 加载背景图片 125 x 125
URL imagePath = getClass().getResource("/images/grassland.jpg");
try
{
textureImage = ImageIO.read(imagePath);
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D) g;
// 纯色填充
if (true)
{
Color color = new Color(0xFF7F24);
g2d.setPaint(color);
g2d.fillRect(0, 0, width / 2, height / 2);
}
// 线性渐变填充
if (true)
{
// 右上角
Rectangle r = new Rectangle(width / 2, 0, width / 2, height / 2);
// 线性渐变
// 渐变的方向:从start->end
// 渐变的颜色:设置N个关键点位
Point2D start = new Point2D.Float(r.x, r.y); // 起点
Point2D end = new Point2D.Float(r.x+r.width, r.y); // 终点(渐变的方向)
float[] dist = { 0.0f, 0.8f, 1.0f };// 插入关键点
Color[] colors = { Color.GREEN, Color.YELLOW, Color.WHITE }; // 关键点的颜色值
Paint paint = new LinearGradientPaint(start, end, dist, colors);
// 设置填充
g2d.setPaint(paint); // 设置颜色
g2d.fillRect(r.x, r.y, r.width, r.height);
}
// 辐射渐变填充
if (true)
{
// 左下角
Rectangle r = new Rectangle(0, height / 2, width / 2, height / 2);
//
Point2D center = new Point2D.Double(r.getCenterX(), r.getCenterY());
float radius = width/2;
float[] dist = { 0.0f, 0.2f, 1.0f };
Color[] colors = { Color.RED, Color.WHITE, Color.BLUE };
RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
// 设置填充
g2d.setPaint(paint);
g2d.fillRect(r.x, r.y, r.width, r.height);
}
// 统理填充
if (true)
{
// 右下角
Rectangle r = new Rectangle(width/2, height / 2, width / 2, height / 2);
// 图片是125x125的小图,实际填充时的循环填充直到充满
// anchor用于指定图片的哪一部分用于填充
Rectangle anchor = new Rectangle(0,0,textureImage.getWidth(), textureImage.getHeight());
Paint paint = new TexturePaint(textureImage, anchor);
// 设置填充
g2d.setPaint(paint);
g2d.fillRect(r.x, r.y, r.width, r.height);
}
}
}
剪裁区域

package my;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class MyPanel extends JPanel {
BufferedImage textureImage;
public MyPanel()
{
// 加载背景图片 125 x 125
URL imagePath = getClass().getResource("/images/demo_pic.jpg");
try
{
textureImage = ImageIO.read(imagePath);
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D) g;
// 设置clip区域(仅区域内可被绘制)
int size = 200;
Shape region = new Ellipse2D.Double((width - size) / 2, (height - size) / 2, size, size);
g2d.clip(region);
// 绘制图形
g2d.drawImage(textureImage, 0, 0, width, height, null);
}
}
绘制彩色边框

package my;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class MyPanel extends JPanel {
public MyPanel()
{
}
@Override
protected void paintComponent(Graphics g)
{
int width = getWidth();
int height = getHeight();
Graphics2D g2d = (Graphics2D) g;
// 设置线型
Stroke stroke = new BasicStroke(10);
g2d.setStroke(stroke);
Color c1 = Color.GREEN;
Color c2 = Color.RED;
drawLine(g2d, 0, 0, width, 0, c1, c2); // 上
drawLine(g2d, width, 0, width, height, c2, c1); // 右
drawLine(g2d, width, height, 0, height, c1, c2); // 下
drawLine(g2d, 0, height, 0, 0, c2, c1); // 左
}
private void drawLine(Graphics2D g2d,
int x1, int y1, int x2, int y2,
Color c1, Color c2)
{
// Point2D
Point2D start = new Point2D.Double(x1, y1); // 起点
Point2D end = new Point2D.Double(x2, y2); // 终点(渐变的方向)
float[] dist = { 0.0f, 1.0f };// 插入关键点
Color[] colors = { c1, c2}; // 关键点的颜色值
Paint paint = new LinearGradientPaint(start, end, dist, colors);
// 设置Paint
g2d.setPaint(paint);
// 构造一个 Shape
Shape shape = new Line2D.Double(start,end);
g2d.draw( shape );
}
}
本文深入探讨了Java Swing中的高级图形绘制技术,包括使用Graphics2D进行精准绘图,实现图形填充,设置剪裁区域以及绘制多彩边框。通过这些技巧,开发者可以创建出更加丰富和复杂的用户界面。
111

被折叠的 条评论
为什么被折叠?



