如何用java保存画布的图像?

q9rjltbz  于 2021-07-05  发布在  Java
关注(0)|答案(0)|浏览(278)

我正在尝试使用jframe和画布制作一个绘图程序,还有一些其他的问题,但是我现在要解决的主要问题是如何将画布保存为图像文件。我在网上找到了很多不同的方法,但它们都只是保存了一个透明的屏幕什么的。请帮助我,这是一个学校的项目,我的老师是有史以来最无助的人。我不知道我在做什么。抱歉,如果我的代码让你畏缩。

//Java Program to create a canvas and paint the canvas
import java.awt.*;
import javax.swing.*;
//apparently this lets me use the mouse to draw
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
//didn't need BreezySwing

//to save canvas
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.Graphics2D;

//use JFrame instead of GBFrame, GBFrame won't let me use a canvas
class paint3 extends JFrame implements MouseListener, MouseMotionListener{

// create menubar and canvas
JMenuBar menuBar = new JMenuBar();
Canvas c;

// create color variable, set it to black
Color color = Color.black;
// create size variable, set it to 5
int size = 5;

////CONSTRUCTOR FOR FRAME////
public paint3()
{
    ////MENU STUFF////

    setJMenuBar(menuBar); // Add the menu bar to the window

    // create menus/menuitems
    JMenu colorMenu = new JMenu("Color"); //create color menu

    //implement methods to change the color of the brush when corresponding menu item is selected
    JMenuItem black = new JMenuItem(new AbstractAction("Black") {
        public void actionPerformed(ActionEvent e) {
            color = Color.black;
        }
    }); 
    JMenuItem darkGray = new JMenuItem(new AbstractAction("Dark Gray") {
        public void actionPerformed(ActionEvent e) {
            color = Color.darkGray;
        }
    });
    JMenuItem gray = new JMenuItem(new AbstractAction("Gray") {
        public void actionPerformed(ActionEvent e) {
            color = Color.gray;
        }
    });
    JMenuItem lightGray = new JMenuItem(new AbstractAction("Light Gray") {
        public void actionPerformed(ActionEvent e) {
            color = Color.lightGray;
        }
    });
    JMenuItem red = new JMenuItem(new AbstractAction("Red") {
        public void actionPerformed(ActionEvent e) {
            color = Color.red;
        }
    });
    JMenuItem orange = new JMenuItem(new AbstractAction("Orange") {
        public void actionPerformed(ActionEvent e) {
            color = Color.orange;
        }
    });
    JMenuItem yellow = new JMenuItem(new AbstractAction("Yellow") {
        public void actionPerformed(ActionEvent e) {
            color = Color.yellow;
        }
    });
    JMenuItem green = new JMenuItem(new AbstractAction("Green") {
        public void actionPerformed(ActionEvent e) {
            color = Color.green;
        }
    });
    JMenuItem cyan = new JMenuItem(new AbstractAction("Cyan") {
        public void actionPerformed(ActionEvent e) {
            color = Color.cyan;
        }
    });
    JMenuItem blue = new JMenuItem(new AbstractAction("Blue") {
        public void actionPerformed(ActionEvent e) {
            color = Color.blue;
        }
    });
    JMenuItem magenta = new JMenuItem(new AbstractAction("Magenta") {
        public void actionPerformed(ActionEvent e) {
            color = Color.magenta;
        }
    });
    JMenuItem pink = new JMenuItem(new AbstractAction("Pink") {
        public void actionPerformed(ActionEvent e) {
            color = Color.pink;
        }
    });
    JMenuItem white = new JMenuItem(new AbstractAction("White") {
        public void actionPerformed(ActionEvent e) {
            color = Color.white;
        }
    });

    JMenu sizeMenu = new JMenu("Size"); // Create Size menu

    //implement methods to change the size of the brush when corresponding menu item is selected
    JMenuItem five = new JMenuItem(new AbstractAction("5") {
        public void actionPerformed(ActionEvent e) {
            size = 5;
        }
    });  
    JMenuItem ten = new JMenuItem(new AbstractAction("10") {
        public void actionPerformed(ActionEvent e) {
            size = 10;
        }
    }); 
    JMenuItem fifteen = new JMenuItem(new AbstractAction("15") {
        public void actionPerformed(ActionEvent e) {
            size = 15;
        }
    });
    JMenuItem twenty = new JMenuItem(new AbstractAction("20") {
        public void actionPerformed(ActionEvent e) {
            size = 20;
        }
    });
    JMenuItem twentyfive = new JMenuItem(new AbstractAction("25") {
        public void actionPerformed(ActionEvent e) {
            size = 25;
        }
    });
    JMenuItem thirty = new JMenuItem(new AbstractAction("30") {
        public void actionPerformed(ActionEvent e) {
            size = 30;
        }
    }); 

    //JMenu clearButton = new JMenu(new AbstractAction("Clear") {
        //public void actionPerformed(ActionEvent e) {
            //c.clearRect(0,0,800,600);
        //}
    //});

    JMenu fileMenu = new JMenu("File"); //create file menu

    JMenuItem save = new JMenuItem(new AbstractAction("Save") {
        public void actionPerformed(ActionEvent e) {
            saveCanvas(c);
        }
    });  

    // add menus/menuitems to frame
    menuBar.add(colorMenu); // Add the color menu
    colorMenu.add(black); //add color menu items to frame
    colorMenu.add(darkGray);
    colorMenu.add(gray);
    colorMenu.add(lightGray);
    colorMenu.add(red);
    colorMenu.add(orange);
    colorMenu.add(yellow);
    colorMenu.add(green);
    colorMenu.add(cyan);
    colorMenu.add(blue);
    colorMenu.add(magenta);
    colorMenu.add(pink);
    colorMenu.add(white);

    menuBar.add(sizeMenu); // Add the size menu
    sizeMenu.add(five); // Add size menu items to frame
    sizeMenu.add(ten);
    sizeMenu.add(fifteen);
    sizeMenu.add(twenty);
    sizeMenu.add(twentyfive);
    sizeMenu.add(thirty);

    menuBar.add(fileMenu); // Add the file menu
    fileMenu.add(save); //Add file menu items to frame

    //terminates program when window is closed
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    ////////

    //create an empty canvas
    c = new Canvas() {

        //paint the canvas
        public void paint(Graphics g)
        {
        }

    };

    //set background color of canvas
    c.setBackground(Color.white);

    //add mouse listener to canvas
    c.addMouseListener(this);
    c.addMouseMotionListener(this);

    //add canvas to frame
    add(c);
    setSize(800,600);
    setResizable(false);
    show(); 

}
////////////

//mouse listener and mouse motion listener methods
public void mouseClicked(MouseEvent e)
{
    Graphics g = c.getGraphics();

    g.setColor(color);

    //get x and y pos
    int x, y;
    x = e.getX();
    y = e.getY();

    //draw an oval at the point were mouse is moved
    g.fillOval(x, y, size, size);
}

public void mouseMoved(MouseEvent e)
{
}

public void mouseDragged(MouseEvent e)
{
    Graphics g = c.getGraphics();

    g.setColor(color);

    //get x and y pos
    int x, y;
    x = e.getX();
    y = e.getY();

    //draw an oval at the point were mouse is moved
    g.fillOval(x, y, size, size);
}

public void mouseExited(MouseEvent e)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseReleased(MouseEvent e)
{
}   

public void mousePressed(MouseEvent e)
{
}

//method to save canvas, but it just saves a black screen
public static void saveCanvas(Canvas c) {
    BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(),BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = image.createGraphics();

    c.paint(graphics);
    //save file
    File file = new File("drawing.png");

    try {
        ImageIO.write(image, "png", file);
    } catch (Exception e) {
    }

}

//Main method
public static void main(String args[]) 
{
    Frame frm = new paint3();
    //need to capitalize canvas
    Canvas c = new Canvas();
    frm.setSize(800,600);
    //frm.pack();
    frm.setVisible(true);

}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题