ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

从C驱动器加载Java代码中的映像

2019-09-01 16:00:13  阅读:161  来源: 互联网

标签:java image swing jframe


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

   }

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

解决方法:

>不要覆盖JFrame中的paint()
>不要在JFrame上调用setSize()而是在将其设置为可见之前使用JFrame#pack()
>养成使用/的习惯,无论平台如何支持.

这是我做的一个例子:

>创建JPanel / JLabel实例
>在JPanel / JLabel中覆盖paintComponent(..)
>覆盖getPreferredSize()以返回尺寸正确为Image的尺寸/组件
>将JPanel / Jlabel添加到JFrame实例
>通过JFrame#pack()打包JFrame
>设置JFrame可见

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

标签:java,image,swing,jframe
来源: https://codeday.me/bug/20190901/1784476.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有