从C驱动器加载Java代码中的映像 [英] Loading image in Java Code from C drive

查看:134
本文介绍了从C驱动器加载Java代码中的映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am new to Java . i was just trying to load image as background in JFrame. What i wanted to do is get the image from C Drive(that is not my workspace) so what i did in 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);
    }

然后我就像这样在我的主要课程中打电话

And then i am calling in my main class like this

   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());

   }

但是我没有显示任何图像,所以它是合法的方式添加图像

but i am not getting any image displayed , so is it legal way to add Image ?

推荐答案


  • 不要覆盖 paint() in JFrame

  • 不要在<$ c上调用 setSize() $ c> JFrame 而不是在设置可见之前使用 JFrame#pack()

  • 养成习惯使用 / ,无论平台如何支持。

    • Do not override paint() in JFrame
    • Do not call setSize() on JFrame rather use JFrame#pack() before setting it visible
    • Get into the habit of using / as regardless of platform this is supported.
    • 这是我做的一个例子:


      • 创建 JPanel / JLabel 实例

      • 覆盖 paintComponent(..) in JPanel / JLabel

      • 覆盖 getPreferredSize()返回尺寸正确的尺寸/组件图片

      • 添加 JPanel / Jlabel JFrame instance

      • pack JFrame by JFrame# pack()

      • 设置 JFrame 可见

      • Create JPanel/JLabel instance
      • Override paintComponent(..) in JPanel/JLabel
      • Override getPreferredSize() to return dimensions/component which is correctly sized to Image
      • Add JPanel/Jlabel to JFrame instance
      • pack JFrame by JFrame#pack()
      • set JFrame visible

      Test.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();
                      }
                  }
              });
          }
      }
      

      这篇关于从C驱动器加载Java代码中的映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