资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java显示图片的代码,java读取图片并显示

Java对话框上显示图片

其实有很多种方法可以解决图片显示大小的问题:

专注于为中小企业提供网站设计制作、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业卫辉免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

使用photoshop修改. 优点是可以节省系统资源, 显示图片的时候,不用做处理,缺点是需要了解ps的基本操作

使用JDialog 自定义对话框. 优点 可以实现复杂的效果, 缺点,代码量比较多

使用ImageIcon, Image 类 实现图片的缩放,. 优点: 纯java代码解决, 缺点: 如果大量的图片需要缩放, 那么可能影响程序的速度.

方案3的代码如下

import java.awt.Image;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

public class Test {

public static void main(String[] args) {

ImageIcon icon = new ImageIcon("imgs/1.png"); // 得到icon对象 .注意我的图片地址和你的不一样,注意修改!!

Image image = icon.getImage(); //icon---Image

float scale = 0.5f; //缩放比例 50%

int width = Math.round(icon.getIconWidth()*scale); // 变小 50%的宽

int height= Math.round(icon.getIconHeight()*scale);// 变小50%的高

Image miniIcon = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);

// image 变成指定大小. 缩放模式为 SCALE_SMOOTH(平滑优先)

ImageIcon smallIcon = new ImageIcon(miniIcon);// Image---icon

JOptionPane.showInputDialog(null, "吃了吗?", "标题", 0, smallIcon, null, "默认值");

}

}

效果图

图1 图片显示比例为原图的50%

图2 图片显示比例为原图的120%

怎么在java面板中显示一张图片

是这样的,你在面板上搞一个和面板一样大的JLabel

然后,通过JFileChooser获得路径,利用这个图片的路径,构建一个ImageIcon

最后,根据这个ImageIcon去给JLabel对象setIcon(ImageIcon对象);

具体地:

1.panel.add(label,BorderLayout.CENTER);

2.ImageIcon icon = new ImageIcon(url);

3.label.setIcon(icon);

下面的代码你把 .JPG改成BMP试试看,O(∩_∩)O~

package com.shlq.sample;

import java.awt.BorderLayout;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class ImagePane extends JPanel

{

JLabel jl = null;

ImageIcon img = null;

public ImagePane()

{

img = new ImageIcon( "E:\\Picture\\1.jpg ");

jl = new JLabel(img);

this.setLayout(new BorderLayout());

this.add(jl, BorderLayout.CENTER);

}

public static void main(String[] args)

{

JFrame test = new JFrame( "Image Pane ");

test.getContentPane().add(new ImagePane());

test.pack();

test.setVisible(true);

test.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

}

java图片显示代码

不是很明白,

类似于启动界面的么?

import javax.swing.*;

import java.awt.*;

import java.net.*;

public class JSplashWindow extends JWindow implements Runnable

{

Thread splashThread=null;

public JSplashWindow()

{

setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//设置启动界面的光标样式

JPanel splash=new JPanel(new BorderLayout());

URL url=getClass().getResource("1.jpg");//获得指定资源文件的绝对路径。

if(url!=null)

{

splash.add(new JLabel(new ImageIcon(url)),BorderLayout.CENTER);

}

setContentPane(splash);

Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕的大小

pack();

setLocation((screen.width-getSize().width)/2,(screen.height-getSize().height)/2);//使启动窗口居中显示

start();

}

public void start()

{

toFront();//window类的toFront()方法可以让启动界面显示的时候暂时在最前面,用window类的setAlwayOnTop(boolean)方法可以让窗口总保持在最前面。

splashThread=new Thread(this);

splashThread.start();

}

public void run()

{

try

{

setVisible(true);

Thread.sleep(50000);

}

catch(Exception e)

{

e.printStackTrace();

}

dispose();

}

static void showFrame(String title)

{

JFrame frame=new JFrame(title);

frame.setSize(400,300);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Dimension screenSize=frame.getToolkit().getScreenSize();//获得屏幕的大小

Dimension frameSize=frame.getSize();

if(frameSize.heightscreenSize.height)

{

frameSize.height=screenSize.height;

}

if(frameSize.widthscreenSize.width)

{

frameSize.width=screenSize.width;

}

frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);

frame.setVisible(true);

}

public static void main(String[] args)

{

showFrame("Demo splash window");

JSplashWindow splash=new JSplashWindow();

//splash.start();

}

}

