import java.awt.*;
import java.util.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}public class MakeJPEG extends Canvas implements MouseMotionListener,MouseListener,ActionListener
{
int x=-1,y=-1,橡皮擦通知=0,清除通知=0;
Vector v=null;int n=1;
Graphics2D ggg ; //负责将图形绘制到image的画笔对象
BufferedImage image; //图像对象
Frame window;
Button 保存,调色板,橡皮,清除,画笔,获取屏幕,绘制图形;
Color 画笔颜色;
Panel pCenter,pSouth,pNorth;
public MakeJPEG()
{
保存=new Button("将绘制的图形或屏幕保存为JPG文件");
获取屏幕=new Button("获取屏幕");
绘制图形=new Button("绘制图形");
调色板=new Button("打开调色板");
画笔=new Button("画笔");
橡皮=new Button("橡皮");
清除=new Button("清除");
调色板.addActionListener(this);
绘制图形.addActionListener(this);
保存.addActionListener(this);
画笔.addActionListener(this);
橡皮.addActionListener(this);
清除.addActionListener(this);
获取屏幕.addActionListener(this);
画笔颜色=new Color(0,0,0);
addMouseMotionListener(this);
addMouseListener(this);
v=new Vector();
setBackground(Color.white); //设置画布颜色
//创建一个宽高各为210个像素的待绘制的image对象
image=new BufferedImage(210,210,BufferedImage.TYPE_INT_RGB);
ggg=image.createGraphics(); //创建在image对象中进行绘制的画笔对象
Rectangle2D rect=new Rectangle2D.Double(0,0,210,210);
ggg.setColor(getBackground());
ggg.fill(rect); //将图像的底色设置为和画布相同
window=new Frame("JPEG图像生成器");
pCenter=new Panel();
pCenter.setLayout(null);
pCenter.add(this);
pCenter.setBackground(Color.gray);
this.setBounds(80,30,210,210); //设置画布的大小和图像的大小相同
window.add(pCenter,BorderLayout.CENTER);
pNorth=new Panel();
pNorth.add(保存);
pNorth.add(绘制图形);
pNorth.add(获取屏幕);
window.add(pNorth,BorderLayout.NORTH);
pSouth=new Panel();
pSouth.add(调色板);
pSouth.add(橡皮);
pSouth.add(清除);
pSouth.add(画笔);
window.add(pSouth,BorderLayout.SOUTH);
window.setVisible(true);
window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
window.setBounds(100,80,390,380);
window.validate(); //确保窗口中的组件能正确显示出来
}
public void paint(Graphics g) //负责在画布中绘制图形的笔画对象
{
if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0)
{
g.setColor(画笔颜色);
n=v.size();
for(int i=0;i<n-1;i++)
{
Point p1=(Point)v.elementAt(i);
Point p2=(Point)v.elementAt(i+1);
g.drawLine(p1.x,p1.y,p2.x,p2.y); //画笔g在画布中绘制图形
ggg.setColor(g.getColor());
ggg.drawLine(p1.x,p1.y,p2.x,p2.y); //同时画笔ggg也在image中绘制图形
}
}
else if(橡皮擦通知==1&&清除通知==0) //擦去4X4大小区域的图形
{
g.setColor(getBackground());
g.fillRect(x-2,y-2,4,4);
ggg.setColor(getBackground());
ggg.fillRect(x-2,y-2,4,4);
}
else if(清除通知==1&&橡皮擦通知==0) //清除画布及image中的图形
{
g.setColor(getBackground());
g.fillRect(0,0,200,200);
ggg.setColor(getBackground());
ggg.fillRect(0,0,200,200);
} g.drawImage(image,0,0,200,200,this); //将image对象绘制到画布中
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
Point p=new Point(x,y);
v.addElement(p);
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e) //放开鼠标,则清除图形向量
{
v.removeAllElements();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void update(Graphics g)
{
{
paint(g);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==橡皮)
{
橡皮擦通知=1;
清除通知=0 ;
}
else if(e.getSource()==清除)
{
清除通知=1;
橡皮擦通知=0;
repaint();
}
else if(e.getSource()==画笔)
{
橡皮擦通知=0;
清除通知=0;
} else if(e.getSource()==保存)
{
//显示“保存文件”对话框
FileDialog savedialog=new FileDialog(window,"保存图型到JPG格式",FileDialog.SAVE);
//该对话框是有模式对话框,只有收到响应,程序才能继续执行
savedialog.setVisible(true);
//选择对话框的yes或cancel按钮后,对话框自动关闭,程序继续执行
if(savedialog.getFile()!=null)
//如果选择了yes,getFile()将返回文件的名字
{
try{
//JPEG文件的命名由对话框负责
String fileName=savedialog.getFile();
FileOutputStream out=new FileOutputStream(fileName);
//获取一个JPEG编码器
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fileName);
//获取解码器的编码参数
JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);
//将图像质量设置为100%
param.setQuality(1.0f,false);
encoder.setJPEGEncodeParam(param);
//编码器负责将image写入输出流out
encoder.encode(image);
out.close();
}
catch(Exception EE)
{
}
}
}
else if(e.getSource()==获取屏幕)
{
Robot robot=null; //用来获取屏幕图像的Robot对象
try{
robot=new Robot();
}
catch(Exception er)
{
}
Rectangle screenRect=null; //用来确定image大小的矩形对象
int width=getToolkit().getScreenSize().width; //获取屏幕的宽
int height=getToolkit().getScreenSize().height; //获取屏幕的高
screenRect=new Rectangle(0,0,width,height);
window.setVisible(false); //隐去当前Java程序窗口,以使获取屏幕图像
this.window.setVisible(false);
image=robot.createScreenCapture(screenRect); //将屏幕图像存入image
window.setVisible(true); //重新显示当前程序窗口
repaint();
}
else if(e.getSource()==调色板) //选择画颜色
{
Color tempColor=JColorChooser.showDialog(window,"调色板",画笔颜色);
{
if(tempColor!=null)
{
画笔颜色=tempColor;
画笔.setForeground(画笔颜色);
}
}
}
else if(e.getSource()==绘制图形)
{
window.dispose(); //释放窗口
this.window.dispose();
MakeJPEG canvas=new MakeJPEG(); //创建画布对象
}
}
public static void main(String args[])
{
new MakeJPEG();
}
}
import java.util.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}public class MakeJPEG extends Canvas implements MouseMotionListener,MouseListener,ActionListener
{
int x=-1,y=-1,橡皮擦通知=0,清除通知=0;
Vector v=null;int n=1;
Graphics2D ggg ; //负责将图形绘制到image的画笔对象
BufferedImage image; //图像对象
Frame window;
Button 保存,调色板,橡皮,清除,画笔,获取屏幕,绘制图形;
Color 画笔颜色;
Panel pCenter,pSouth,pNorth;
public MakeJPEG()
{
保存=new Button("将绘制的图形或屏幕保存为JPG文件");
获取屏幕=new Button("获取屏幕");
绘制图形=new Button("绘制图形");
调色板=new Button("打开调色板");
画笔=new Button("画笔");
橡皮=new Button("橡皮");
清除=new Button("清除");
调色板.addActionListener(this);
绘制图形.addActionListener(this);
保存.addActionListener(this);
画笔.addActionListener(this);
橡皮.addActionListener(this);
清除.addActionListener(this);
获取屏幕.addActionListener(this);
画笔颜色=new Color(0,0,0);
addMouseMotionListener(this);
addMouseListener(this);
v=new Vector();
setBackground(Color.white); //设置画布颜色
//创建一个宽高各为210个像素的待绘制的image对象
image=new BufferedImage(210,210,BufferedImage.TYPE_INT_RGB);
ggg=image.createGraphics(); //创建在image对象中进行绘制的画笔对象
Rectangle2D rect=new Rectangle2D.Double(0,0,210,210);
ggg.setColor(getBackground());
ggg.fill(rect); //将图像的底色设置为和画布相同
window=new Frame("JPEG图像生成器");
pCenter=new Panel();
pCenter.setLayout(null);
pCenter.add(this);
pCenter.setBackground(Color.gray);
this.setBounds(80,30,210,210); //设置画布的大小和图像的大小相同
window.add(pCenter,BorderLayout.CENTER);
pNorth=new Panel();
pNorth.add(保存);
pNorth.add(绘制图形);
pNorth.add(获取屏幕);
window.add(pNorth,BorderLayout.NORTH);
pSouth=new Panel();
pSouth.add(调色板);
pSouth.add(橡皮);
pSouth.add(清除);
pSouth.add(画笔);
window.add(pSouth,BorderLayout.SOUTH);
window.setVisible(true);
window.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
window.setBounds(100,80,390,380);
window.validate(); //确保窗口中的组件能正确显示出来
}
public void paint(Graphics g) //负责在画布中绘制图形的笔画对象
{
if(x!=-1&&y!=-1&&橡皮擦通知==0&&清除通知==0)
{
g.setColor(画笔颜色);
n=v.size();
for(int i=0;i<n-1;i++)
{
Point p1=(Point)v.elementAt(i);
Point p2=(Point)v.elementAt(i+1);
g.drawLine(p1.x,p1.y,p2.x,p2.y); //画笔g在画布中绘制图形
ggg.setColor(g.getColor());
ggg.drawLine(p1.x,p1.y,p2.x,p2.y); //同时画笔ggg也在image中绘制图形
}
}
else if(橡皮擦通知==1&&清除通知==0) //擦去4X4大小区域的图形
{
g.setColor(getBackground());
g.fillRect(x-2,y-2,4,4);
ggg.setColor(getBackground());
ggg.fillRect(x-2,y-2,4,4);
}
else if(清除通知==1&&橡皮擦通知==0) //清除画布及image中的图形
{
g.setColor(getBackground());
g.fillRect(0,0,200,200);
ggg.setColor(getBackground());
ggg.fillRect(0,0,200,200);
} g.drawImage(image,0,0,200,200,this); //将image对象绘制到画布中
}
public void mouseDragged(MouseEvent e)
{
x=(int)e.getX();
y=(int)e.getY();
Point p=new Point(x,y);
v.addElement(p);
repaint();
}
public void mouseMoved(MouseEvent e)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e) //放开鼠标,则清除图形向量
{
v.removeAllElements();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void update(Graphics g)
{
{
paint(g);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==橡皮)
{
橡皮擦通知=1;
清除通知=0 ;
}
else if(e.getSource()==清除)
{
清除通知=1;
橡皮擦通知=0;
repaint();
}
else if(e.getSource()==画笔)
{
橡皮擦通知=0;
清除通知=0;
} else if(e.getSource()==保存)
{
//显示“保存文件”对话框
FileDialog savedialog=new FileDialog(window,"保存图型到JPG格式",FileDialog.SAVE);
//该对话框是有模式对话框,只有收到响应,程序才能继续执行
savedialog.setVisible(true);
//选择对话框的yes或cancel按钮后,对话框自动关闭,程序继续执行
if(savedialog.getFile()!=null)
//如果选择了yes,getFile()将返回文件的名字
{
try{
//JPEG文件的命名由对话框负责
String fileName=savedialog.getFile();
FileOutputStream out=new FileOutputStream(fileName);
//获取一个JPEG编码器
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fileName);
//获取解码器的编码参数
JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);
//将图像质量设置为100%
param.setQuality(1.0f,false);
encoder.setJPEGEncodeParam(param);
//编码器负责将image写入输出流out
encoder.encode(image);
out.close();
}
catch(Exception EE)
{
}
}
}
else if(e.getSource()==获取屏幕)
{
Robot robot=null; //用来获取屏幕图像的Robot对象
try{
robot=new Robot();
}
catch(Exception er)
{
}
Rectangle screenRect=null; //用来确定image大小的矩形对象
int width=getToolkit().getScreenSize().width; //获取屏幕的宽
int height=getToolkit().getScreenSize().height; //获取屏幕的高
screenRect=new Rectangle(0,0,width,height);
window.setVisible(false); //隐去当前Java程序窗口,以使获取屏幕图像
this.window.setVisible(false);
image=robot.createScreenCapture(screenRect); //将屏幕图像存入image
window.setVisible(true); //重新显示当前程序窗口
repaint();
}
else if(e.getSource()==调色板) //选择画颜色
{
Color tempColor=JColorChooser.showDialog(window,"调色板",画笔颜色);
{
if(tempColor!=null)
{
画笔颜色=tempColor;
画笔.setForeground(画笔颜色);
}
}
}
else if(e.getSource()==绘制图形)
{
window.dispose(); //释放窗口
this.window.dispose();
MakeJPEG canvas=new MakeJPEG(); //创建画布对象
}
}
public static void main(String args[])
{
new MakeJPEG();
}
}
解决方案 »
- 数据备份的方法
- 多个线程同时写一个文件,该怎么做?
- 使用java正则表达式将绝对地址转换为相对地址
- 龙龙问题
- 求助!如何实现用java对swf格式进行缩放
- 关于变量作用域的问题,请赐教
- int[] a2 = new int[10]; Arrays.fill(a1, 47); 数组a2的所有元素都引用一个对象 "47"; 还是有10个"47"对象一一对应a2中的每个元素
- 一个java小程序 请大家帮小妹一下 多谢了 在线等待
- 如何下载 .txt .xml 文件?
- Think in Java 大讨论一 ( 关于 Inner class )
- 在netbeans环境下的JAVA倒计时器怎样做
- 怎么发布java程序给其他人用
try{
//JPEG文件的命名由对话框负责
String fileName=savedialog.getFile();
FileOutputStream out=new FileOutputStream(fileName);
//获取一个JPEG编码器
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fileName);
//获取解码器的编码参数
JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);
//将图像质量设置为100%
param.setQuality(1.0f,false);
encoder.setJPEGEncodeParam(param);
//编码器负责将image写入输出流out
encoder.encode(image);
out.close();
}
这段代码有问题,不知道如何修改
应该修改为:JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
这句话错误显示为Rename in file,导致后面的代码也无法正确运行,5楼的改法也有问题
改如何修改呢,跪求……
* 生成文件
* @param path (String)生成图片的路径+文件名
* @param image (BufferedImage)要生成文件的BufferedImage
* @return boolean 成功true,失败false
*/
private boolean createJpgFile(String path,BufferedImage image) {
boolean b=false;
FileOutputStream fos = null;
BufferedOutputStream bos= null;
try {
fos = new FileOutputStream(path);
bos = new BufferedOutputStream(fos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(image);
bos.close();
b=true;
} catch(Exception e) {
e.printStackTrace();
}finally{
try{
if(bos!=null)
bos.close();
if(fos!=null)
fos.close();
}catch(Exception e1){}
}
return b;
}
bos.close(); //这个去掉,顺手写多了
b=true;
String fileName=savedialog.getFile();
FileOutputStream out=new FileOutputStream(fileName);
//获取一个JPEG编码器
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fileName);
//获取解码器的编码参数
JPEGEncodeParam param=encoder.getDefaultJPEGEncodeParam(image);
中的JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(fileName);
改为JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);然后解决Eclipse的设置问题
这个事eclipse的设置问题,它默认把这些受访问限制的API设成了ERROR,你只要把
Windows-Preferences-Java-Complicer-Errors/Warnings
里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过了。成功通过,真是太有才了,哈哈