用java代碼模擬一張圖片可以這樣操作:1.創(chuàng)建BufferedImage類
成都創(chuàng)新互聯(lián)專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,西部信息機(jī)房,西部信息機(jī)房,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。
2.根據(jù)BufferedImage類得到一個(gè)Graphics2D對(duì)象
3.根據(jù)Graphics2D對(duì)象進(jìn)行邏輯操作
4.處理繪圖
5.將繪制好的圖片寫入到圖片
下面是我以前做的小程序,希望能給你點(diǎn)幫助!
package menu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DaYin extends JFrame implements ActionListener{
public DaYin() {
Container c=this.getContentPane();
c.setLayout(new BorderLayout());
JMenuBar jmb=new JMenuBar();
JMenu jm=null;
jm=new JMenu("文件");
jmb.add(jm);
jm=new JMenu("編輯");
jmb.add(jm);
jm=new JMenu("打印");
JMenuItem jmi=new JMenuItem("打印預(yù)覽");
jmi.addActionListener(this);
jm.add(jmi);
jmb.add(jm);
jm=new JMenu("幫助");
jmb.add(jm);
this.setJMenuBar(jmb);
this.setSize(1024,768);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args) {
new DaYin();
}
public void update(Graphics g){
g.setColor(Color.black);
String title[]={"摘要","科目","子目或戶名","借方金額","貸方金額"};
String danwei[]={"千","百","十","萬","千","百","十","元","角","分"};
int csx=200;//打印x的初始坐標(biāo)
int csy=200;//打印y的初始坐標(biāo)
int x=csx;
int y=csy;
int a=x+20;//打印字體x的初始坐標(biāo)
int b=y+20;//打印字體y的初始坐標(biāo)
int row=7;//一共打印出幾條橫線
int cul=5;//一共打印出幾條豎線
int hangju=30;//線與線之間的縱向間距
int lieju=100;//線與線之間的橫向間距
int yy=hangju*row+y;
int c=yy+20;
g.setFont(new Font("行楷",Font.TYPE1_FONT,24));
g.drawString("記 賬 憑 證", x+160, y-40);//打印出記賬憑證這幾個(gè)字
g.drawLine(x+100, y-30, x+340, y-30);//打印出記賬憑證下面的那條線
g.setFont(new Font("行楷",Font.TYPE1_FONT,13));
g.drawString("年 月 日 第_______號(hào)", x+260, y-10);
//g.drawLine(x+430, y-10, x+470, y-10);
for(int i=0;i=row;i++){
g.drawLine(x, y, cul*lieju+x+25, y);//此循環(huán)用于畫出表格的行
if(i==0)
y+=hangju*2;
else
y+=hangju;
}
y=csy;
g.drawLine(x+lieju*3, y+hangju, cul*lieju+x, y+hangju);//打印出第一行半的橫線
for(int j=0;j=cul;j++){
g.drawLine(x, y, x, row*hangju+y);//此循環(huán)用于畫出表格的列
x+=lieju;
}
x=csx;
for(int z=0;zcul;z++){
g.setColor(Color.red);//設(shè)置顏色
g.setFont(new Font("",Font.TYPE1_FONT,13));//設(shè)置表頭字體
if(z3)
g.drawString(title[z], a, b+hangju/2);
else
g.drawString(title[z], a, b);//為表格添加表頭
a+=lieju;
}//76-89打印出一個(gè)表格形狀
int xx=lieju*3+x+10;
for(int v=0;v=9;v++){
g.drawLine(xx, y+hangju, xx, row*hangju+y+hangju);//此循環(huán)用于畫出表格的列
g.setFont(new Font("",Font.TYPE1_FONT,11));
g.drawString(danwei[v], xx-10, y+hangju+20);//為表格添加表頭
if(v%2==0)
g.setColor(Color.red);//設(shè)置顏色
else
g.setColor(Color.BLUE);//設(shè)置顏色
xx+=10;
}
for(int n=0;n9;n++){
g.drawLine(xx, y+hangju, xx, row*hangju+y+hangju);//此循環(huán)用于畫出表格的列
g.drawString(danwei[n],xx-10, y+hangju+20);//為表格添加表頭
if(n%2==0)
g.setColor(Color.red);//設(shè)置顏色
else
g.setColor(Color.BLUE);//設(shè)置顏色
xx+=10;
}
g.drawString(danwei[9],xx-10, y+hangju+20);
g.setColor(Color.black);//設(shè)置顏色
for(int i=0;i=1;i++){
g.drawLine(x,yy , cul*lieju+x, yy);//此循環(huán)用于畫出表格的行
yy+=hangju;
}
yy=hangju*row+y;
for(int j=0;j=3;j++){
g.drawLine(x, yy, x, (row+1)*hangju+y);//此循環(huán)用于畫出表格的列
if(j0){
x+=lieju;
}else{
x+=lieju*3;
}
}//112-128打印出表格的最后一行單元格
x=csx;
g.setFont(new Font("",Font.TYPE1_FONT,13));
g.drawString("合 計(jì)",x+60, c);
g.drawString("會(huì)計(jì)主管 記賬 出納 審核 制證", x+30, yy+50);
g.drawLine(cul*lieju+x+25, y, cul*lieju+x+25, (row+1)*hangju+y);//打印出最后一列
g.drawString("記",cul*lieju+x+6, y+15);
g.drawString("賬", cul*lieju+x+6, y+35);
g.drawString("√", cul*lieju+x+6, y+55);//134-136為最后一列寫入字體
g.drawString("附",cul*lieju+x+35, y+75);
g.drawString("件", cul*lieju+x+35, y+95);
g.drawString("張", cul*lieju+x+35, y+175);//134-136為最后一列后寫入字體
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("打印預(yù)覽")){
this.repaint();
}
}
}
使用drawLine()做了一個(gè)記賬憑證。
執(zhí)行點(diǎn)擊"打印"子菜單"打印預(yù)覽"就可顯示出我們所畫的"記賬憑證表"
希望這個(gè)小程序能夠給你點(diǎn)靈感
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不規(guī)則圖形的繪制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath對(duì)象實(shí)例
Point aPoint;
//構(gòu)造函數(shù)
public IrregularShapeDemo() {
super("不規(guī)則圖形的繪制"); //調(diào)用父類構(gòu)造函數(shù)
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件
setSize(300, 200); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時(shí)退出程序
}
public void paint(Graphics g) { //重載窗口組件的paint()方法
Graphics2D g2D = (Graphics2D)g; //獲取圖形環(huán)境
g2D.draw(gPath); //繪制路徑
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠標(biāo)事件處理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)
gPath = new GeneralPath(); //重新實(shí)例化GeneralPath對(duì)象
gPath.moveTo(aPoint.x,aPoint.y); //設(shè)置路徑點(diǎn)
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠標(biāo)運(yùn)動(dòng)事件處理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)
gPath.lineTo(aPoint.x, aPoint.y); //設(shè)置路徑
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重繪組件
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不規(guī)則圖形的繪制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath對(duì)象實(shí)例
Point aPoint;
//構(gòu)造函數(shù)
public IrregularShapeDemo() {
super("不規(guī)則圖形的繪制"); //調(diào)用父類構(gòu)造函數(shù)
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件
setSize(300, 200); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時(shí)退出程序
}
public void paint(Graphics g) { //重載窗口組件的paint()方法
Graphics2D g2D = (Graphics2D)g;//獲取圖形環(huán)境
g2D.draw(gPath); //繪制路徑
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠標(biāo)事件處理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)
gPath = new GeneralPath(); //重新實(shí)例化GeneralPath對(duì)象
gPath.moveTo(aPoint.x,aPoint.y); //設(shè)置路徑點(diǎn)
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠標(biāo)運(yùn)動(dòng)事件處理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到當(dāng)前鼠標(biāo)點(diǎn)
gPath.lineTo(aPoint.x, aPoint.y); //設(shè)置路徑
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重繪組件
}
}
}