Swing201图片绘制

本文深入探讨了Java Swing中的高级图形绘制技术,包括使用Graphics2D进行精准绘图,实现图形填充,设置剪裁区域以及绘制多彩边框。通过这些技巧,开发者可以创建出更加丰富和复杂的用户界面。

高级绘制

在这里插入图片描述

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 );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值