快速业务通道

Java游戏中延迟下载资源及调用示例 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-20
hread.start(); }

private String getFileName(URL url) { String fileName = url.getFile(); return fileName.substring(fileName.lastIndexOf(''/'') + 1); }

public String getFileName() { return getFileName(url); }

public boolean isExists() { return new File(getFileName()).exists(); }

public String getDownloadName() { return downloadName; }

public void setDownloadName(String downloadName) { this.downloadName = downloadName; }

Java游戏中延迟下载资源及调用示例(5)

时间:2011-01-26 cping

/** * 进行文件下载并显示进度 */ public void run() { if (!isExists()) { RandomAccessFile file = null; InputStream stream = null; try { HttpURLConnection connection = (HttpURLConnection) url .openConnection(); connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); connection.connect(); if (connection.getResponseCode() / 100 != 2) { error(); } contentLength = connection.getContentLength(); if (contentLength < 1) { error(); } if (size == -1) { size = contentLength; } file = new RandomAccessFile(getFileName(url), "rw"); file.seek(downloaded); stream = connection.getInputStream(); while (status == DOWNLOADING) { byte buffer[]; if (size - downloaded > MAX_BUFFER_SIZE) { buffer = new byte[MAX_BUFFER_SIZE]; } else { buffer = new byte[size - downloaded]; } int read = stream.read(buffer); if (read == -1) { break; } file.write(buffer, 0, read); downloaded += read; listen.updateScreen(); } if (status == DOWNLOADING) { status = COMPLETE; listen.call(); } } catch (Exception e) { error(); } finally { if (file != null) { try { file.close(); file = null; } catch (Exception e) { } } if (stream != null) { try { stream.close(); stream = null; } catch (Exception e) { } } } } else { status = COMPLETE; listen.call(); } }

Java游戏中延迟下载资源及调用示例(6)

时间:2011-01-26 cping

/** * 绘制下载进度 * * @param g */ public synchronized void draw(Graphics g) { int progress = getProgress(); double ratio = (double) ((double) getLevel() / (double) getSize()); int offset = (int) (rectangle.width * ratio); g.drawImage(backgroundBarImage, rectangle.x + 19, rectangle.y, null); g.drawImage(progressBarImage, rectangle.x + 1, rectangle.y, offset + 20, rectangle.height - 2, null); g.drawImage(dialogBarImage, rectangle.x, rectangle.y, null); g.setFont(font); String mes = (getDownloadName() + ",已完成进度 : " + progress + " %") .intern(); FontMetrics fm = g.getFontMetrics(); int w = fm.stringWidth(mes); int h = fm.getHeight() + 2; g.setColor(Color.white); GraphicsUtils.setRenderingHints(g); g.drawString(mes, (rectangle.x + rectangle.width) / 2 - w / 2, rectangle.y + h); }

}

DownloadCanvas.java(下载条及背景显示用画布)

package org.loon.game.simple.download;

import java.awt.Canvas; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.Window; import java.awt.image.BufferStrategy; import java.util.ArrayList; import java.util.List;

/** * Copyright 200

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号