※ 引述《ligod (徵MVP連線)》之銘言:
: 請問板上各位前輩
: 用Graphics在JPanel上面繪圖之後
: 可以把JPanel目前的圖像轉成一個Image物件嗎
可以 我小時候寫過 XD ( 現在廢九成了 Orz)
以下是我當年寫的code 我印象深刻 這隻是我第一個java程式 XD
參考看看
//package test1;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.*;
import java.io.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class DrawSpace extends JScrollPane {
BufferedImage image;
Graphics2D g2;
int currentX, currentY, oldX, oldY;
int flag=0;
public DrawSpace() {
// setDoubleBuffered(false);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (Frame1.selectItems_d == "DrawPath") drawPath_m1(e);
else if (Frame1.selectItems_d == "StrightLine") strightLine_m1(e);
else if (Frame1.selectItems_d == "DrawOval") DrawOval_m1(e);
else if (Frame1.selectItems_d == "Eraser") Eraser_m1(e);
}
public void mouseReleased(MouseEvent e) {
if (Frame1.selectItems_d == "StrightLine") strightLine_m2(e);
else if (Frame1.selectItems_d == "DrawOval") DrawOval_m2(e);
}
});
this.addMouseMotionListener(new MouseMotionAdapter( ) {
public void mouseDragged(MouseEvent e) {
if (Frame1.selectItems_d == "DrawPath") drawPath_m2(e);
if (Frame1.selectItems_d == "Eraser") Eraser_m2(e);
}
});
}
public void paint(Graphics g) {
//if (image == null) {
if (image==null){
image = new BufferedImage(getSize().width, getSize( ).height, BufferedImage.TYPE_INT_RGB);
g2 = (Graphics2D)image.getGraphics( );
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
clear();
flag++;
}
if(flag==0){//給載入的image
flag++;
g2 = (Graphics2D)image.getGraphics( );
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
}
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
public void clear() {
g2.setPaint(Color.white);
g2.fillRect(0, 0, getSize().width, getSize( ).height);
g2.setPaint(Color.black);
repaint();
}
public void showImage() {
flag=0;
FileInputStream in;
try{
in = new FileInputStream(Frame1.url.getFile());
image=JPEGCodec.createJPEGDecoder(in).decodeAsBufferedImage();
in.close();
}
catch(java.io.FileNotFoundException e){}
catch(java.io.IOException e){}
repaint();
}
public void saveImage(){
FileOutputStream out;
try{
out = new FileOutputStream(Frame1.url.getFile());
JPEGCodec.createJPEGEncoder(out).encode(image);
out.close();
}
catch(java.io.IOException ex){}
}
//-------------------------Mouse Event-------------------------------
//-----------m1-----------------
public void drawPath_m1(MouseEvent e){
oldX = e.getX( ); oldY = e.getY( );
}
public void strightLine_m1(MouseEvent e){
oldX = e.getX( ); oldY = e.getY( );
}
public void DrawOval_m1(MouseEvent e){
oldX = e.getX( ); oldY = e.getY( );
}
public void Eraser_m1(MouseEvent e){
oldX = e.getX( ); oldY = e.getY( );
}
//-----------------------------m2----------------------------------------------
public void drawPath_m2(MouseEvent e){
currentX = e.getX( ); currentY = e.getY( );
if (g2 != null){
g2.setStroke(new BasicStroke(Frame1.selectItems_s));
g2.setPaint(Frame1.color);
g2.drawLine(oldX, oldY, currentX, currentY);
}
repaint();
oldX = currentX; oldY = currentY;
}
public void strightLine_m2(MouseEvent e){
currentX = e.getX( ); currentY = e.getY( );
if (g2 != null){
g2.setStroke(new BasicStroke(Frame1.selectItems_s));
g2.setPaint(Frame1.color);
g2.drawLine(oldX, oldY, currentX, currentY);
}
repaint();
oldX = currentX; oldY = currentY;
}
public void DrawOval_m2(MouseEvent e){
currentX = e.getX( ); currentY = e.getY( );
if (g2 != null){
g2.setStroke(new BasicStroke(Frame1.selectItems_s));
g2.setPaint(Frame1.color);
g2.drawOval(oldX, oldY, Math.abs(oldX-currentX), Math.abs(oldY-currentY));
}
repaint();
oldX = currentX; oldY = currentY;
}
public void Eraser_m2(MouseEvent e){
currentX = e.getX( ); currentY = e.getY( );
if (g2 != null){
g2.setStroke(new BasicStroke(Frame1.selectItems_s));
g2.setPaint(Color.white);
g2.drawLine(oldX, oldY, currentX, currentY);
}
repaint();
oldX = currentX; oldY = currentY;
}
}
--
我想當一個壞人.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 68.187.13.21