一、概念介紹
1、Java圖像編程的核心類
Java圖像編程的核心類包括:
- BufferedImage:用于表示圖像的類,可以進行像素級的操作。
- Image:表示圖像的抽象類,是所有圖像類的基類。
- ImageIcon:用于顯示圖像的類,可以將圖像嵌入到Swing組件中。
- ImageIO:用于讀取和寫入圖像文件的類。
- Graphics:用于進行圖像繪制操作的抽象類,可以繪制直線、矩形、橢圓等圖形。
- Graphics2D:繼承自Graphics類,提供了更多的繪制方法和功能,可以進行更高級的圖像繪制操作。
- Color:用于表示顏色的類,可以設(shè)置圖像的顏色。
- Font:用于表示字體的類,可以設(shè)置圖像的字體樣式。
這些類是Java圖像編程中常用的核心類,可以幫助你進行圖像的處理、顯示和繪制操作。
2、Graphics簡介
java.awt.Graphics提供了繪制圖形和圖像的功能。它是Abstract Window Toolkit(AWT)的一部分,用于創(chuàng)建基于圖形的用戶界面。
通過使用Graphics類,您可以在屏幕上繪制直線、矩形、橢圓、多邊形等基本形狀,并填充它們的顏色。您還可以繪制圖像、文本和其他復(fù)雜的圖形。
要使用Graphics類,您需要獲取一個Graphics對象。您可以通過調(diào)用組件的getGraphics()方法來獲取該對象,例如JPanel、JFrame、BufferedImage。然后,您可以使用Graphics對象的各種方法來繪制您想要的圖形。
除了繪制圖形,Graphics類還提供了其他一些方法,如設(shè)置顏色、字體和渲染提示等。
3、Graphics主要方法
方法名 | 描述 |
---|---|
void clearRect(int x, int y, int width, int height) | 清除指定矩形區(qū)域的像素 |
void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) | 繪制一個圓弧 |
void drawImage(Image img, int x, int y, ImageObserver observer) | 在指定位置繪制指定的圖像 |
void drawLine(int x1, int y1, int x2, int y2) | 繪制一條直線 |
void drawOval(int x, int y, int width, int height) | 繪制一個橢圓 |
void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) | 繪制一個多邊形 |
void drawRect(int x, int y, int width, int height) | 繪制一個矩形 |
void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) | 繪制一個圓角矩形 |
void drawString(String str, int x, int y) | 在指定位置繪制給定字符串 |
void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) | 填充一個圓弧 |
void fillOval(int x, int y, int width, int height) | 填充一個橢圓 |
void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) | 填充一個多邊形 |
void fillRect(int x, int y, int width, int height) | 填充一個矩形 |
void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight) | 填充一個圓角矩形 |
Color getColor() | 返回當(dāng)前顏色 |
Font getFont() | 返回當(dāng)前字體 |
void setColor(Color c) | 設(shè)置顏色 |
void setFont(Font font) | 設(shè)置字體 |
這些是Graphics類中最常用的一些方法,可以用于繪制基本形狀、圖像和文本,并設(shè)置顏色和字體等屬性。
二、代碼示例
以下通過示例代碼,演示幾個主要的方法使用。
注意:原點坐標(biāo)是左上角,x軸向右增加,y軸向下增加
1、常用方法演示
ImageUtil工具類
public class ImageUtil {
public static BufferedImage createImage() {
int imageWidth = 500;
int imageHeight = 500;
return new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
}
/**
* 將圖片保存到指定位置
*/
public static void saveImage2File(BufferedImage image, String fileLocation, String fileName) {
try {
File file = new File(fileLocation);
if (!file.exists()) {
file.mkdir();
}
FileOutputStream fos = new FileOutputStream(fileLocation + fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ImageIO.write(image, "png", fos);
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用Graphics圖像工具類
public static void test() {
BufferedImage bufferedImage = ImageUtil.createImage();
Graphics g = bufferedImage.getGraphics();
//設(shè)置淺灰色,并繪制背景
g.setColor(new Color(0XEEEEEE));
g.fillRect(0,0,500,500);
//設(shè)置顏色
g.setColor(Color.pink);
//填充圓形
// x、y 繪制的左上角坐標(biāo),width、height 橢圓的寬、高,如果寬高一致就是圓型
g.fillOval(10, 50, 100, 100);
//設(shè)置顏色
g.setColor(Color.ORANGE);
// 繪制矩形
// x、y 繪制的左上角坐標(biāo),width、height 矩形的寬、高
g.drawRect(50, 20, 100, 100);
// 填充繪制圓角矩形
// x、y 繪制的左上角坐標(biāo),width、height 矩形的寬、高
// arcWidth–四個角處圓弧的水平直徑,arcHeight–四個角處圓弧的垂直直徑。
g.fillRoundRect(50, 180, 100, 100,20,20);
//設(shè)置顏色
g.setColor(Color.RED);
// 繪制圓弧
// x、y 繪制的左上角坐標(biāo),width、height 寬、高,startAngle 開始的角度,arcAngle 繪制的總角度,繪制角度從右到左計算
g.drawArc(10, 300, 100, 100, 30, 180);
//設(shè)置顏色
g.setColor(Color.BLUE);
// 填充繪制多邊形
// xPoints、yPoints x軸和y軸的坐標(biāo)數(shù)組,分別一一對應(yīng)組成數(shù)個點,nPoints 要繪制的點數(shù)
int[] xPoints = new int[]{200, 300, 400, 300};
int[] yPoints = new int[]{110, 210, 110, 10};
int nPoints = 4;
g.fillPolygon(xPoints, yPoints, nPoints);
//設(shè)置顏色
g.setColor(Color.YELLOW);
// 填充3d矩形
g.fill3DRect(300, 300, 100, 100, true);//畫一個線框
//設(shè)置顏色
g.setColor(Color.DARK_GRAY);
// 設(shè)置字體
Font font = new Font("微軟雅黑", Font.BOLD, 38);
g.setFont(font);
// 寫文字
g.drawString("寫第一標(biāo)題", 10, 450);
ImageUtil.saveImage2File(bufferedImage, "d:/temp/image/", "g01.png");
}
2、設(shè)置裁剪區(qū)域
public static void test0() {
BufferedImage bufferedImage = ImageUtil.createImage();
Graphics g = bufferedImage.getGraphics();
//設(shè)置淺灰色,并繪制背景
g.setColor(new Color(0XEEEEEE));
g.fillRect(0,0,500,500);
//設(shè)置顏色
g.setColor(Color.pink);
//填充圓形
g.fillOval(20, 20, 100, 100);
//設(shè)置顏色
g.setColor(Color.CYAN);
//設(shè)置裁剪區(qū)域,設(shè)置以后,后續(xù)的所有繪制都只會在此區(qū)域內(nèi)有效
g.setClip(100, 100, 200, 200);
// 在裁剪區(qū)域填充圓型
g.fillOval(150, 150, 200, 200);
ImageUtil.saveImage2File(bufferedImage, "d:/temp/image/", "g00.png");
}
文章來源:http://www.zghlxwxcb.cn/news/detail-787025.html
3、原點坐標(biāo)轉(zhuǎn)換
public static void test2() {
BufferedImage bufferedImage = ImageUtil.createImage();
Graphics g = bufferedImage.getGraphics();
//設(shè)置淺灰色,并繪制背景
g.setColor(new Color(0XEEEEEE));
g.fillRect(0,0,500,500);
//設(shè)置顏色
g.setColor(Color.pink);
//重新設(shè)置坐標(biāo)原點,新繪制的圖形將以此坐標(biāo)作為原點
g.translate(-100, -100);
//填充圓形,原本應(yīng)該在中間,經(jīng)上述轉(zhuǎn)換后,向左上角移動了
g.fillRect(200, 200, 100, 100);
ImageUtil.saveImage2File(bufferedImage, "d:/temp/image/", "g03.png");
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-787025.html
到了這里,關(guān)于Java圖像編程之:Graphics的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!