好奇为什么我的图片没有显示

发布于 2024-12-03 05:35:40 字数 603 浏览 2 评论 0原文

仅使用绘画方法,我的图像最初不会显示。一旦我最小化 java 窗口并调整其大小,图像就会显示。我缺少任何代码吗?

public class Lil extends JFrame {

Image image = Toolkit.getDefaultToolkit().getImage("images/Untitled.png");

public Lil(){

    setTitle("flame");
    setBackground(Color.WHITE);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g){

    g.clearRect(0, 0, 400, 400);        
    g.drawImage(image, 60, 25, null);
    //repaint();
}   

public static void main(String [] args){    
        new lil();  
    }
}

Just using the paint method and my image won't show up initially. Once I minimize the java window and resize it, the image shows up. Is there any code I'm missing?

public class Lil extends JFrame {

Image image = Toolkit.getDefaultToolkit().getImage("images/Untitled.png");

public Lil(){

    setTitle("flame");
    setBackground(Color.WHITE);
    setSize(400, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
}

public void paint(Graphics g){

    g.clearRect(0, 0, 400, 400);        
    g.drawImage(image, 60, 25, null);
    //repaint();
}   

public static void main(String [] args){    
        new lil();  
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳哥哥 2024-12-10 05:35:40

不要直接在 JFrame 中绘制。相反,在 JPanel 的 PaintComponent 方法中进行绘制,然后将该 JPanel 添加到 JFrame 的 contentPane 上。更好的是,如果您不打算使用图像组件作为容器(以容纳其他组件),只需使用图像创建一个 ImageIcon,通过其构造函数或其 setIcon 方法将图标放入 JLabel 中,然后简单地显示 JLabel。没有混乱,没有大惊小怪,没有麻烦。另外,如果您将 super 的 PaintComponent 方法作为 JPanel 的 PaintComponent 重写方法中的第一个调用,则可能不需要调用clearRect。

例如,如果采用直接在 JPanel 中绘图的更复杂的路线,您将执行如下操作:

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Lil extends JPanel {

   private static final String URL_PATH = "http://duke.kenai.com/Oracle/" +
        "OracleStratSmall.png";

   BufferedImage image = null;

   public Lil() {
      setBackground(Color.white);
      try {
         image = ImageIO.read(new URL(URL_PATH));
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (image != null) {
         return new Dimension(image.getWidth(), image.getHeight());
      }
      return super.getPreferredSize(); // default
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            Lil lil = new Lil();

            JFrame frame = new JFrame();
            frame.getContentPane().add(lil);
            frame.setTitle("flame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
         }
      });

   }    
}

同样,仅当您要将组件(例如文本字段、按钮等)放置在图像上时才执行此操作四。如果没有,那么使用更简单的 ImageIcon/JLabel 想法。

Don't draw directly in a JFrame. Instead draw in a JPanel's paintComponent method and then add that JPanel onto your JFrame's contentPane. Better still, if you're not going to be using the image-component as a container (to hold other components), just create an ImageIcon with the Image, place the icon into a JLabel via its constructor or its setIcon method, and simply display the JLabel. No muss, no fuss, no trouble. Also, likely there's no need to call clearRect if you call super's paintComponent method as the first call in the JPanel's paintComponent override method.

For instance, if going the more complex route of drawing directly in a JPanel, you'd do something like so:

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Lil extends JPanel {

   private static final String URL_PATH = "http://duke.kenai.com/Oracle/" +
        "OracleStratSmall.png";

   BufferedImage image = null;

   public Lil() {
      setBackground(Color.white);
      try {
         image = ImageIO.read(new URL(URL_PATH));
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   @Override
   public Dimension getPreferredSize() {
      if (image != null) {
         return new Dimension(image.getWidth(), image.getHeight());
      }
      return super.getPreferredSize(); // default
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      if (image != null) {
         g.drawImage(image, 0, 0, null);
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            Lil lil = new Lil();

            JFrame frame = new JFrame();
            frame.getContentPane().add(lil);
            frame.setTitle("flame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
         }
      });

   }    
}

Again, only do this if you're going to be placing components on the image such as text fields, buttons, and so forth. If not, then use the simpler ImageIcon/JLabel idea.

眼藏柔 2024-12-10 05:35:40

Hovercraft 已经添加了一些建议,因此不再重复。

您的代码无法正常工作,因为您在重写 paint() 时尚未调用超类 Paint 方法。

只需在 paint() 方法的末尾添加一行即可。像这样:

public void paint(Graphics g) {
    super.paint(g);
    g.clearRect(0, 0, 400, 400);
    g.drawImage(image, 60, 25, null);
    // repaint();
}

Hovercraft has already added some suggestions so not repeating those.

Your code is not working because you have not called the super classes paint method when you have overridden paint().

Just add one single line at the binging of your paint() method. Like this:

public void paint(Graphics g) {
    super.paint(g);
    g.clearRect(0, 0, 400, 400);
    g.drawImage(image, 60, 25, null);
    // repaint();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文