如何在窗体中的某一区域将数组信息以图片形式显示 您所在的位置:网站首页 vb怎么在frame里面加入内容 如何在窗体中的某一区域将数组信息以图片形式显示

如何在窗体中的某一区域将数组信息以图片形式显示

2023-03-16 09:22| 来源: 网络整理| 查看: 265

1. 引言

在使用DEM数据时,通常需要将提取出的高程数组以图像的方式在窗体的某一区域显示出来。对于初学者而言,常常无从下手。

实际上,获得上述效果,只需要在窗体中加入JPanel容器,并利用JPanel的paintComponent(Graphics g)方法即可实现。

2. 实现步骤

将下图放置在窗体中的700\times 700区域。利用ImageIO.read(new FileInputStream(pathRoot + "testPanel.png")),将图片testPanel.png转换为BufferedImage。在paintComponent中绘制BufferedImage即可,g2.drawImage(imageBackground, 0, 0, this)。

放置后的效果

 

 示例代码

import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; public class MakePanelExample { private JFrame frame; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MakePanelExample window = new MakePanelExample(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. * @throws IOException * @throws FileNotFoundException */ public MakePanelExample() throws FileNotFoundException, IOException { // 设置应用的主窗口 frame = new JFrame(); frame.setBounds(10, 10, 1000, 800); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(null); contentPane.setSize(frame.getContentPane().getSize()); frame.setContentPane(contentPane); // 定义地图显示的窗口 double lon_SystemCenter = 94.36488; // 中心的经度 double lat_SystemCenter = 29.29815; // 中心的纬度 double lon_range = 7.5; double lat_range = 7.5; Backgnd backPanel = new Backgnd(lon_SystemCenter, lat_SystemCenter, lon_range, lat_range); contentPane.add(backPanel); } } class Backgnd extends JPanel { BufferedImage imageBackground; public Backgnd(double lon_SystemCenter, double lat_SystemCenter, double lon_range, double lat_range) throws FileNotFoundException, IOException { this.setSize(700, 700); this.setLocation(30, 30); this.setBackground(Color.RED); int width_Image = this.getWidth(); int height_Image = this.getHeight(); int x_ImageCenter = width_Image/2; int y_ImageCenter = height_Image/2; // 获取系统文件夹的路径 String pathRoot = System.getProperty("user.dir") + File.separator; if (pathRoot.startsWith("file")) { pathRoot = pathRoot.substring(5); } pathRoot.replace("/", File.separator); imageBackground = ImageIO.read(new FileInputStream(pathRoot + "testPanel.png")); } public void paintComponent(Graphics g) { // 重载窗口组件的paint()方法 super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawImage(imageBackground, 0, 0, this); } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有