Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.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
Java 在小程序中绘制缓冲图像_Java_Swing_Graphics_Applet_Paintcomponent - Fatal编程技术网

Java 在小程序中绘制缓冲图像

Java 在小程序中绘制缓冲图像,java,swing,graphics,applet,paintcomponent,Java,Swing,Graphics,Applet,Paintcomponent,我要做的是将一个已经缓冲的图像绘制到JFrame JFrame p1=new JFrame(); p1.getContentPane(); p1.setSize(new Dimension(h,w)); p1.setVisible(true); p1.update(bufferedImage.getGraphics()); 这是到目前为止的代码bufferedImage是一个缓冲图像,但此代码只打开JFrame,不绘制图像。我以前从未使用过图形。谢谢你把 2) 不要直接涂在JFrame上,放在

我要做的是将一个已经缓冲的图像绘制到JFrame

JFrame p1=new JFrame();
p1.getContentPane();
p1.setSize(new Dimension(h,w));
p1.setVisible(true);
p1.update(bufferedImage.getGraphics());
这是到目前为止的代码bufferedImage是一个缓冲图像,但此代码只打开JFrame,不绘制图像。我以前从未使用过图形。谢谢你把

2) 不要直接涂在
JFrame
上,放在那里


2) 不要直接在
JFrame
上画画,放在那里

请参见此工作代码:

public static void main(String[] args) throws Exception {
    BufferedImage buf=null;
    try {
        buf = ImageIO.read(new File("estbest.jpg"));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    new ImageFrame(buf, "Input Image ");
}
public class ImageFrame extends JFrame {
    BufferedImage image;

    public ImageFrame(final BufferedImage image) {
        this(image, "No Title");
    }

    public ImageFrame(final BufferedImage image, final String title) {
        this.image = image;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (image != null) {
                    setSize(image.getWidth(null), image.getHeight(null));
                } else {
                    setSize(250, 90);
                }
                setTitle(title);
                setVisible(true);
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        if (image == null) {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 250, 90);
            System.out.println("image null");
            g.setFont(new Font("Arial", Font.BOLD, 24));
            g.setColor(Color.RED);
            g.drawString("Invalid or No Image", 10, 50);
        } else {
            g.drawImage(image, 0, 0, null);
        }
    }
}
ImageFrame类:

public static void main(String[] args) throws Exception {
    BufferedImage buf=null;
    try {
        buf = ImageIO.read(new File("estbest.jpg"));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    new ImageFrame(buf, "Input Image ");
}
public class ImageFrame extends JFrame {
    BufferedImage image;

    public ImageFrame(final BufferedImage image) {
        this(image, "No Title");
    }

    public ImageFrame(final BufferedImage image, final String title) {
        this.image = image;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (image != null) {
                    setSize(image.getWidth(null), image.getHeight(null));
                } else {
                    setSize(250, 90);
                }
                setTitle(title);
                setVisible(true);
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        if (image == null) {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 250, 90);
            System.out.println("image null");
            g.setFont(new Font("Arial", Font.BOLD, 24));
            g.setColor(Color.RED);
            g.drawString("Invalid or No Image", 10, 50);
        } else {
            g.drawImage(image, 0, 0, null);
        }
    }
}

来源:

请参阅此工作代码:

public static void main(String[] args) throws Exception {
    BufferedImage buf=null;
    try {
        buf = ImageIO.read(new File("estbest.jpg"));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    new ImageFrame(buf, "Input Image ");
}
public class ImageFrame extends JFrame {
    BufferedImage image;

    public ImageFrame(final BufferedImage image) {
        this(image, "No Title");
    }

    public ImageFrame(final BufferedImage image, final String title) {
        this.image = image;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (image != null) {
                    setSize(image.getWidth(null), image.getHeight(null));
                } else {
                    setSize(250, 90);
                }
                setTitle(title);
                setVisible(true);
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        if (image == null) {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 250, 90);
            System.out.println("image null");
            g.setFont(new Font("Arial", Font.BOLD, 24));
            g.setColor(Color.RED);
            g.drawString("Invalid or No Image", 10, 50);
        } else {
            g.drawImage(image, 0, 0, null);
        }
    }
}
ImageFrame类:

public static void main(String[] args) throws Exception {
    BufferedImage buf=null;
    try {
        buf = ImageIO.read(new File("estbest.jpg"));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    new ImageFrame(buf, "Input Image ");
}
public class ImageFrame extends JFrame {
    BufferedImage image;

    public ImageFrame(final BufferedImage image) {
        this(image, "No Title");
    }

    public ImageFrame(final BufferedImage image, final String title) {
        this.image = image;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if (image != null) {
                    setSize(image.getWidth(null), image.getHeight(null));
                } else {
                    setSize(250, 90);
                }
                setTitle(title);
                setVisible(true);
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        if (image == null) {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 250, 90);
            System.out.println("image null");
            g.setFont(new Font("Arial", Font.BOLD, 24));
            g.setColor(Color.RED);
            g.drawString("Invalid or No Image", 10, 50);
        } else {
            g.drawImage(image, 0, 0, null);
        }
    }
}
资料来源: