Java 怎么绘制(Paint)并调整小部件大小

2019年8月20日22:37:55 评论 23

1.简介

Java 怎么绘制(Paint)并调整小部件大小?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Rectangle;

public class Main {
    public static void drawResizeWidgets(Rectangle bounds, int widgetWidth,
            Color color, Graphics2D g2) {
        int offset = widgetWidth / 2;
        g2.setPaint(color);
        
        int x = bounds.x - offset;
        int y = bounds.y - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        x = bounds.x + bounds.width / 2 - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        x = bounds.x + bounds.width - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        y = bounds.y + bounds.height / 2 - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        y = bounds.y + bounds.height - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        x = bounds.x + bounds.width / 2 - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        x = bounds.x - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
        
        y = bounds.y + bounds.height / 2 - offset;
        g2.fillRect(x, y, widgetWidth, widgetWidth);
    }
}

 

3.总结

示例代码演示了 Java 怎么绘制(Paint)并调整小部件大小,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: