Java - 保存草图

时间:2013-12-16 15:04:42

标签: java

我创建了一个程序,允许一个人在画布上绘制或涂鸦,如Microsoft Paint。您也可以选择其他颜色。当您最小化草图时,它会保留所有内容,当您最大化它时,草图会在您离开时显示。我希望能够保存草图并打开草图。我怎么能这样做?

SketchPad.java

package drs;

import java.awt.BorderLayout;
import javax.swing.JFrame;

/**
 *
 * @author Sameer Anand
 * 
 */

public class SketchPad extends JFrame {

    public static void main(String[] args) {

        SketchPad sp = new SketchPad("My Sketch Pad");

        sp.setVisible(true);

    }

    public SketchPad(String title) {

        super(title);

        this.setLayout(new BorderLayout());

        Canvas c = new Canvas();

        this.add(c,BorderLayout.CENTER);

        ButtonPanel bp = new ButtonPanel(c);

        this.add(bp,BorderLayout.NORTH);

        this.setSize(400,400);

    }

}

Doodle.java

package drs;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;


/**
 *
 * @author Sameer Anand
 * 
 */

class Doodle {

    private ArrayList<Point> points = new ArrayList<Point>();

    private Color theColor;

    public Doodle(Color color) {

        theColor = color;

    }

    public ArrayList<Point> getPoints() {

        return points;

    }

    public Color getColor() {

        return theColor;

    }

    public void add(Point p) {

        points.add(p);

    }

}

Canvas.java

package drs;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 *
 * @author Sameer Anand
 * 
 */

class Canvas extends JPanel implements MouseListener, MouseMotionListener {

    // a canvas has-a current location

    private int currentX, currentY;
    private Doodle currentDoodle;
    private Color currentColor = Color.BLACK;

    // a canvas has-a bunch of doodles

    private ArrayList<Doodle> doodles = new ArrayList<Doodle>();

    public Canvas() {

        this.addMouseListener(this);

        //this.addMouseMotionListener(this);

    }

    @Override
    public void paint(Graphics g) {

        System.out.println("Painting");

        for (Doodle d : doodles) {

            ArrayList<Point> pts = d.getPoints();

            // remember "old" color and use the "current" color instead

            Color old = g.getColor();

            g.setColor(d.getColor());


            for (int i = 0; i < pts.size()-1; i++) {

                g.drawLine((int)pts.get(i).getX(),(int)pts.get(i).getY(),
                    (int)pts.get(i+1).getX(),(int)pts.get(i+1).getY());

            } 

            // reset the color

            g.setColor(old);

        }

    }

    public void setCurrentColor(Color c) {

        currentColor = c;

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        System.out.println("Clicked " + e);

    }

    @Override
    public void mousePressed(MouseEvent e) {

        this.addMouseMotionListener(this);

        currentX = e.getX();

        currentY = e.getY();

        Doodle d = new Doodle(currentColor);

        currentDoodle = d;

        doodles.add(d);

        d.add(new Point(currentX,currentY));

    }

    @Override
    public void mouseReleased(MouseEvent e) {

        System.out.println("Released " + e);

        this.removeMouseMotionListener(this);

    }

    @Override
    public void mouseEntered(MouseEvent e) {

        System.out.println("Entered " + e);

    }

    @Override
    public void mouseExited(MouseEvent e) {

        System.out.println("Exited " + e);;

    }

    @Override
    public void mouseDragged(MouseEvent e) {

        Graphics g = this.getGraphics();

        Color old = g.getColor();

        g.setColor(currentColor);

        g.drawLine(currentX, currentY, e.getX(), e.getY());

        currentX = e.getX();

        currentY = e.getY();

        Point p = new Point(currentX, currentY);

        currentDoodle.add(p);

        g.setColor(old);

    }

    @Override
    public void mouseMoved(MouseEvent e) {

        System.out.println("Moved " + e);    

    }

}

ButtonPanel.java

package drs;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Sameer Anand
 * 
 */

public class ButtonPanel extends JPanel implements ActionListener {

    private Canvas canvas;

    public ButtonPanel(Canvas canvas) {

        this.canvas = canvas;

        this.setLayout(new FlowLayout());

        Color[] colors = {Color.BLACK, Color.RED, Color.GREEN, Color.BLUE };

        String[] names = {"Black", "Red", "Green","Blue"};

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

            Color c = colors[i];

            String n = names[i];

            JButton jb = new JButton(n);

            jb.setForeground(c);

            jb.addActionListener(this);

            add(jb);

        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {

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

        canvas.setCurrentColor(j.getForeground());

    }

}

2 个答案:

答案 0 :(得分:2)

如果要保存到磁盘,有多种方法。最简单的是保存每个像素坐标和颜色的矩阵。你也可以序列化Doodle课程。保存后,您可以阅读此内容并重新创建草图。

答案 1 :(得分:1)

你可以使Doodle实现Serializable,然后你可以简单地将Doodle保存为XML:

    XMLEncoder enc = null;
    String fileName = ...;
    try
    {
        enc = new XMLEncoder(new FileOutputStream(new File(fileName)));
        enc.writeObject(doodle);
    }
    catch (IOException e)
    {
        ...
    }
    finally
    {
        if (enc != null)
            enc.close();
    }

并加载:

        XMLDecoder d = null;
        try
        {
            d = new XMLDecoder(new FileInputStream(new File(...)));
            return (Doodle)d.readObject();
        }
        finally
        {
            if (d != null)
                d.close();
        }