Java图形编程绘制图像错误

Java图形编程绘制图像错误,java,swing,user-interface,compiler-errors,scope,Java,Swing,User Interface,Compiler Errors,Scope,您好,我在画框上绘制图像时出错。我不确定这里出了什么问题 我在这里得到以下错误 Java: 77: cannot find symbol symbol: variable image location: class DrawComponent g.drawImage(image, 0, 0, null); class DrawComponent extends JComponent { public void paintComponent(Graphics g) {

您好,我在画框上绘制图像时出错。我不确定这里出了什么问题

我在这里得到以下错误

Java: 77: cannot find symbol
symbol: variable image
location: class DrawComponent
g.drawImage(image, 0, 0, null);


class DrawComponent extends JComponent {
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        // draw a circle with the same center
        double centerX = 250;
        double centerY = 180;
        double radius = 20;

        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
        g2.setPaint(Color.RED);
        g2.fill(circle);
        g2.draw(circle);

        String filename = "SydneyOperaHouse.jpeg";
        try{
            Image image = ImageIO.read(new File(filename));
        }catch(IOException ex){
            // Handle Exeption
        }

        g.drawImage(image, 0, 0, null);

    }
}

任何帮助都非常有用:)

您只需在try块中声明图像变量。。。它在外面是看不见的

try{
    Image image = ImageIO.read(new File(filename));
}catch(IOException ex){
    // Handle Exeption
}

g.drawImage(image, 0, 0, null);
变量
image
的输入错误。请注意,您正在
try
-块中声明变量。变量不存在于
try
-块的
{…}
之外

try
-块之外声明变量:

Image image = null;
try {
    image = ImageIO.read(new File(filename));
} catch(IOException ex) {
    // Handle Exeption
}

if (image != null) {
    g.drawImage(image, 0, 0, null);
}
顺便说一下,您不应该在
paintComponent
方法中进行I/O操作。最好将图像加载到其他地方(例如,当应用程序启动时),将其存储在成员变量中,并在
paintComponent
方法中使用


当您在
paintComponent
方法中加载图像时,它将在每次需要绘制组件时加载图像。这将使您的应用程序速度变慢。

如果出现异常情况,您希望看到什么

    String filename = "SydneyOperaHouse.jpeg";
    try{
        Image image = ImageIO.read(new File(filename));
    }catch(IOException ex){
        // Handle Exeption
    }

    g.drawImage(image, 0, 0, null);
您应该声明/initalise并在
try{}
块中绘制

  • 以解决属性范围的问题。应将
    图像
    属性交给(或加载到)构造函数,并作为绘制方法可见的类属性存储。切勿尝试使用此方法加载图像(或执行其他可能长时间运行的任务)
  • BG的映像在部署时通常是嵌入式资源,因此通过URL访问它
  • A
    JComponent
    是一个
    ImageObserver
    so
    g.drawImage(image,0,0,null)应为
    g.drawImage(图像,0,0,this)
  • 我怀疑0x0处的图像绘制应该在绘制红色椭圆之前进行,否则它将绘制在它的顶部
  • 这是一个基于悉尼的图片的例子(不,不是血腥的歌剧院-挑剔,挑剔…)


    +所有分数为1。也许您应该补充一点,解决点1也解决了他的范围问题
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.net.URL;
    
    public class DrawComponent extends JComponent {
    
        private Image image;
    
        DrawComponent(Image image) {
            this.image = image;
            Dimension d = new Dimension(image.getWidth(this),image.getHeight(this));
            this.setPreferredSize(d);
        }
        public void paintComponent(Graphics g) {
            // always call super first, to get borders etc.
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
    
            // paint the BG
            g.drawImage(image, 0, 0, this);
    
            // draw a circle with the same center
            double centerX = 250;
            double centerY = 180;
            double radius = 20;
    
            Ellipse2D circle = new Ellipse2D.Double();
            circle.setFrameFromCenter(centerX, centerY, centerX + radius, centerY + radius);
            g2.setPaint(Color.RED);
            g2.fill(circle);
            g2.draw(circle);
        }
    
        public static void main(String[] args) throws Exception {
            String s = "http://pscode.org/media/citymorn1.jpg";
            final Image image = ImageIO.read(new URL(s));
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    JComponent gui = new DrawComponent(image);
                    JOptionPane.showMessageDialog(null, gui);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }