其他分享
首页 > 其他分享> > swing203不规则形状绘制

swing203不规则形状绘制

作者:互联网

不规则形状绘制

在这里插入图片描述

package my;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class MyPanel extends JPanel
{

    public MyPanel()
    {
    }

    @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);
        g2d.setPaint(Color.white);
        g2d.fillRect(0, 0, width, height);

        // 创建一个Path
        Path2D outline = new Path2D.Double();
        outline.moveTo(50, 10);  // 起点
        outline.lineTo(150,60);  // 直线连接到下一点
        outline.lineTo(100,120);
        outline.lineTo(40,100);
        outline.lineTo(10,40);
        outline.closePath(); // 收尾闭合

        Color fillColor = Color.GREEN;
        g2d.setPaint(fillColor);
        g2d.draw(outline);

    }


}

圆弧

在这里插入图片描述

package my;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class MyPanel extends JPanel
{

    public MyPanel()
    {
    }

    @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);
        g2d.setPaint(Color.white);
        g2d.fillRect(0, 0, width, height);

        // 指定一个外围矩形
        Rectangle rect = new Rectangle(30,30, 100,100);
        g2d.setPaint(Color.LIGHT_GRAY);
        g2d.draw(rect);

        // 创建一个 Arc
        // Arc2D.OPEN(圆弧) , Arc2D.CHORD (弓形 ), Arc2D.PIE (扇形)
        Shape shape = new Arc2D.Double(rect, 0, 90, Arc2D.OPEN);
        g2d.setPaint(Color.BLUE);
        g2d.draw(shape);

    }


}

直线与圆弧拼接

在这里插入图片描述

package my;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class MyPanel extends JPanel
{

    public MyPanel()
    {
    }

    @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);
        g2d.setPaint(Color.white);
        g2d.fillRect(0, 0, width, height);

        // 直线与圆弧连接构成不规则形状
        int x= 50, y=50;
        Rectangle r1 = new Rectangle(x,y,100,100);
        Rectangle r2 = new Rectangle(x+50,y,100,100);

        // ar1 : 左侧圆弧               ar2 : 右侧圆弧
        Arc2D arc1 = new Arc2D.Double(r1, 90, 180, Arc2D.OPEN);
        Arc2D arc2 = new Arc2D.Double(r2, 270, 180, Arc2D.OPEN);

        // 构造一条闭合路径
        Path2D outline = new Path2D.Double();
        outline.append(arc1, true);
        outline.append(arc2, true); // true表示自动连接上一条线的终点和下一条线的起点
        outline.closePath(); // 闭合 ( 自动连接最后一个点 和 最开始的点 )

        Color fillColor = Color.GREEN;
        g2d.setPaint(fillColor);
        g2d.fill(outline);

    }


}

标准五角星

在这里插入图片描述

package my;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class MyPanel extends JPanel
{

    public MyPanel()
    {
    }

    @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);
        g2d.setPaint(Color.white);
        g2d.fillRect(0, 0, width, height);

        // 10个顶点的坐标
        // 注意:这里是以中心为原点、x轴向右、y轴向上的坐标系,标准圆半径为1
        Point2D.Double pA = new Point2D.Double(0, 1);
        Point2D.Double pB = new Point2D.Double(-0.95106,0.30902);
        Point2D.Double pC = new Point2D.Double(-0.58779,-0.80902);
        Point2D.Double pD = new Point2D.Double(0.58779,-0.80902);
        Point2D.Double pE = new Point2D.Double(0.95106,0.30902);
        Point2D.Double pF = new Point2D.Double(0,-0.38197);
        Point2D.Double pG = new Point2D.Double(0.36328,-0.11804);
        Point2D.Double pH = new Point2D.Double(0.22452,0.30902);
        Point2D.Double pI = new Point2D.Double(-0.22452,0.30902);
        Point2D.Double pJ = new Point2D.Double(-0.36328,-0.11804);
        Point2D.Double[] points = { pA, pI, pB, pJ, pC, pF, pD, pG, pE, pH };

        // 坐标转换 ( 指定目标正方形,在里面画一个五角星 )
        Rectangle rect = new Rectangle(30, 30, 200,200);
        int radius_x = rect.width / 2;
        int radius_y = rect.width / 2;
        for(Point2D.Double point : points)
        {
            point.x = rect.getCenterX() + radius_x * point.x ;
            point.y = rect.getCenterY() - radius_y * point.y ; // 坐标反转
        }

        // 画五角星
        Path2D outline = new Path2D.Double();
        outline.moveTo(points[0].x, points[0].y);
        for(int i=1; i<points.length; i++)
        {
            outline.lineTo(points[i].x, points[i].y);
        }
        outline.closePath();

        Color fillColor = Color.YELLOW;
        g2d.setPaint(fillColor);
        g2d.fill(outline);

        // 画圆
        Shape circle = new Ellipse2D.Double(rect.x, rect.y, rect.width, rect.height);
        g2d.draw(circle);


    }


}

