画图板

本文介绍了一个简单的Java画图板程序实现,支持多种图形绘制如直线、椭圆等,并提供了图形保存和加载的功能。文中详细展示了如何使用Java AWT和Swing库来创建用户界面及图形绘制逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该文章简要介绍java画图板的思路。

一、多种画图的函数

//绘制图形

        if(type.equals("Line")){

           g.drawLine(x1, y1, x2, y2);//(x1,y1)和(x2,y2)为直线的起点和终点

           shape sh = new shape(1,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Oval")){

           g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//指定矩形边界内画圆

           shape sh = new shape(2,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Arc")){

           String str1 = JOptionPane.showInputDialog(null, "请输入起始角度");

           int r1 = Integer.parseInt(str1);

           String str2 = JOptionPane.showInputDialog(null, "请输入结束角度");

           int r2 = Integer.parseInt(str2);

           g.drawArc(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2), r1, r2);//指定矩形边界内从起始角度到结束角度之间画弧

           shape sh = new shape(3,x1, y1, x2, y2,r1, r2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Rect")){

           g.drawRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽

           shape sh = new shape(4,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("RoundRect")){

           g.drawRoundRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/6, Math.abs(y1-y2)/6);//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽,Math.abs(x1-x2)/6为圆角的弧宽, Math.abs(y1-y2/6为圆角的弧高

           shape sh = new shape(5,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Bytes")){

           String str1 = JOptionPane.showInputDialog(null, "请输入所需显示的字符串");

           byte []data = str1.getBytes();

           g.drawBytes(data,0,data.length,x1, y1);

           shape sh = new shape(6,x1,y1,data, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Chars")){

           String str1 = JOptionPane.showInputDialog(null, "请输入所需显示的字符串");

           char []data = str1.toCharArray();

           g.drawChars(data,0,data.length,x1, y1);

           shape sh = new shape(7,x1,y1,data, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("String")){

           String str1 = JOptionPane.showInputDialog(null, "请输入所需显示的字符串");

           g.drawString(str1,x1, y1);

           shape sh = new shape(8,x1,y1,str1, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Image")){

          

           g.drawImage(im.getImage(), x1, y1, Math.abs(x1-x2), Math.abs(y1-y2), this);

           shape sh = new shape(9,x1, y1, x2, y2);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillOval")){

           g.fillOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//指定矩形边界内画椭圆

           shape sh = new shape(10,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillArc")){

           String str1 = JOptionPane.showInputDialog(null, "请输入起始角度");

           int r1 = Integer.parseInt(str1);

           String str2 = JOptionPane.showInputDialog(null, "请输入结束角度");

           int r2 = Integer.parseInt(str2);

           g.fillArc(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2), r1, r2);//指定矩形边界内从起始角度到结束角度之间画弧

           shape sh = new shape(11,x1, y1, x2, y2,r1, r2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillRect")){

           g.fillRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽

           shape sh = new shape(12,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillRoundRect")){

           g.fillRoundRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/6, Math.abs(y1-y2)/6);//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽,Math.abs(x1-x2)/6为圆角的弧宽, Math.abs(y1-y2/6为圆角的弧高

           shape sh = new shape(13,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

    }

if(type.equals("Draw")){

           g.drawLine(x1,y1,x3,y3);//(x1,y1)和(x2,y2)为直线的起点和终点

           shape sh = new shape(1,x1, y1, x3, y3, c);

           s.add(sh);

           index++;

           x1 = x3;

           y1 = y3;

        }

        else if(type.equals("Line")){

           dr.paint(g);

           g.drawLine(x1,y1,x3,y3);

    }

并且通过paint()函数实现画图板的重绘。

二、画图板信息的保存

通过DataOutputStream类的包装,将图形的信息输出并保存在指定文件里面。

public void saveShape(String pathname) throws IOException{

        OutputStream out = new FileOutputStream(pathname);

        DataOutputStream dop = new DataOutputStream(out);

        dop.writeInt(s.size());

        for(int i = 0;i<s.size();i++){

           if(s.get(i).name==1){

               dop.writeInt(1);//1为直线

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==2){

               dop.writeInt(2);//2为椭圆

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==3){

               dop.writeInt(3);//3Arc

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).arc1);

               dop.writeInt(s.get(i).arc2);

               dop.writeInt(s.get(i).color.getRGB());       

           }

           else if(s.get(i).name==4){

               dop.writeInt(4);//4为长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==5){

               dop.writeInt(5);//5为圆角长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==6){

               dop.writeInt(6);//6为字节

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).data1.length);//字节数组长度

               dop.write(s.get(i).data1);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==7){

               dop.writeInt(7);//7为字符

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               String st = new String(s.get(i).data2);

               dop.writeInt(st.getBytes().length);//字节数组长度

               dop.write(st.getBytes());

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==8){

               dop.writeInt(8);//8为字符串

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).str1.getBytes().length);//字节数组长度

               dop.write(s.get(i).str1.getBytes());

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==9){

               dop.writeInt(9);//9为图片

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

           }

           else if(s.get(i).name==10){

               dop.writeInt(10);//10为椭圆

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==11){

               dop.writeInt(11);//11Arc

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).arc1);

               dop.writeInt(s.get(i).arc2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==12){

               dop.writeInt(12);//12为长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==13){

               dop.writeInt(13);//13为圆角长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==14){

               dop.writeInt(14);//14为多边形

               dop.writeInt(s.get(i).num);

               for(int j = 0;j<s.get(i).num;j++){

                   dop.writeInt(s.get(i).X[j]);

                   dop.writeInt(s.get(i).Y[j]);

               }

               dop.writeInt(s.get(i).color.getRGB());

           }

        }

}

二、画图板信息的读取

通过DataInputStream类,将图形的信息从指定文件中输出并用重绘函数绘制在画图板上。读取顺序必须与存储信息的顺序一致。

public void readShape(DataInputStream in) throws IOException{

        int size = in.readInt();

        for(int i = 0;i<size;i++){

           shape sh = new shape();

           sh.name = in.readInt();

           if(sh.name == 1){//直线

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 2){//椭圆

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 3){//arc

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.arc1 = in.readInt();

               sh.arc2 = in.readInt();

               sh.color = new Color(in.readInt());      

           }

           else if(sh.name == 4){//长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 5){//圆角长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 6){//字节

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               int length = in.readInt();

               sh.data1 = new byte[length];

               in.read(sh.data1);

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 7){//字符

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               int length = in.readInt();

               byte[] bArr = new byte[length];

               in.read(bArr);

               String a = new String(bArr);

               sh.data2 = a.toCharArray();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 8){//字符串

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               int length = in.readInt();

               byte[] bArr = new byte[length];

               in.read(bArr);

               sh.str1 = new String(bArr);

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 9){//图片

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

           }

           else if(sh.name == 10){//椭圆

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 11){//arc

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.arc1 = in.readInt();

               sh.arc2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 12){//fill长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 13){//fill圆角长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 14){//多边形

               sh.num = in.readInt();

               for(int j = 0;j<sh.num;j++){

                   sh.X[j] = in.readInt();

                   sh.Y[j] = in.readInt();

               }

               sh.color = new Color(in.readInt());

           }

           s.add(sh);

        }

    }

 

下附完整代码:

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

 

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

 

public class drawing extends JPanel {

 

    /**

     * @param args

     */

    private ArrayList<shape> s;

    private ImageIcon im = new ImageIcon(this.getClass().getResource("login.png"));

 

    public static void main(String[] args) {

        // TODO Auto-generatedmethod stub

        drawing d = new drawing();

        d.UIinit();

    }

 

    public void UIinit() {

        JFrame frame = new JFrame();

        frame.setTitle("画图");

        frame.setSize(700, 600);

        frame.setDefaultCloseOperation(3);

        frame.setLocationRelativeTo(null);

 

        // 实例化drawinglistener事件处理类的对象

        drawlistener dl = new drawlistener(this);

 

        // 北边面板

        JPanel jp1 = new JPanel();

        jp1.setBackground(Color.GRAY);

        jp1.setPreferredSize(new Dimension(0, 110));

        // 添加按钮

        String[] name = { "Draw","Line", "Oval", "Arc", "Rect", "RoundRect",

               "Polygon", "Bytes", "Chars", "String", "Image", "FillOval",

               "FillArc", "FillRect", "FillRoundRect", 打开  ", 保存  " };

        for (int i = 0; i < name.length; i++) {

           JButton bu = new JButton(name[i]);

           bu.addActionListener(dl);// 按钮上的监听

           jp1.add(bu);

        }

        Color[] color = {Color.red,Color.blue,Color.GREEN,Color.cyan};

        for (int j = 0; j < color.length; j++){

           JButton bu = new JButton();

           bu.setBackground(color[j]);

           bu.setPreferredSize(new Dimension(30, 30));

           bu.addActionListener(dl);// 按钮上的监听

           jp1.add(bu);

        }

 

        frame.add(jp1, BorderLayout.NORTH);

 

        // 中间画图面板

        this.setBackground(Color.WHITE);

        frame.add(this, BorderLayout.CENTER);

 

        frame.setVisible(true);

        // 添加鼠标监听

        this.addMouseListener(dl);

        this.addMouseMotionListener(dl);

        s = dl.getshape();

    }

 

    public void paint(Graphics g) {

        super.paint(g);

 

        for (int i = 0; i < s.size(); i++) {

           if (s.get(i) != null) {

               g.setColor(s.get(i).color);

               if (s.get(i).name==1) {

                   g.drawLine(s.get(i).x1, s.get(i).y1, s.get(i).x2, s.get(i).y2);

               }

               else if (s.get(i).name==1) {

                   g.drawLine(s.get(i).x1,s.get(i).y1,s.get(i).x2,s.get(i).y2);

               }

               else if (s.get(i).name==2) {

                   g.drawOval(s.get(i).x1, s.get(i).y1, Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2));

               }

               else if (s.get(i).name==3) {

                   g.drawArc(s.get(i).x1, s.get(i).y1, Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2),s.get(i).arc1,s.get(i).arc2);

               }

               else if (s.get(i).name==4) {

                   g.drawRect(s.get(i).x1, s.get(i).y1, Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2));

               }

               else if (s.get(i).name==5) {

                   g.drawRoundRect(s.get(i).x1, s.get(i).y1,

                           Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2),

                           Math.abs(s.get(i).x1 - s.get(i).x2) / 6,

                           Math.abs(s.get(i).y1 - s.get(i).y2) / 6);

               }

               else if (s.get(i).name==6) {

                   g.drawBytes(s.get(i).data1, 0, s.get(i).data1.length, s.get(i).x1,

                           s.get(i).y1);

               }

               else if (s.get(i).name==7) {

                   g.drawChars(s.get(i).data2, 0, s.get(i).data2.length, s.get(i).x1,

                           s.get(i).y1);

               }

               else if (s.get(i).name==8) {

                   g.drawString(s.get(i).str1, s.get(i).x1, s.get(i).y1);

               }

               else if (s.get(i).name==9) {

                   g.drawImage(im.getImage(),s.get(i).x1, s.get(i).y1,

                           Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2), this);

               }

               else if (s.get(i).name==10) {

                   g.fillOval(s.get(i).x1, s.get(i).y1, Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2));

               }

               else if (s.get(i).name==11) {

                   g.fillArc(s.get(i).x1, s.get(i).y1, Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2), s.get(i).arc1, s.get(i).arc2);// 指定矩形边界内从起始角度到结束角度之间画弧

               }

               else if (s.get(i).name==12) {

                   g.fillRect(s.get(i).x1, s.get(i).y1, Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2));

               }

               else if (s.get(i).name==13) {

                   g.fillRoundRect(s.get(i).x1, s.get(i).y1,

                           Math.abs(s.get(i).x1 - s.get(i).x2),

                           Math.abs(s.get(i).y1 - s.get(i).y2),

                           Math.abs(s.get(i).x1 - s.get(i).x2) / 6,

                           Math.abs(s.get(i).y1 - s.get(i).y2) / 6);

               }

               else if (s.get(i).name==14) {

                   g.drawPolygon(s.get(i).X, s.get(i).Y, s.get(i).num);

               }

           }

        }

    }

   

    public void saveShape(String pathname) throws IOException{

        OutputStream out = new FileOutputStream(pathname);

        DataOutputStream dop = new DataOutputStream(out);

        dop.writeInt(s.size());

        for(int i = 0;i<s.size();i++){

           if(s.get(i).name==1){

               dop.writeInt(1);//1为直线

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==2){

               dop.writeInt(2);//2为椭圆

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==3){

               dop.writeInt(3);//3Arc

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).arc1);

               dop.writeInt(s.get(i).arc2);

               dop.writeInt(s.get(i).color.getRGB());       

           }

           else if(s.get(i).name==4){

               dop.writeInt(4);//4为长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==5){

               dop.writeInt(5);//5为圆角长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==6){

               dop.writeInt(6);//6为字节

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).data1.length);//字节数组长度

               dop.write(s.get(i).data1);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==7){

               dop.writeInt(7);//7为字符

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               String st = new String(s.get(i).data2);

               dop.writeInt(st.getBytes().length);//字节数组长度

               dop.write(st.getBytes());

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==8){

               dop.writeInt(8);//8为字符串

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).str1.getBytes().length);//字节数组长度

               dop.write(s.get(i).str1.getBytes());

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==9){

               dop.writeInt(9);//9为图片

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

           }

           else if(s.get(i).name==10){

               dop.writeInt(10);//10为椭圆

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==11){

               dop.writeInt(11);//11Arc

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).arc1);

               dop.writeInt(s.get(i).arc2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==12){

               dop.writeInt(12);//12为长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==13){

               dop.writeInt(13);//13为圆角长方形

               dop.writeInt(s.get(i).x1);

               dop.writeInt(s.get(i).y1);

               dop.writeInt(s.get(i).x2);

               dop.writeInt(s.get(i).y2);

               dop.writeInt(s.get(i).color.getRGB());

           }

           else if(s.get(i).name==14){

               dop.writeInt(14);//14为多边形

               dop.writeInt(s.get(i).num);

               for(int j = 0;j<s.get(i).num;j++){

                   dop.writeInt(s.get(i).X[j]);

                   dop.writeInt(s.get(i).Y[j]);

               }

               dop.writeInt(s.get(i).color.getRGB());

           }

        }

    }

 

    public void readShape(DataInputStream in) throws IOException{

        int size = in.readInt();

        for(int i = 0;i<size;i++){

           shape sh = new shape();

           sh.name = in.readInt();

           if(sh.name == 1){//直线

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 2){//椭圆

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 3){//arc

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.arc1 = in.readInt();

               sh.arc2 = in.readInt();

               sh.color = new Color(in.readInt());      

           }

           else if(sh.name == 4){//长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 5){//圆角长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 6){//字节

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               int length = in.readInt();

               sh.data1 = new byte[length];

               in.read(sh.data1);

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 7){//字符

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               int length = in.readInt();

               byte[] bArr = new byte[length];

               in.read(bArr);

               String a = new String(bArr);

               sh.data2 = a.toCharArray();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 8){//字符串

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               int length = in.readInt();

               byte[] bArr = new byte[length];

               in.read(bArr);

               sh.str1 = new String(bArr);

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 9){//图片

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

           }

           else if(sh.name == 10){//椭圆

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 11){//arc

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.arc1 = in.readInt();

               sh.arc2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 12){//fill长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 13){//fill圆角长方形

               sh.x1 = in.readInt();

               sh.y1 = in.readInt();

               sh.x2 = in.readInt();

               sh.y2 = in.readInt();

               sh.color = new Color(in.readInt());

           }

           else if(sh.name == 14){//多边形

               sh.num = in.readInt();

               for(int j = 0;j<sh.num;j++){

                   sh.X[j] = in.readInt();

                   sh.Y[j] = in.readInt();

               }

               sh.color = new Color(in.readInt());

           }

           s.add(sh);

        }

    }

}

 

 

 

 

 

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.image.ImageObserver;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

 

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

 

