正好寫了一個,給你看看哈
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了蒲縣免費建站歡迎大家使用!
import?java.awt.*;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.util.Date;
import?javax.imageio.ImageIO;
public?class?ScreenCapturer?{
public?static?void?main(String[]?args)?throws?Exception{
Date?date?=?new?Date();
Robot?rbt?=?new?Robot();
BufferedImage?bf?=??rbt.createScreenCapture(new?Rectangle(1440,900));??//這是屏幕分辨率??可以根據(jù)自己的屏幕修改
File?file?=?new?File("d://"+?date.toString().replace("?","").replace(":","")+".jpg");??//這是保存路徑D盤根目錄
ImageIO.write(bf,"jpg",file);
System.out.println("截圖成功!保存于D盤根目錄下!時間:"?+?date.toString());
}
}
使用PowerDesigner軟件逆向生成java類圖的方法、步驟:1、打開PowerDesiger軟件,點擊文件-建立新模型,新建Obect-OrientedModel2、點擊語言-ReverseEngineerJava,彈出反向生成類圖設(shè)置窗口;3、在反向生成類圖設(shè)置窗口中,選擇ReverseEngineer,可以選擇JavaFiles或JavaDirectories等,在這里我選擇JavaDirectories;4、然后點擊Add按鈕添加目錄,在這里我選擇一個java源文件目錄;5、點擊確定,PowerDesigner會顯示生成進度窗口,完成以后在PowerDesigner工作空間下會顯示類圖目錄結(jié)構(gòu);6、到此便將選中的Java目錄下的Java類文件生成了靜態(tài)類結(jié)構(gòu),
package?honest.imageio;
import?java.awt.Color;
import?java.awt.Font;
import?java.awt.Graphics;
import?java.awt.image.BufferedImage;
import?java.io.File;
import?java.io.IOException;
import?javax.imageio.ImageIO;
/**
*?圖片操作類
*?
*?@author?
*?
*/
public?class?ImageUtil?{
private?BufferedImage?image;
private?int?width;?//?圖片寬度
private?int?height;?//?圖片高度
public?ImageUtil(int?width,?int?height)?{
this.width?=?width;
this.height?=?height;
image?=?new?BufferedImage(width,?height,?BufferedImage.TYPE_INT_ARGB);
}
/**
*?創(chuàng)建一個含有指定顏色字符串的圖片
*?
*?@param?message
*????????????字符串
*?@param?fontSize
*????????????字體大小
*?@param?color
*????????????字體顏色
*?@return?圖片
*/
public?BufferedImage?drawString(String?message,?int?fontSize,?Color?color)?{
Graphics?g?=?image.getGraphics();
g.setColor(color);
Font?f?=?new?Font("宋體",?Font.BOLD,?fontSize);
g.setFont(f);
int?len?=?message.length();
g.drawString(message,?(width?-?fontSize?*?len)?/?2,
(height?+?(int)?(fontSize?/?1.5))?/?2);
g.dispose();
return?image;
}
/**
*?縮放圖片
*?
*?@param?scaleW
*????????????水平縮放比例
*?@param?scaleY
*????????????垂直縮放比例
*?@return
*/
public?BufferedImage?scale(double?scaleW,?double?scaleH)?{
width?=?(int)?(width?*?scaleW);
height?=?(int)?(height?*?scaleH);
BufferedImage?newImage?=?new?BufferedImage(width,?height,
image.getType());
Graphics?g?=?newImage.getGraphics();
g.drawImage(image,?0,?0,?width,?height,?null);
g.dispose();
image?=?newImage;
return?image;
}
/**
*?旋轉(zhuǎn)90度旋轉(zhuǎn)
*?
*?@return?對應(yīng)圖片
*/
public?BufferedImage?rotate()?{
BufferedImage?dest?=?new?BufferedImage(height,?width,
BufferedImage.TYPE_INT_ARGB);
for?(int?i?=?0;?i??width;?i++)
for?(int?j?=?0;?j??height;?j++)?{
dest.setRGB(height?-?j?-?1,?i,?image.getRGB(i,?j));
}
image?=?dest;
return?image;
}
/**
*?合并兩個圖像
*?
*?@param?anotherImage
*????????????另一張圖片
*?@return?合并后的圖片,如果兩張圖片尺寸不一致,則返回null
*/
public?BufferedImage?mergeImage(BufferedImage?anotherImage)?{
int?w?=?anotherImage.getWidth();
int?h?=?anotherImage.getHeight();
if?(w?!=?width?||?h?!=?height)?{
return?null;
}
for?(int?i?=?0;?i??w;?i++)?{
for?(int?j?=?0;?j??h;?j++)?{
int?rgb1?=?image.getRGB(i,?j);
int?rgb2?=?anotherImage.getRGB(i,?j);
Color?color1?=?new?Color(rgb1);
Color?color2?=?new?Color(rgb2);
//?如果該位置兩張圖片均沒有字體經(jīng)過,則跳過
//?如果跳過,則最后將會是黑色背景
if?(color1.getRed()?+?color1.getGreen()?+?color1.getBlue()
+?color2.getRed()?+?color2.getGreen()
+?color2.getBlue()?==?0)?{
continue;
}
Color?color?=?new?Color(
(color1.getRed()?+?color2.getRed())?/?2,
(color1.getGreen()?+?color2.getGreen())?/?2,
(color1.getBlue()?+?color2.getBlue())?/?2);
image.setRGB(i,?j,?color.getRGB());
}
}
return?image;
}
/**
*?保存圖片int?rgb1?=?image.getRGB(i,?j);?int?rgb2?=?anotherImage.getRGB(i,?j);
*?rgb2?=?rgb1??rgb2;?image.setRGB(height?-?i,?j,?rgb2);
*?
*?@param?filePath
*????????????圖片路徑
*/
public?void?save(String?filePath)?{
try?{
ImageIO.write(image,?"png",?new?File(filePath));
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
/**
*?得到對應(yīng)的圖片
*?
*?@return
*/
public?BufferedImage?getImage()?{
return?image;
}
}
1.調(diào)用"images"文件夾里的文件放在ImageIcon里顯示2.在windows下面顯示正常Java在圖形處理時調(diào)用了本地的圖形處理庫。在利用Java作圖形處理(比如:圖片縮放,圖片簽名,生成報表)時,如果運行在windows上不會出問題。如果將程序移植到Linux/Unix上的時候有可能出現(xiàn)圖形不能顯示的錯誤。3.提示信息:"Can'tconnecttoX11windowserver"這是由于Linux的圖形處理需要一個XServer服務(wù)器。(關(guān)于Linux的圖形處理模式參考Tomcat的修改如下:打開%Tomcat_home%/bin/catalina.sh文件在文件的86%處,啟動Tomcat的腳本中加上:-Djava.awt.headless=true/參考:"$_RUNJAVA"$JAVA_OPTS$CATALINA_OPTS/-Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS"-classpath"$CLASSPATH"/-Dcatalina.base="$CATALINA_BASE"/-Dcatalina.home="$CATALINA_HOME"/-Djava.io.tmpdir="$CATALINA_TMPDIR"/-Djava.awt.headless=true/org.apache.catalina.startup.Bootstrap"$@"start/"$CATALINA_BASE"/logs/catalina.out21if[!-z"$CATALINA_PID"];thenecho$!$CATALINA_PIDfifielif["$1"="stop"];then保存后,重新啟動就可以處理圖形了。
這是簡單的初步實現(xiàn),你可以借鑒一下哈
package v01;
import javax.swing.*;
import java.awt.*;
public class ColorP extends JFrame {
public ColorP(){
this.add(new NewPanel());
}
public static void main(String[] args) {
ColorP frame = new ColorP();
frame.setTitle("Content");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class NewPanel extends JPanel{//擴展JPanel
public NewPanel(){
}
protected void paintComponent(Graphics g){//運用畫筆Graphics繪制
super.paintComponent(g);//添加畫筆
int[] x = new int[4];
int[] y = new int[4];
for(int i = 0; i 10; i++){
x[0] = i * 40 ;y[0] = 0;
x[1] = x[0] + 40;y[1] = 0;
x[2] = 0;y[2] = (i+1) * 40;
x[3] = 0;y[3] = y[2] - 40 ;
if(i % 2 == 0)
g.setColor(Color.black);
else
g.setColor(Color.yellow);
g.fillPolygon(x, y, x.length);
}
}
}
}