/*getToolkit()方法是java.awt.window类的方法它可以得到一个Toolkit类。Toolkit对象的getScreenSize()方法可以得到屏幕的大小。

getScreenSize()方法返回一个 Dimension对象,它的width,height属性就是屏幕的宽和高。

Object getClass()方法是java.lang.Object类的方法它可以获得当前正在运行类的对象

URL getResource(String name)方法是java.lang.Class类的方法用此方法可以获得一个指定资源文件的绝对路径。*/

java怎么显示本地图片

在面板上搞一个和面板一样大的JLabel

然后,通过JFileChooser获得路径,利用这个图片的路径,构建一个ImageIcon

最后,根据这个ImageIcon去给JLabel对象setIcon(ImageIcon对象);

具体地:

1.panel.add(label,BorderLayout.CENTER);

2.ImageIcon icon = new ImageIcon(url);

3.label.setIcon(icon);

下面的代码你把 .JPG改成BMP试试看,O(∩_∩)O~

package com.shlq.sample;

import java.awt.BorderLayout;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class ImagePane extends JPanel

{

JLabel jl = null;

ImageIcon img = null;

public ImagePane()

{

img = new ImageIcon( "E:\\Picture\\1.jpg ");

jl = new JLabel(img);

this.setLayout(new BorderLayout());

this.add(jl, BorderLayout.CENTER);

}

public static void main(String[] args)

{

JFrame test = new JFrame( "Image Pane ");

test.getContentPane().add(new ImagePane());

test.pack();

test.setVisible(true);

test.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

}

java如何读取文件夹中的图片并在界面显示

下面给你提供一个实现,该实现采用了代理模式。这个实现包含两个文件,分别是Client.java和ImageIcoProxy.java,ImageIcoProxy.java负责了图片的延迟加载,你可以修改为不延迟即可。

Client.java的代码为:

import java.awt.Graphics;

import java.awt.Insets;

import javax.swing.Icon;

import javax.swing.JFrame;

public class Client extends JFrame {

private static int IMG_WIDTH = 510;

private static int IMG_HEIGHT = 317;

private Icon imgProxy = null;

public static void main(String[] args) {

Client app = new Client();

app.setVisible(true);

}

public Client() {

super("Virture Proxy Client");

imgProxy = new ImageIcoProxy("D:/test.jpg", IMG_WIDTH, IMG_HEIGHT);

this.setBounds(100, 100, IMG_WIDTH + 10, IMG_HEIGHT + 30);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void paint(Graphics g) {

super.paint(g);

Insets insets = getInsets();

imgProxy.paintIcon(this, g, insets.left, insets.top);

}

}

ImageIcoProxy.java的代码为:

import java.awt.Component;

import java.awt.Graphics;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.SwingUtilities;

public class ImageIcoProxy implements Icon {

private ImageIcon realIcon = null;

private String imgName;

private int width;

private int height;

boolean isIconCreated = false;

public ImageIcoProxy(String imgName, int width, int height) {

this.imgName = imgName;

this.width = width;

this.height = height;

}

public int getIconHeight() {

return realIcon.getIconHeight();

}

public int getIconWidth() {

return realIcon.getIconWidth();

}

public void paintIcon(final Component c, Graphics g, int x, int y) {

if (isIconCreated) {

//已经加载了图片,直接显示

realIcon.paintIcon(c, g, x, y);

g.drawString("Just Test", x + 20, y + 370);

} else {

g.drawRect(x, y, width-1, height-1);

g.drawString("Loading photo...", x+20, y+20);

synchronized(this) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

try {

Thread.currentThread().sleep(2000);

realIcon = new ImageIcon(imgName);

isIconCreated = true;

} catch (Exception e) {

e.printStackTrace();

}

c.repaint();

}

}

);

}

}

}

}

java中输出图片的代码

final ImageView iv=(ImageView)findViewById(R.id.iv);

Button bt=(Button)findViewById(R.id.bt);

bt.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View p1)

{

// TODO: Implement this method

if(iv.getDrawable()!=null)

iv.setImageResource(R.id.photo);

else iv.setImageResource(0);

}

});


分享标题:java显示图片的代码,java读取图片并显示
网页路径:http://cdkjz.cn/article/phesgg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220