透明背景

在这里插入图片描述

package my;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class MyFrame extends JFrame
{

    public MyFrame(String title)
    {
        super(title);

        // Content Pane
        JPanel root = new JPanel();
        this.setContentPane(root);
        root.setLayout(new FlowLayout());

        // 根容器设置为红色
        root.setOpaque(true);
        root.setBackground(new Color(0xEE4000));

        // 创建五角星控件
        Star star = new Star();
        star.setPreferredSize(new Dimension(150,150));
        root.add(star);

    }


}

package my;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Calendar;

import javax.imageio.ImageIO;
import javax.swing.JPanel;


public class Star extends JPanel
{

	public Star()
	{
		// true, 背景不透明 ; false, 背景透明
		this.setOpaque( false );
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		int width = getWidth();
		int height = getHeight();
		Graphics2D g2d = (Graphics2D) g;

		// 整个控件的背景填充为白色
//		g2d.setPaint(Color.white);
//		g2d.fillRect(0, 0, width, height);
		
		// 平滑绘制 ( 反锯齿 )
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
				
		// 10个顶点的坐标
		// 注意:这里是以中心为原点、x轴向右、y轴向上的坐标系,标准圆半径为1
		Point2D.Double pA = new Point2D.Double(0, 1);
		Point2D.Double pB = new Point2D.Double(-0.95106,0.30902);
		Point2D.Double pC = new Point2D.Double(-0.58779,-0.80902);
		Point2D.Double pD = new Point2D.Double(0.58779,-0.80902);
		Point2D.Double pE = new Point2D.Double(0.95106,0.30902);
		Point2D.Double pF = new Point2D.Double(0,-0.38197);
		Point2D.Double pG = new Point2D.Double(0.36328,-0.11804);
		Point2D.Double pH = new Point2D.Double(0.22452,0.30902);
		Point2D.Double pI = new Point2D.Double(-0.22452,0.30902);
		Point2D.Double pJ = new Point2D.Double(-0.36328,-0.11804);
		Point2D.Double[] points = { pA, pI, pB, pJ, pC, pF, pD, pG, pE, pH };
		
		// 坐标转换 ( 指定目标正方形,在里面画一个五角星 )
		// Rectangle rect = new Rectangle(30, 30, 200,200);
		int size = width;
		if(size > height) size = height;
		size -= 2; // 往里缩一点
		Rectangle rect = new Rectangle( (width-size)/2, (height-size)/2, size,size);
		
		int radius_x = rect.width / 2;
		int radius_y = rect.width / 2;
		for(Point2D.Double point : points)
		{
			point.x = rect.getCenterX() + radius_x * point.x ;
			point.y = rect.getCenterY() - radius_y * point.y ; // 坐标反转
		}		
		
		// 画五角星
		Path2D outline = new Path2D.Double();
		outline.moveTo(points[0].x, points[0].y);
		for(int i=1; i<points.length; i++)
		{
			outline.lineTo(points[i].x, points[i].y);
		}
		outline.closePath();
		
		Color fillColor = Color.YELLOW;
		g2d.setPaint(fillColor);
		g2d.fill(outline);
		
		// 画圆
		Shape circle = new Ellipse2D.Double(rect.x, rect.y, rect.width, rect.height);
		g2d.draw(circle);


	}

	
}

标签:java,Double,awt,swing203,不规则,new,import,绘制,Point2D
来源: https://blog.csdn.net/dingpython/article/details/120377773