简体   繁体   English

图像未在 JPanel.paintComponent 中绘制

[英]Image not drawn in JPanel.paintComponent

I am actually want to load image but only applet dialog open and no error occurred but the image is not loading.the code is below here我实际上想加载图像但只打开小程序对话框并且没有发生错误但图像没有加载。代码在下面

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Ball extends JPanel{
  Image image; 


  public Ball(){
    super();       
    image = Toolkit.getDefaultToolkit().getImage("/D:\\lolololo\\tuto\\bin\\sa.jpg");
  }

private Image getResource(String string) {
    return image;
    // TODO Auto-generated method stub

}

  public void paintComponent(Graphics g){


   // Draw our Image object.
   g.drawImage(image,50,10,574,960, this); // at location 50,10
     // 200 wide and high
  }

  public void main(String arg[]){
   JFrame frame = new JFrame("ShowImage");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setSize(800,500);

   Ball panel = new Ball();
   frame.setContentPane(panel);
   frame.setVisible(true);
  }
}

The way you are loading the image is wrong.您加载图像的方式是错误的。 This will never work when you extract as Runnable jar.当您提取为 Runnable jar 时,这将永远不会起作用。

  • Create package ("res") inside inside your src在 src 内部创建包(“res”)

Now load the image this way现在以这种方式加载图像

image = ImageIO.read(Ball.class.getResource("/res/sa.jpg"));

This will work.这将起作用。

As indicated by Andrew in his comment main class should be正如安德鲁在他的评论中所指出的,主类应该是

public static void main(String arg[]) {}

+1 to @AndrewThompsons comments. +1 @AndrewThompsons 评论。

1) Below is incorrect, you do not honer the paint chain by calling the supers implementation of super.paintComponent(...) : 1) 下面是不正确的,你没有通过调用super.paintComponent(...)super.paintComponent(...)实现来super.paintComponent(...)油漆链:

public void paintComponent(Graphics g) {
   // Draw our Image object.
   g.drawImage(image,50,10,574,960, this); // at location 50,10
   // 200 wide and high

}

As per docs for paintComponent : 根据paintComponent的文档

Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color.此外,如果您不调用 super 的实现,则必须遵守 opaque 属性,即如果此组件是不透明的,则必须以非不透明的颜色完全填充背景。 If you do not honor the opaque property you will likely see visual artifacts.如果您不遵守不透明属性,您可能会看到视觉伪影。

should be:应该:

public class Ball extends JPanel {

    BufferedImage image;

    public Ball() {
       super();

       try {
           image=ImageIO.read(new File("c:/test.jpg"));//change to your path of file 
       }catch(Exception ex) {//file did not load properly
           ex.printStackTrace();
       }
    }

    @Override
    protected void paintComponent(Graphics g){
         super.paintComponent(g);

         // Draw our Image object.
         g.drawImage(image,0,0,image.getWidth(),image.getHeight(), this); // at location 50,10
        // 200 wide and high
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(),image.getHeight());//replace with size of image
    }

}

Also notice:还要注意:

  • I overrode getPreferredSize() of JPanel and returned Dimension s I wanted (ie Image size) or the JPanel will only be as big as the components added to it and not the image (so if no components 0,0).我覆盖了JPanel getPreferredSize()并返回了我想要的Dimension s(即图像大小),否则JPanel只会与添加到其中的组件一样大,而不是与图像一样大(因此,如果没有组件 0,0)。

  • I also chose BufferedImage vs Image and surrounded the statement with a try catch to check if any errors are thrown.我还选择了BufferedImage vs Image并用 try catch 包围了语句,以检查是否抛出了任何错误。

  • I see you also had g.drawImage(image,50,10...) why 50 and 10 and not 0,0?我看到你也有g.drawImage(image,50,10...)为什么是 50 和 10 而不是 0,0?

2) Also this (as @AndrewThompson has said): 2)还有这个(正如@AndrewThompson所说):

image = Toolkit.getDefaultToolkit().getImage("/D:\\\\lolololo\\\\tuto\\\\bin\\\\sa.jpg");

No need for the / thats only if its located in your classes package etc not when its on your HDD at a set location.不需要/ 那只是当它位于您的类包等时而不是当它位于您的硬盘驱动器上时。

3) also as said by @AndrewThompson a main method should be: 3)也正如@AndrewThompson所说,主要方法应该是:

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

Notice the static modifer applied other wise its just another method.注意静态修饰符以其他方式应用,它只是另一种方法。

4) Also dont use JFrame#setSize(..) , rather use LayoutManager and/or override getPreferredSize and than simply call pack() on JFrame instance before setting it visible. 4) 也不要使用JFrame#setSize(..) ,而是使用LayoutManager和/或覆盖getPreferredSize而不是简单地在JFrame实例上调用pack()在设置它可见之前。

5) Also please have a read on Concurrency in Swing . 5) 另请阅读Swing 中的并发 especially the Event-Dispatch-Thread尤其是事件调度线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM