導航:首頁 > 編程語言 > java下載顯示圖片

java下載顯示圖片

發布時間:2023-02-26 01:38:21

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在應用程序顯示圖片

如果想用JLabel實現的話就是JLabel jl = new JLabel(new ImageIcon(/* 圖片路徑 */));

閱讀全文

與java下載顯示圖片相關的資料

熱點內容
android仿ios時間選擇器 瀏覽:378
見識pdf 瀏覽:81
男孩子慰菊手冊pdf 瀏覽:531
注冊表啟動項命令 瀏覽:109
89c51單片機定時器 瀏覽:687
一般不適合做程序員的適合做啥 瀏覽:923
點在多邊形內演算法 瀏覽:494
程序員下班急忙回家 瀏覽:359
安慶php全套源碼交友類型網站源碼 瀏覽:92
浪潮伺服器公司地址 瀏覽:733
密約聊天交友app怎麼賺錢 瀏覽:280
滴滴java 瀏覽:109
phpexpires 瀏覽:218
matlab編程用什麼語言 瀏覽:70
php查找字元是否存在 瀏覽:701
可編程函數發生器 瀏覽:881
單片機正極接地視頻 瀏覽:67
利用python爬火車票 瀏覽:375
androidaidl如何工作 瀏覽:855
第三胸椎壓縮骨折什麼症狀 瀏覽:506