import java.awt.*; import java.awt.image.*;public class MyApplet extends Applet { .... .... whatever .... ....Image greyscaleImage = null;public int[] convertToGreyscale(Image image, int width, int height) { int pixels[] = new int[width*height]; try { // ********************************************* // ***************** I CHANGE THIS ************* // ********************************************* PixelGrabber pG = new PixelGrabber(image, 0,0,width,height,pixels,0,width); if (pG.grabPixels()) { for (int cnt = 0; cnt < pixels.length; cnt++) { int alpha = (pixels[cnt] >> 24) & 0xff; int red = (pixels[cnt] >> 16) & 0xff; int green = (pixels[cnt] >> 8) & 0xff; int blue = (pixels[cnt] ) & 0xff; int all = (int) ((red+green+blue)/3); pixels[cnt]=(alpha << 24) + (all << 16) + (all << 8) + all; } } { System.out.println("Cannot grab pixel"); } } catch(Exception e) { System.out.println("PixelGrabber is interrupted");} return pixels; }public void init() { Image img = ... get the image from anywhere ...; // get size of the original image int width=img.getWidth(this); int height=img.getHeight(this); // pixels is array of int[], the greyscale colors int pixels[] = convertToGreyscale(img, width, height); // now, creating greyscaleImage greyscaleImage = this.createImage(new MemoryImageSource(width, height, pixels, 0,width)); repaint(); }public void paint(Graphics gr) { // put the greyscale image to screen when ready if (greyscaleImage!=null) gr.drawImage(greyscaleImage, 0 , 0, this); } ...... ...... bla bla bla whatever ...... }
但是问题找出来了,你的sendGraph()方法的本来目的是用来远程传送图像是不是?但是你传送的Graphics对象,这么做是不可以的,原因一,这个对象能串行化吗?(不能)原因二,其实无论什么情况下你都无法传送Graphics对象,因为它是一个绘图句柄,相当于Windows中的DC。
解决办法:总之你得把这个思路推翻,你试试串行化一个Image然后用于传送(我没试过,有其他朋友知道这个方法行不通或者有更好的方法请指出)。希望你尽快把问题解决!
我关键是怎么可以把画图板的图像信息保存为可以传送的Image之类的对象呢?!
我关键是怎么可以把画图板的图像信息保存为可以传送的Image之类的对象呢?!
给你两个回答仅供参考:
(1)仔细查一下Java API看一下是否有其他类可以实现,(主要是BufferedImage及其子类,以及java.awt.image包中的内容,可能真正实现起来比较复杂)
(2)就你的要求的话,你传送的无非是些规则的图形,不妨保存Image的关键信息,比如位置、大小、颜色等等,再由接收端根据这些信息解释为正确的图形!可以考虑一下,这样的话就比较容易了,你说呢?
我的老师提供的接口是用象素数组传的我自己看书,象素数组那节没有看明白^_^b
那又是怎样的方法呢?!
import java.awt.image.*;public class MyApplet extends Applet
{
....
.... whatever ....
....Image greyscaleImage = null;public int[] convertToGreyscale(Image image, int width, int height)
{
int pixels[] = new int[width*height];
try
{
// *********************************************
// ***************** I CHANGE THIS *************
// *********************************************
PixelGrabber pG = new PixelGrabber(image,
0,0,width,height,pixels,0,width);
if (pG.grabPixels())
{
for (int cnt = 0; cnt < pixels.length; cnt++)
{
int alpha = (pixels[cnt] >> 24) & 0xff;
int red = (pixels[cnt] >> 16) & 0xff;
int green = (pixels[cnt] >> 8) & 0xff;
int blue = (pixels[cnt] ) & 0xff;
int all = (int) ((red+green+blue)/3); pixels[cnt]=(alpha << 24) + (all << 16) +
(all << 8) + all;
}
}
{ System.out.println("Cannot grab pixel"); }
}
catch(Exception e) { System.out.println("PixelGrabber is interrupted");}
return pixels;
}public void init()
{
Image img = ... get the image from anywhere ...; // get size of the original image
int width=img.getWidth(this);
int height=img.getHeight(this); // pixels is array of int[], the greyscale colors
int pixels[] = convertToGreyscale(img, width, height); // now, creating greyscaleImage
greyscaleImage = this.createImage(new MemoryImageSource(width, height, pixels, 0,width)); repaint();
}public void paint(Graphics gr)
{
// put the greyscale image to screen when ready
if (greyscaleImage!=null)
gr.drawImage(greyscaleImage, 0 , 0, this);
}
......
...... bla bla bla whatever
......
}
怎么能取到画板信息存为image对象呢!?
(通过服务器中转)
(2003-05-14 18:51:59) 肥嘟嘟同学
对象素数组工作原理还不是太明白请指点
(通过服务器中转)
我把DrawFigurePanel方法改成下面那样,发现bufferedImage截的是屏幕信息,我切换窗口,bufferedImage的内容就改变了,而且点击菜单,菜单的内容也显示在里头。我怎么才可以使bufferedImage只对画板对象截图呢?!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.image.*;public class DrawFigurePanel extends JPanel {
public Vector figures = new Vector(10,10);
public Figure figure = null;
BufferedImage bi = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics( );
public DrawFigurePanel() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
this.setBackground(UIManager.getColor("window"));
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
this_mouseDragged(e);
}
});
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
public void mousePressed(MouseEvent e) {
this_mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
this_mouseReleased(e);
}
});
}
Image greyscaleImage = null; public int[] convertToGreyscale(Image image, int width, int height)
{
int pixels[] = new int[width*height];
try
{ PixelGrabber pG = new PixelGrabber(image,
0,0,width,height,pixels,0,width);
if (pG.grabPixels())
{
for (int cnt = 0; cnt < pixels.length; cnt++)
{
int alpha = (pixels[cnt] >> 24) & 0xff;
int red = (pixels[cnt] >> 16) & 0xff;
int green = (pixels[cnt] >> 8) & 0xff;
int blue = (pixels[cnt] ) & 0xff;
int all = (int) ((red+green+blue)/3); pixels[cnt]=(alpha << 24) + (all << 16) +
(all << 8) + all;
}
}
{ System.out.println("Cannot grab pixel"); }
}
catch(Exception e) { System.out.println("PixelGrabber is interrupted");}
return pixels;
} public void init()
{
Image img = bi; // get size of the original image
int width=img.getWidth(this);
int height=img.getHeight(this); // pixels is array of int[], the greyscale colors
int pixels[] = convertToGreyscale(img, width, height); // now, creating greyscaleImage
greyscaleImage = this.createImage(new MemoryImageSource(width, height, pixels, 0,width)); repaint();
} public void paint(Graphics gr)
{
// put the greyscale image to screen when ready
if (greyscaleImage!=null)
gr.drawImage(greyscaleImage, 0 , 0, 800,600,this);}
void this_mouseClicked(MouseEvent e) {
if(figure == null)
return;
//Graphics g = this.getGraphics() ;
g.setXORMode(Color.green);
if(figure instanceof LineFigure){
LineFigure lineFigure = (LineFigure)figure;
lineFigure.draw(g);
figures.add(lineFigure.clone());
}
if(figure instanceof RectangleFigure){
RectangleFigure rectangleFigure = (RectangleFigure)figure;
rectangleFigure.draw(g);
figures.add(rectangleFigure.clone());
}
if(figure instanceof OvalFigure){
OvalFigure ovalFigure = (OvalFigure)figure;
ovalFigure.draw(g);
figures.add(ovalFigure.clone());
}
} void this_mouseDragged(MouseEvent e) {
if(figure == null)
return;
Graphics g = this.getGraphics();
g.setXORMode(Color.green);
if(figure instanceof LineFigure){
LineFigure lineFigure = (LineFigure)figure;
if(lineFigure.firstDraw){
lineFigure.draw(g);
lineFigure.firstDraw = false;
}else{
lineFigure.draw(g);
lineFigure.endPoint = e.getPoint();
lineFigure.draw(g);
}
}
if(figure instanceof RectangleFigure){
RectangleFigure rectangleFigure = (RectangleFigure)figure;
if(rectangleFigure.firstDraw){
rectangleFigure.draw(g);
rectangleFigure.firstDraw = false;
}else{
rectangleFigure.draw(g);
rectangleFigure.endPoint = e.getPoint();
rectangleFigure.draw(g);
}
}
if(figure instanceof OvalFigure){
OvalFigure ovalFigure = (OvalFigure)figure;
if(ovalFigure.firstDraw){
ovalFigure.draw(g);
ovalFigure.firstDraw = false;
}else{
ovalFigure.draw(g);
ovalFigure.endPoint = e.getPoint();
ovalFigure.draw(g);
}
} } void this_mousePressed(MouseEvent e) {
if(figure != null){
figure.startPoint = e.getPoint();
figure.endPoint = e.getPoint();
figure.firstDraw = true;
}
} void this_mouseReleased(MouseEvent e) {
if(figure instanceof LineFigure){
LineFigure lineFigure = (LineFigure)figure;
figures.add(lineFigure.clone());
}
if(figure instanceof RectangleFigure){
RectangleFigure rectangleFigure = (RectangleFigure)figure;
figures.add(rectangleFigure.clone());
}
if(figure instanceof OvalFigure){
OvalFigure ovalFigure = (OvalFigure)figure;
figures.add(ovalFigure.clone());
}
} protected void paintComponent(Graphics parm1) {
/**@todo: Override this javax.swing.JComponent method*/
super.paintComponent( parm1);
parm1.setXORMode(Color.green);
int i;
for(i=0;i<figures.size();i++){
Object x = figures.get(i);
if(x instanceof LineFigure){
LineFigure lineFigure = (LineFigure)x;
lineFigure.draw(parm1);
}
if(x instanceof RectangleFigure){
RectangleFigure rectFigure = (RectangleFigure)x;
rectFigure.draw(parm1);
}
if(x instanceof OvalFigure){
OvalFigure ovalFigure = (OvalFigure)x;
ovalFigure.draw(parm1);
}
}
}
}
But this is a chance for me to learn AWT. xixi.
If I am available, I will study your code.
Good luck!
这是一个rmi文件下载的例子
不知对你有没有帮助,我没太仔细看你的要求,不过可以先把图像文件载到本地
我的问题已经解决了,我最后采用了传向量的方法把图形一次传过去了^_^
现在我结贴了