Discussion:
[問題] JPanel可以轉成Image嗎
(时间太久无法回复)
徵MVP連線
2006-10-17 16:04:24 UTC
Permalink
請問板上各位前輩

用Graphics在JPanel上面繪圖之後

可以把JPanel目前的圖像轉成一個Image物件嗎

--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.217.17
※ 編輯: ligod 來自: 140.115.217.17 (10/18 08:04)
t***@kkcity.com.tw
2006-10-18 02:28:08 UTC
Permalink
Post by 徵MVP連線
請問板上各位前輩
用Graphics在JPanel上面繪圖之後
可以把JPanel目前的圖像轉成一個Image物件嗎
我不知道有沒有這種方法

但換個方式的話我倒會一點,
可以利用getGraphics()這種方法取得一個BufferedImage
到時在把BufferedImage貼在JPanel上,
但同時也可以將BufferedImage轉型成Image物件,
當然,BufferedImage本身也算是Image物件的一種
(關於這方面的知識我很陌生,無法幫您講解這一塊....)
--
┌─────◆KKCITY◆─────┐ ◢ ╱  想要成立班系社團站台嗎? 
│ bbs.kkcity.com.tw │ █▉ ─ KKcity即日起開放BBS站申請囉!
└──《From:61.62.107.41 》──┘ ◥ ╲ 免程式技術、硬體成本的選擇!!
--
偽.強者
2006-10-17 21:15:00 UTC
Permalink
※ 引述《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

继续阅读narkive:
Loading...