public class drawlistener implementsActionListener,MouseListener,ImageObserver,MouseMotionListener{

    private drawing dr = new drawing();

    private String type = "Draw";

    private Color c = Color.BLACK;

    private int x1,y1,x2,y2,x3,y3;

    private Graphics g;

    private int X[] = new int[20],Y[] = new int[20];

    private int num = 0;

    private ImageIcon im = new ImageIcon(this.getClass().getResource("login.png"));

    private ArrayList<shape> s =new ArrayList<shape>();

    private int index = 0;

   

    public ArrayList<shape> getshape(){

        return s;

    }

   

    //构造函数

    public drawlistener(drawing dr){

        this.dr = dr;

    }

    public void actionPerformed(ActionEvent e){

       

        if("".equals(e.getActionCommand())){

           JButton jbu = (JButton)e.getSource();

           c = jbu.getBackground();

        }

        else{

           type = e.getActionCommand();

        }

       

        if(type.equals(打开  ")){

           String path = null;

           //显示一个文件选择器

           JFileChooser chooser = new JFileChooser();

           int returnVal = chooser.showOpenDialog(null);//返回值

            if(returnVal ==JFileChooser.APPROVE_OPTION) {

               path =chooser.getSelectedFile().getAbsolutePath();

            }

            File file = new File(path);

            try {

               DataInputStream dis = new DataInputStream(new FileInputStream(file));

               s.removeAll(s);

               dr.readShape(dis);

               g = dr.getGraphics();

               dr.paint(g);

           } catch (FileNotFoundException e1) {

               // TODO Auto-generatedcatch block

               e1.printStackTrace();

           } catch (IOException e1) {

               // TODO Auto-generatedcatch block

               e1.printStackTrace();

           }

        }

        else if(type.equals(保存  ")){

           String path = null;

           JFileChooser chooser = new JFileChooser(); 

           chooser.setFileSelectionMode(JFileChooser.SAVE_DIALOG |JFileChooser.DIRECTORIES_ONLY);

           chooser.showSaveDialog(null);

           int returnVal = chooser.showOpenDialog(null);//返回值

           if(returnVal == JFileChooser.APPROVE_OPTION) {

               path =chooser.getSelectedFile().getAbsolutePath();

           }

           try {

               dr.saveShape(path+"\\"+"shape.shap");

           } catch (IOException e1) {

               // TODO Auto-generatedcatch block

               e1.printStackTrace();

           }

        }

    }

    public boolean imageUpdate(java.awt.Image arg0, int arg1, int arg2, int arg3, int arg4, int arg5){

        return false;

    }

   

    public void mouseDragged(MouseEvent m){

        x3 = m.getX();

        y3 = m.getY();

        g.setColor(c);

        if(type.equals("Draw")){

           g.drawLine(x1,y1,x3,y3);//(x1,y1)和(x2,y2)为直线的起点和终点

           shape sh = new shape(1,x1, y1, x3, y3, c);

           s.add(sh);

           index++;

           x1 = x3;

           y1 = y3;

        }

        else if(type.equals("Line")){

           dr.paint(g);

           g.drawLine(x1,y1,x3,y3);

        }

    }

 

   

    public void mouseMoved(MouseEvent m){

   

    }

   

    public void mouseClicked(MouseEvent m){

       

        if(m.getClickCount() == 2){

           if(type.equals("Polygon")){

               g.setColor(c);

               g.drawPolygon(X, Y, num);

              

               shape sh = new shape(14,X, Y, num,c);

               s.add(sh);

               index++;

              

               num = 0;

           }

        }

        else{

           X[num] = m.getX();

           Y[num] = m.getY();

           num++;

        }

    }

     

    public void mousePressed(MouseEvent m){

        x1 = m.getX();

        y1 = m.getY();

        //获取面板上画笔画布对象

        g = dr.getGraphics();

    }

    public void mouseReleased(MouseEvent m){

        x2 = m.getX();

        y2 = m.getY();

        g.setColor(c);

        //绘制图形

        if(type.equals("Line")){

           g.drawLine(x1, y1, x2, y2);//(x1,y1)和(x2,y2)为直线的起点和终点

           shape sh = new shape(1,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Oval")){

           g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//指定矩形边界内画圆

           shape sh = new shape(2,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Arc")){

           String str1 = JOptionPane.showInputDialog(null, "请输入起始角度");

           int r1 = Integer.parseInt(str1);

           String str2 = JOptionPane.showInputDialog(null, "请输入结束角度");

           int r2 = Integer.parseInt(str2);

           g.drawArc(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2), r1, r2);//指定矩形边界内从起始角度到结束角度之间画弧

           shape sh = new shape(3,x1, y1, x2, y2,r1, r2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Rect")){

           g.drawRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽

           shape sh = new shape(4,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("RoundRect")){

           g.drawRoundRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/6, Math.abs(y1-y2)/6);//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽,Math.abs(x1-x2)/6为圆角的弧宽, Math.abs(y1-y2/6为圆角的弧高

           shape sh = new shape(5,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Bytes")){

           String str1 = JOptionPane.showInputDialog(null, "请输入所需显示的字符串");

           byte []data = str1.getBytes();

           g.drawBytes(data,0,data.length,x1, y1);

           shape sh = new shape(6,x1,y1,data, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Chars")){

           String str1 = JOptionPane.showInputDialog(null, "请输入所需显示的字符串");

           char []data = str1.toCharArray();

           g.drawChars(data,0,data.length,x1, y1);

           shape sh = new shape(7,x1,y1,data, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("String")){

           String str1 = JOptionPane.showInputDialog(null, "请输入所需显示的字符串");

           g.drawString(str1,x1, y1);

           shape sh = new shape(8,x1,y1,str1, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("Image")){

          

           g.drawImage(im.getImage(), x1, y1, Math.abs(x1-x2), Math.abs(y1-y2), this);

           shape sh = new shape(9,x1, y1, x2, y2);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillOval")){

           g.fillOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//指定矩形边界内画椭圆

           shape sh = new shape(10,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillArc")){

           String str1 = JOptionPane.showInputDialog(null, "请输入起始角度");

           int r1 = Integer.parseInt(str1);

           String str2 = JOptionPane.showInputDialog(null, "请输入结束角度");

           int r2 = Integer.parseInt(str2);

           g.fillArc(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2), r1, r2);//指定矩形边界内从起始角度到结束角度之间画弧

           shape sh = new shape(11,x1, y1, x2, y2,r1, r2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillRect")){

           g.fillRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽

           shape sh = new shape(12,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

        else if(type.equals("FillRoundRect")){

           g.fillRoundRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2),Math.abs(x1-x2)/6, Math.abs(y1-y2)/6);//(x1,y1)为矩形的其中一个点,Math.abs(x1-x2)为矩形的长, Math.abs(y1-y2)为矩形的宽,Math.abs(x1-x2)/6为圆角的弧宽, Math.abs(y1-y2/6为圆角的弧高

           shape sh = new shape(13,x1, y1, x2, y2, c);

           s.add(sh);

           index++;

        }

    }

    public void mouseEntered(MouseEvent m){

         

    }

    public void mouseExited(MouseEvent m){

         

    }

   

 

}

 

 

 

 

 

import java.awt.Color;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

 

public class shape {

    int name;

    Color color;

    int arc1,arc2;

    int x1,y1,x2,y2;

    int X[] = new int[20],Y[] = new int[20];

    int num = 0;

    byte []data1;

    char []data2;

    String str1;

   

    public shape(int name, int x1, int y1, char []data2, Color color) {

        this.name = name;

        this.x1 = x1;

        this.y1 = y1;

        this.data2 = data2;

        this.color = color;

    }

    public shape(int name, int x1, int y1, String str1, Color color) {

        this.name = name;

        this.x1 = x1;

        this.y1 = y1;

        this.str1 = str1;

        this.color = color;

    }

    public shape(int name, int x1, int y1, byte[] data1, Color color) {

        this.name = name;

        this.x1 = x1;

        this.y1 = y1;

        this.data1 = data1;

        this.color = color;

    }

    public shape(int name,int x1, int y1, int x2, int y2, Color color) {

        this.x1 = x1;

        this.y1 = y1;

        this.x2 = x2;

        this.y2 = y2;

        this.name = name;

        this.color = color;

    }

    public shape(int name ,int x1, int y1, int x2,

           int y2, int arc1, int arc2, Color color) {

        this.name = name;

        this.arc1 = arc1;

        this.arc2 = arc2;

        this.x1 = x1;

        this.y1 = y1;

        this.x2 = x2;

        this.y2 = y2;

        this.color = color;

    }

 

    public shape(int name, int[] x, int[] y, int num, Color color) {

        X = x;

        Y = y;

        this.num = num;

        this.name = name;

        this.color = color;

    }

    public shape(int name,int x1, int y1, int x2, int y2) {

        this.x1 = x1;

        this.y1 = y1;

        this.x2 = x2;

        this.y2 = y2;

        this.name = name;

    }

   

    public shape() {

        // TODO Auto-generatedconstructor stub

    }

   

    public byte[] Intobyte(int number){

        byte[] turn = new byte[4];//int4个字节,转化为字节数组,数组长度为4

       

        turn[0] = (byte) (number%256);

        number = number/256;

        turn[1] = (byte) (number%256);

        number = number/256;

        turn[2] = (byte) (number%256);

        turn[3] = (byte) (number/256);

       

        return turn;

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值