用java代碼模擬一張圖片可以這樣操作:1.創(chuàng)建BufferedImage類
創(chuàng)新互聯(lián)專注于樂(lè)業(yè)企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城開(kāi)發(fā)。樂(lè)業(yè)網(wǎng)站建設(shè)公司,為樂(lè)業(yè)等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
2.根據(jù)BufferedImage類得到一個(gè)Graphics2D對(duì)象
3.根據(jù)Graphics2D對(duì)象進(jìn)行邏輯操作
4.處理繪圖
5.將繪制好的圖片寫(xiě)入到圖片
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(); //重繪組件
}
}
}