
正文
java显示图像代码实现 用java编程实现图片显示
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA 在屏幕上显示图形
import java.awt.Graphics;
import javax.swing.JFrame;
public class Test456 extends JFrame{
public static void main(String[] str){
//重写了JFrame中的paint()方法(该方法实际从JComponent中继承)。
JFrame frame = new JFrame("Frame"){
public void paint(Graphics e) {
e.drawString("画一条直线、矩形", 30, 60);
e.drawLine(30, 90, 100, 90);
e.drawRect(30, 120, 100, 50);
}
};
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
相关问答
Q1: java实现图片上传至服务器并显示,如何做?希望要具体的代码实现
很简单。
可以手写IO读写(有点麻烦)。
怕麻烦的话使用FileUpload组件 在servlet里doPost嵌入一下代码
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
PrintWriter out=response.getWriter();
//设置保存上传文件的目录
String uploadDir =getServletContext().getRealPath("/up");
System.out.println(uploadDir);
if (uploadDir == null)
{
out.println("无法访问存储目录!");
return;
}
//根据路径创建一个文件
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()){
if(!fUploadDir.mkdir())//如果UP目录不存在 创建一个 不能创建输出...
{
out.println("无法创建存储目录!");
return;
}
}
if (!DiskFileUpload.isMultipartContent(request))
{
out.println("只能处理multipart/form-data类型的数据!");
return ;
}
DiskFileUpload fu = new DiskFileUpload();
//最多上传200M数据
fu.setSizeMax(1024 * 1024 * 200);
//超过1M的字段数据采用临时文件缓存
fu.setSizeThreshold(1024 * 1024);
//采用默认的临时文件存储位置
//fu.setRepositoryPath(...);
//设置上传的普通字段的名称和文件字段的文件名所采用的字符集编码
fu.setHeaderEncoding("gb2312");
//得到所有表单字段对象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);//解析request对象中上传的文件
}
catch (FileUploadException e)
{
out.println("解析数据时出现如下问题:");
e.printStackTrace(out);
return;
}
//处理每个表单字段
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField()){
String content = fi.getString("GB2312");
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}else{
try
{
String pathSrc = fi.getName();
if(pathSrc.trim().equals("")){
continue;
}
int start = pathSrc.lastIndexOf('\\');
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName);
fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}catch (Exception e){
out.println("存储文件时出现如下问题:");
e.printStackTrace(out);
return;
}
finally //总是立即删除保存表单字段内容的临时文件
{
fi.delete();
}
}
}
注意 JSP页面的form要加enctype="multipart/form-data" 属性, 提交的时候要向服务器说明一下 此页面包含文件。
如果 还是麻烦,干脆使用Struts 的上传组件 他对FileUpload又做了封装,使用起来更傻瓜化,很容易掌握。
-----------------------------
以上回答,如有不明白可以联系我。
Q2: 怎么在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);
}
});
}
}
Q3: JAVA 中在一个组件上显示图片的所有方法
所有方法我可不知道,介绍个最常用的:通过Graphics对象来进行绘图,要获得此对象可通过方法getGraphics()来实现,举个例子:Graphics g=getGraphics();g.drawImage(image,0,0,this);drawImage()函数是Graphics对象的一个方法,意思就是把image画到本对象的0,0位置。此函数有很多重载的方法: drawImage(Image,int,int,ImageObserver);不再说了。 drawImage(Image,int,int,Color,ImageObserver);需要说的是Color是指的图片的背景色,如果图片有透明的区域,就用这个背景色。其余的参数含义刚说了,就不重复了。 drawImage(Image,int x,int y,int width,int height,ImageObserver);把image画在ImageObserver的x,y位置,宽为width,高为height。如果图片的大小与指定的宽高不一致,就会进行缩放,总是把整张图全画进去。 drawImage(Image,int,int,int,int,Color,ImageObserver);意思跟刚刚的一样,Color还是背景色。 drawImage(Image,int x1,int y1,int width1,int height1,int x2,int y2,int width2,int height2,ImageObserver);先将图片裁剪,从x2,y2开始裁,留下的图像宽为width2,高为height2,再把这个裁好的图画到ImageObserver的x1,y1上,大小为width1,height1,如果大小不一致还是用缩放的办法。 drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver);跟上面的一样,Color是背景色。 需要注意的是getGraphics()方法是JComponent对象的方法,所有的Swing对象都继承JComponent对象,所以象JFrame等类都有这个方法,也就是说,此方法不能在JComponent对象以外的地方调用,显然的不能写在main方法里。drawImage()函数里的Image对象可以这样用:Image image=Toolkit.getDefaultToolKit().getImage("my.jpg");这样就获得了一张图片。drawImage()函数里的ImageObserver对象其实就是JComponent对象,因为ImageObserver是个接口,JComponent类实现了它,所以所有的JComponent对象就是ImageObserver对象,就是JFrame,JPanel等对象。当然还有很多方法,刚才说了缩放的裁剪,其实图像的过滤还有很多内容,要说起来的话恐怕要上万字了,我懒得说那么多了...这个方法挺好用的,一般的软件就用这个就没问题了,除非你说你想做个photoshop那我就爱莫能助了。再举个例子吧,显示一张图片://Display.java//-------------------------------import javax.swing.JFrame;import java.awt.Toolkit; public class Display extends JFrame{ private Image image; public JFrame(){super("MyDisplay");image=Toolkit.getDefaultToolkit().getImage("my.jpg");//图片换成你自己的this.setSize(800,600);} public void paint(Graphics g){g.drawImage(image,0,0,800,600,this);//画图片,这里的paint方法系统自动调用,并且还传递进来了Graphics对象,省的再自己用getGraphics方法了。} public static void main(String[] args){new Display().setVisible(true);} }就说这么多吧,要真讲起来实在太麻烦了,介绍本书给你:java2图形设计卷I:AWT,java2图形设计卷II:SWING,这两本书够你看的了,虽然内容老旧,10年前的书,但是讲的全是基础,而且里面的东西现在还在用。你要是E文好就去SUN官网看技术文档或者jdk的帮助文档。
Q4: 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显示图像代码实现的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用java编程实现图片显示、java显示图像代码实现的信息别忘了在本站进行查找喔。







