Java——圖片滾動顯示
使用java swing,滾動顯示圖片示例。
public class ImageScrollShow extends JFrame {
public ImageScrollShow(){
setSize(800, 600);
Container c = getContentPane();
Dimension displaySize = Toolkit.getDefaultToolkit().getScreenSize(); // 獲得顯示器大小
Dimension frameSize = getSize();
if (frameSize.width > displaySize.width)
frameSize.width = displaySize.width; // 視窗的寬度不能大於顯示器的寬度
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation((displaySize.width - frameSize.width) / 2, (displaySize.height - frameSize.height) / 2); // 設定視窗居中顯示器顯示
JPanel jp = new JPanel();
jp.setLayout(new GridLayout(3, 4, 3, 3));
List<String> urls = getImageUrls();
if(urls != null && urls.size() > 0){
for(String url : urls){
JLabel cl = new JLabel();
cl.setIcon(new ImageIcon(getScaleImage(url, 257, 326, false)));
jp.add(cl);
}
}
// jp.setPreferredSize(new Dimension(600, 400));
JScrollPane jsp = new JScrollPane(jp);
c.add(jsp);
// c.add(jp);
setTitle("圖片列表");
setVisible(true);
}
/**
* 驗證url的有效性
* @param url
*/
public static boolean isUrlValid(String url) {
boolean flag = false;
int counts = 0;
if (url == null || url.length() <= 0) {
return flag;
}
//重試3次
while (counts < 3) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url)
.openConnection();
int state = connection.getResponseCode();
if (state == 200) {
// String realurl = connection.getURL().toString();
flag = true;
}
break;
} catch (Exception ex) {
counts++;
continue;
}
}
return flag;
}
public static List<String> getImageUrls(){
List<String> urlList = new ArrayList<>();
String imageUrl = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3155280771,606419104&fm=23&gp=0.jpg";
for(int i = 0; i < 17; i++){
if(isUrlValid(imageUrl)){
urlList.add(imageUrl);
}
}
return urlList;
}
/**
* 縮放圖片
* @param urlStr
* @param height
* @param width
* @param filler
* @return
*/
public static byte[] getScaleImage(String urlStr, int height, int width, boolean filler) {
ByteArrayOutputStream baos = null;
try {
double ratio; // 縮放比例
baos = new ByteArrayOutputStream();
URL url = new URL(urlStr);
BufferedImage bi = ImageIO.read(url);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
// 計算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
if (bi.getHeight() > bi.getWidth()) {
ratio = (new Integer(height)).doubleValue() / bi.getHeight();
} else {
ratio = (new Integer(width)).doubleValue() / bi.getWidth();
}
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
itemp = op.filter(bi, null);
}
if (filler) {// 補白
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null),
itemp.getHeight(null), Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null),
itemp.getHeight(null), Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "JPEG", baos);
} catch (IOException e) {
e.printStackTrace();
}
return baos.toByteArray();
}
public static void main(String[] args){
new ImageScrollShow();
}
}
相關文章
- 表格顯示滾動條
- 動態顯示資料庫圖片資料庫
- img圖片無法顯示利用onerror事件顯示替代圖片Error事件
- jquery Banner 圖片自動輪換顯示jQuery
- 頁面圖片自動滾動
- jquery 滑鼠移到圖片彈出浮動層顯示大圖片例子jQuery
- javascript實現圖片滾動JavaScript
- (轉)jquery圖片左右滾動jQuery
- opencv圖片上如何顯示兩個小圖片OpenCV
- win7圖片只顯示圖示不顯示預覽圖解決方案Win7圖解
- jQuery圖片無縫滾動效果jQuery
- 網頁圖片不能顯示 網頁圖片顯示不出來的解決辦法網頁
- VS+Qt+Halcon——顯示圖片,實現滑鼠縮放、移動圖片QT
- iOS設定tabbar不顯示文字,只顯示圖片iOStabBar
- java byte[] 轉圖片 在jsp頁面顯示JavaJS
- 超出文字顯示省略號,hover效果:文字滾動顯示==》求解
- 小程式button背景顯示圖片
- CSS圖片的灰色顯示效果CSS
- ImageView顯示網路上的圖片View
- vue el-image 顯示圖片Vue
- JS圖片滾動(無縫、平滑、上下左右滾動)效果JS
- 滾動cell 顯示隱藏導航欄
- Android 開源圖片裁剪工具、圖片顯示工具分享Android
- iOS Swift 仿微信聊天圖片顯示iOSSwift
- cv2.imshow顯示圖片不全
- div顯示滾動條(類似合同閱讀)
- C#資訊無縫滾動顯示程式碼C#
- JS控制圖片顯示的大小(圖片等比例縮放)JS
- springmvc中圖片回顯返回位元組流陣列顯示圖片完整案例SpringMVC陣列
- asp.net 圖片批量上傳預覽,在Silverlight頁面中讀取並滾動顯示ASP.NET
- CSS 例項之滾動的圖片欄CSS
- 左右帶有按鈕圖片水平滾動
- CSS例項:翻轉圖片、滾動圖片欄、開啟大門CSS
- 安卓圖片顯示與網路訪問安卓
- iOS 左滑按鈕(UITableViewRowAction)顯示圖片iOSUIView
- html2canvas生成圖片顯示不全HTMLCanvas
- 預載入顯示圖片的藝術
- iOS開發之imageView居中顯示圖片iOSView