要实现的功能:
1.在画板上绘制一些图形。
2.把这些图形的参数按照一定规则写入一个文件中。
3.读取这个文件,把图形绘制于画板上。
4.在画板上绘制一张指定图片。
5.将图片的信息按照一定规则写入一个文件中保存。
6.读取这个文件把图片再次绘制在画板上。
思路:
图形的绘制与保存:
按照图形类型的不同建立几个类,它们共有一个父类。绘制图形时,把图形信息存入队列中。
保存图形时,打开文件,遍历队列,输出总共的图形数量。逐个确定结点所属的图形类型,输出标号,再把坐标,长度,颜色等信息都输入。
读取图形时,打开文件,读取图形总数。通过标号,确定每个结点的图形类型,读取坐标,长度,颜色等信息,将图形绘制在画板上。
类 BufferedImage:
BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。BufferedImage 由图像数据的 ColorModel 和 Raster 组成。Raster 的 SampleModel 中 band 的数量和类型必须与 ColorModel 所要求的数量和类型相匹配,以表示其颜色和 alpha 分量。所有 BufferedImage 对象的左上角坐标都为 (0, 0)。因此,用来构造 BufferedImage 的任何 Raster 都必须满足:minX=0 且 minY=0。
图像的打开,保存,与读取:
图像的打开:
建立一个ImageIcon,把图画在画纸上,再把画纸贴在画板上。
示例:
ImageIcon image=new ImageIcon("image/1.jpg");
BI=new BufferedImage(image.getIconWidth(), image.getIconHeigh(),BufferedImage.TYPE_INT_RGB);
Graphics g2=BI.getGraphics();
g2.drawImage(image.getImage(),0,0,null);
g.drawImage(BI,50,150,null);
图像的保存:
得到图像的大小,输出。
循环,把每一个点的颜色写在文件内保存。
图形的读取:
打开文件,读取图像大小。循环,把每一个点的颜色读出来,在相应的坐标上画一个该种颜色的点。
package IR20130720;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class DrawFrame extends JFrame{
ArrayList<shape> lineList = new ArrayList<shape>();
Graphics g;
int x1,y1,x2,y2;
String type="line";
BufferedImage BI;
/**
* @param args
*/
public static void main(String[] args) {
DrawFrame df=new DrawFrame();
df.InitGUI();
}
private void InitGUI() {
this.setTitle("画板");
this.setSize(new Dimension(800,600));
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setVisible(true);
g=this.getGraphics();
//鼠标监听器
this.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
x1=e.getX();
y1=e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
x2=e.getX();
y2=e.getY();
//把线的信息存入队列中
if(type.equals("line")){
Random random=new Random();
int c=100+random.nextInt(155);
g.setColor(new Color((100),(200),c));
line line1=new line();
line1.setX1(x1);
line1.setX2(x2);
line1.setY1(y1);
line1.setY2(y2);
line1.setColor(g.getColor());
lineList.add(line1);
g.drawLine(x1, y1, x2, y2);
}
//把圆的信息存入类中
if(type.equals("oval")){
Random random=new Random();
int c=100+random.nextInt(155);
g.setColor(new Color((100),(200),c));
oval line1=new oval();
int r1=Math.abs(x1-x2);
int r2=Math.abs(y1-y2);
line1.setX1(x1);
line1.setY1(y1);
line1.setR1(r1);
line1.setR2(r2);
line1.setColor(g.getColor());
lineList.add(line1);
g.fillOval(x1, y1, r1, r2);
}
if(type.equals("square")){
Random random=new Random();
int c=100+random.nextInt(155);
g.setColor(new Color((100),(200),c));
square line1=new square();
int r1=Math.abs(x1-x2);
int r2=Math.abs(y1-y2);
line1.setX1(x1);
line1.setY1(y1);
line1.setR1(r1);
line1.setR2(r2);
line1.setColor(g.getColor());
lineList.add(line1);
g.fillRect(x1, y1, r1, r2);
}
}
});
JMenuBar MenuBar=new JMenuBar();
JMenu Menu=new JMenu("文件");
JMenuItem open=new JMenuItem("打开");
JMenuItem save=new JMenuItem("保存");
JMenuItem exit=new JMenuItem("退出");
Menu.add(open);
Menu.add(save);
Menu.add(exit);
JMenu Menu2=new JMenu("选择画笔");
JMenuItem circle=new JMenuItem("画圆");
JMenuItem line=new JMenuItem("画线");
JMenuItem square=new JMenuItem("画方框");
JMenuItem pen=new JMenuItem("笔");
Menu2.add(circle);
Menu2.add(line);
Menu2.add(square);
Menu2.add(pen);
JMenu Menu3=new JMenu("图片操作");
JMenuItem savepic=new JMenuItem("保存图片");
JMenuItem openpic=new JMenuItem("打开图片");
JMenuItem drawpic=new JMenuItem("绘制图片");
Menu3.add(savepic);
Menu3.add(openpic);
Menu3.add(drawpic);
MenuBar.add(Menu);
MenuBar.add(Menu2);
MenuBar.add(Menu3);
this.setJMenuBar(MenuBar);
//菜单的动作监听器
ActionListener l2=new ActionListener(){
public void actionPerformed(ActionEvent e) {
String cmd=e.getActionCommand();
if("打开".equals(cmd)){
open();
}
if("保存".equals(cmd)){
save();
}
if("笔".equals(cmd)){
type="pen";
}
if("画线".equals(cmd)){
type="line";
}
if("画圆".equals(cmd)){
type="oval";
}
if("画方框".equals(cmd)){
type="square";
}
if("打开图片".equals(cmd)){
openpic();
}
if("保存图片".equals(cmd)){
savepic();
}
if("绘制图片".equals(cmd)){
drawpic();
}
}
//绘制图片操作
private void drawpic() {
try{
FileInputStream fin=new FileInputStream("F:/myimage.myjpg");
DataInputStream din=new DataInputStream(fin);
int width=din.readInt();
int heigh=din.readInt();
for(int i=0;i<width;i++){
for(int j=0;j<heigh;j++){
int rbg=din.readInt();
g.setColor(new Color(rbg));
g.drawLine(i+50, j+150, i+50, j+150);
}
}
din.close();
fin.close();
}catch (Exception e) {
e.printStackTrace();
}
}
//保存图片操作
private void savepic() {
try{
FileOutputStream fcl=new FileOutputStream("F:/myimage.myjpg");
DataOutputStream dcl=new DataOutputStream(fcl);
int width=BI.getWidth();
int height=BI.getHeight();
dcl.writeInt(width);
dcl.writeInt(height);
for(int i=0;i<width;i++){
for(int j=0;j<height;j++){
int rbg=BI.getRGB(i,j);
dcl.writeInt(rbg);
}
}
fcl.close();
dcl.close();
}catch (Exception e) {
e.printStackTrace();
}
}
//打开图片操作
private void openpic() {
ImageIcon image=new ImageIcon("image/1.jpg");
BI=new BufferedImage(image.getIconWidth(), image.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g2=BI.getGraphics();
g2.drawImage(image.getImage(),0,0,null);
g.drawImage(BI,50,150,null);
}
//保存文件
private void save() {
try{
java.io.FileOutputStream fop = new FileOutputStream("F:/pic.txt");
DataOutputStream dop = new DataOutputStream(fop);
dop.writeInt(lineList.size());
for(int i=0;i<lineList.size();i++){
shape s = lineList.get(i);
if(s instanceof line){
dop.writeInt(1);
line line1=(line) lineList.get(i);
dop.writeInt(line1.getX1());
dop.writeInt(line1.getY1());
dop.writeInt(line1.getX2());
dop.writeInt(line1.getY2());
dop.writeInt(line1.getColor().getRGB());
}
if(s instanceof oval){
dop.writeInt(2);
oval line1=(oval) lineList.get(i);
dop.writeInt(line1.getX1());
dop.writeInt(line1.getY1());
dop.writeInt(line1.getR1());
dop.writeInt(line1.getR2());
dop.writeInt(line1.getColor().getRGB());
}
if(s instanceof square){
dop.writeInt(3);
square line1=(square) lineList.get(i);
dop.writeInt(line1.getX1());
dop.writeInt(line1.getY1());
dop.writeInt(line1.getR1());
dop.writeInt(line1.getR2());
dop.writeInt(line1.getColor().getRGB());
}
}
fop.close();
dop.close();
} catch (Exception e1) {
e1.printStackTrace();
}
System.out.println("保存成功!");
}
private void open() {
try{java.io.FileInputStream fcl = new FileInputStream("F:/pic.txt");
DataInputStream dcl = new DataInputStream(fcl);
int tot=dcl.readInt();
for(int i=0;i<tot;i++){
int kind = dcl.readInt();
if(kind==1){
line line1 = new line();
line1.setX1(dcl.readInt());
line1.setY1(dcl.readInt());
line1.setX2(dcl.readInt());
line1.setY2(dcl.readInt());
line1.setColor(new Color(dcl.readInt()));
g.setColor(line1.getColor());
g.drawLine(line1.getX1(), line1.getY1(), line1.getX2(), line1.getY2());
}
if(kind==2){
oval line1 = new oval();
line1.setX1(dcl.readInt());
line1.setY1(dcl.readInt());
line1.setR1(dcl.readInt());
line1.setR2(dcl.readInt());
line1.setColor(new Color(dcl.readInt()));
g.setColor(line1.getColor());
g.fillOval(line1.getX1(), line1.getY1(), line1.getR1(), line1.getR2());
}
if(kind==3){
square line1 = new square();
line1.setX1(dcl.readInt());
line1.setY1(dcl.readInt());
line1.setR1(dcl.readInt());
line1.setR2(dcl.readInt());
line1.setColor(new Color(dcl.readInt()));
g.setColor(line1.getColor());
g.fillRect(line1.getX1(), line1.getY1(), line1.getR1(), line1.getR2());
}
}
}catch (Exception e1) {
e1.printStackTrace();
}
}
};
open.addActionListener(l2);
save.addActionListener(l2);
exit.addActionListener(l2);
circle.addActionListener(l2);
line.addActionListener(l2);
pen.addActionListener(l2);
square.addActionListener(l2);
savepic.addActionListener(l2);
openpic.addActionListener(l2);
drawpic.addActionListener(l2);
}
}