gpt4 book ai didi

java - 从 C 驱动器以 Java 代码加载图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:05 24 4
gpt4 key购买 nike

我是 Java 新手。我只是想在 JFrame 中加载图像作为背景。我想做的是从 C 驱动器(那不是我的工作区)获取图像,所以我在 Board.java 中做了什么:

   ImageIcon i = new ImageIcon("C:/image.png");
img =i.getImage();

并试着把它画成这样:

    public void paint(Graphics g )
{
super.paint(g);
Graphics2D g2d= (Graphics2D) g;
g2d.drawImage(img, 0, 100, null);
}

然后我像这样调用我的主类

   public static void main(String[] args) 
{
JFrame frame= new JFrame(" Game") ;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 365);
frame.setVisible(true);
frame.add(new Board());

}

但是我没有显示任何图像,那么添加 Image 是否合法?

最佳答案

  • 不要覆盖JFrame中的paint()
  • 不要在 JFrame 上调用 setSize(),而是在将其设置为可见之前使用 JFrame#pack()
  • 养成使用 / 的习惯,因为无论哪个平台都支持它。

这是我做的一个例子:

enter image description here

  • 创建JPanel/JLabel实例
  • 覆盖JPanel/JLabel中的paintComponent(..)
  • 覆盖 getPreferredSize() 以返回与 Image 正确调整大小的维度/组件
  • JPanel/Jlabel添加到JFrame实例
  • 通过JFrame#pack()打包JFrame
  • 设置JFrame可见

测试.java:

//necessary imports
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

static String filename = "c:/test.jpg";//your file path and name here use / as it will work on linux platforms too so get into the habbit

/**
* Default constructor
*/
public Test() throws Exception {
initComponents();
}

/**
* Initialize GUI and components (including ActionListeners etc)
*/
private void initComponents() throws Exception {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Image background = ImageIO.read(new File(filename));
final Dimension jpanelDimensions = new Dimension(new ImageIcon(background).getIconWidth(), new ImageIcon(background).getIconHeight());

frame.add(new JPanel() {
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(background, 0, 0, null);
}

//return a JPanel that matches images size
@Override
public Dimension getPreferredSize() {
return jpanelDimensions;
}
});

frame.setResizable(false);

//pack frame (size JFrame to match preferred sizes of added components and set visible
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {

/**
* Create GUI and components on Event-Dispatch-Thread
*/
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
//set nimbus look and feel
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
try {
//create GUI instance
Test test = new Test();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
}

关于java - 从 C 驱动器以 Java 代码加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13237908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com