Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JPanel转换为BuffereImage Java_Java_Swing_Jpanel_Bufferedimage_Turtle Graphics - Fatal编程技术网

如何将JPanel转换为BuffereImage Java

如何将JPanel转换为BuffereImage Java,java,swing,jpanel,bufferedimage,turtle-graphics,Java,Swing,Jpanel,Bufferedimage,Turtle Graphics,我有一个半工作油漆/海龟图形程序,但我有一些问题。 我需要保存在某个点上绘制的图像-我知道我需要使用BuffereImage,但我该怎么做?我在下面附上了我的一些代码 简言之,我有一个带有文本字段的JFrame,下面有一个白色JPanel。当用户输入命令时,行通过另一个类绘制并显示在JPanel上。我知道我无法将它们从JPanel保存到映像中,但不知道如何实现缓冲映像以及我需要/不需要更改什么 请客气一点,我是编程新手,还没有完成部分代码 提前谢谢 public class Turtle2 ex

我有一个半工作油漆/海龟图形程序,但我有一些问题。 我需要保存在某个点上绘制的图像-我知道我需要使用BuffereImage,但我该怎么做?我在下面附上了我的一些代码

简言之,我有一个带有文本字段的JFrame,下面有一个白色JPanel。当用户输入命令时,行通过另一个类绘制并显示在JPanel上。我知道我无法将它们从JPanel保存到映像中,但不知道如何实现缓冲映像以及我需要/不需要更改什么

请客气一点,我是编程新手,还没有完成部分代码

提前谢谢

public class Turtle2 extends JFrame implements ActionListener, KeyListener{

    JMenuBar menuBar;
    JMenu help, file;
    JPanel panel, panel2;
    JMenuItem newO, load, save, exitO, about;
    JTextField text;
    int savecounter = 0, newcounter = 1, part2I = 0;
    int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
    int trigger = 1, turnleftconstant = 0, turnrightconstant =0, direction =0;
    int col = 0;

        public  Turtle2() {
            setLayout(new FlowLayout());
            setSize(1000, 1000);
            setTitle("Graphic");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //menu bar

            //sets up general look of window
            panel = new JPanel();
            panel.setBackground(Color.darkGray); 

            //implements box layout
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            //sets up drawing area
            panel2 = new JPanel();
            panel2.setPreferredSize(new Dimension(500, 500));;
            panel2.setBackground(Color.WHITE);
            panel2.setBorder(BorderFactory.createLineBorder(Color.darkGray, 3));

            //starting point on drawing area co-ordinates
            x1 = 15; x2 = 15; y1 = 230; y2 =230;

            //starting point implemented
            LinePen first = new LinePen(x1, y1, x2, y2, col);
            panel2.add(first);

            //sets up user input text box
            text = new JTextField(30);

            //add action listener to text box - ready for input and details responses
            text.addActionListener(new ActionListener(){

                    public void actionPerformed(ActionEvent e){

                         //retrieves user input
                         String actions = text.getText();
                         first.setVisible(true);

                        //commands from text

                //add the command line textfeild to the interface
                panel.add(text);

                //uses border layout to set the layout of the interface
                getContentPane().add(BorderLayout.NORTH, panel);
                getContentPane().add(BorderLayout.CENTER, panel2);

                //set the size of the interface
                setSize(600,600);
                //ensure the interface is visible
                setVisible(true);

                }

我将保留一个包含所有行的数组,然后遍历它们,然后使用
img.createGraphics()
绘制您的行和其他图形。

我将保留一个包含所有行的数组,然后遍历它们,然后使用
img.createGraphics()
绘制线条和其他图形。

您可以执行类似或的操作,但可以使用更好的图像来存储所需的信息,以便重新创建图像并将其直接绘制到
缓冲区图像
1)要更快获得更好的帮助,请发布或。2) 使用缩进代码行和代码块的逻辑和一致形式。缩进的目的是使代码的流程更易于遵循!3) 更直接的问题是,创建所需大小的缓冲图像,调用
getGraphics
获取图像的图形上下文,将图形对象传递到
panel.paint(graphics)
,处理图形对象。您可以执行类似或的操作,但可以使用更好的图像来存储所需的信息,以便重新创建图像并将其直接绘制到
缓冲区图像
1)要更快获得更好的帮助,请发布或。2) 使用缩进代码行和代码块的逻辑和一致形式。缩进的目的是使代码的流程更易于遵循!3) 更直接的问题是,创建所需大小的缓冲图像,调用
getGraphics
获取图像的图形上下文,将图形对象传递到
panel.paint(graphics),处理图形对象。