java - JPanel 不显示图像?

标签 java image image-processing jpanel

我正在尝试使用 JPanel 显示图像。我有这个代码:

package training;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.plaf.SliderUI;

public class Training extends JPanel {
  public static BufferedImage image;
  double maxw, maxh;
  double w, h, ratio;

  public Training () {
    super();
    try {               
      image = ImageIO.read(new File("src/training/P.jpg"));

    }
    catch (IOException e)
    {
      //Not handled.
    }
    maxw = 750;
    maxh = 600;
    w = image.getWidth();
    h = image.getHeight();

    if (w > h) {
    if (w > maxw) {
        ratio = maxw / w;
        h = h * ratio;    // Reset height to match scaled image
        w = w * ratio;   
    }

    }
    if (w <= h) {
   if (h > maxh) {
        ratio = maxh / h;
        w = w * ratio;    // Reset height to match scaled image
        h = h * ratio;   
    }

    } 
  }

  public void paintComponent(Graphics g)        
  {
    Image i = image.getScaledInstance((int)w, (int)h,Image.SCALE_SMOOTH);
    g.drawImage(i, 0, 0, null);
    repaint();
  } 

    public static void main(String[] args) {

        System.out.println("User dir: " + System.getProperty("user.dir"));
        JPanel p = new JPanel();
        JFrame f = new JFrame("Window");

    f.setSize(1000, 600);
    f.add(p); 
    p.add(new Training());
   p.setSize(750, 600);    
    f.setVisible(true);
    p.setVisible(true);
}
}

以前,当我使用 f.add(new Training()); 直接在框架上绘制时,它会起作用。无需先创建 JPanel。窗框上显示的是当时的画面。

如何让 JPanel 正确显示我的图像?

最佳答案

诀窍是将您的图片加载到“JLabel”中。您不能将图像直接放置到“JPanel”上:

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;

public class Main extends JPanel {
    public static BufferedImage image;
    double maxw, maxh;
    double w, h, ratio;

    public Main () {
        super();
        try {
            image = ImageIO.read(new File("src/training/P.jpg"));;
            JLabel l = new JLabel(new ImageIcon(image));
            add(l);
        }
        catch (IOException e)
        {
            //Not handled.
        }
        maxw = 750;
        maxh = 600;
        w = image.getWidth();
        h = image.getHeight();

        if (w > h) {
            if (w > maxw) {
                ratio = maxw / w;
                h = h * ratio;    // Reset height to match scaled image
                w = w * ratio;
            }

        }
        if (w <= h) {
            if (h > maxh) {
                ratio = maxh / h;
                w = w * ratio;    // Reset height to match scaled image
                h = h * ratio;
            }

        }
    }

    public void paintComponent(Graphics g)
    {
        Image i = image.getScaledInstance((int)w, (int)h,Image.SCALE_SMOOTH);
        g.drawImage(i, 0, 0, null);
        repaint();
    }

    public static void main(String[] args) {

        System.out.println("User dir: " + System.getProperty("user.dir"));
        JFrame f = new JFrame("Window");
        JPanel p = new Main();
        f.setSize(1000, 600);
        p.setSize(750, 600);
        f.add(p);
        f.setVisible(true);
    }
}

关于java - JPanel 不显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475048/

相关文章:

OpenCV:基本矩阵分解

matlab - 在 Matlab 图中更新图像对象的快速方法

ios - Swift - 如何一次检索多个图像(GCD)?

html - 图像调整到最大高度但页面溢出

javascript - 为网站选择一个图标形式的图标包

java - 如何在 Intellij IDEA 12 中设置 Maven 项目

c++ - 在 C++ 中执行 sobel 过滤器函数时我做错了什么

java - 带有新线程的新 JFrame

java - Spring MVC 3 异常 : An Errors/BindingResult argument is expected

Java JNA 无效内存访问