在JFrame下顯示圖片下載狀況 |
尚未結案
|
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
又有問題想要請問大家:
我現在有兩個程式碼:一個是能在JFrame下開啟圖片,並依圖片大小控制邊框;另一個是能在applet下開啟圖片,同時能顯示圖片下載狀況(下載中、下載失敗)。我想把applet內使用MediaTracker來追蹤影像過程的程式碼套用在JFrame底下,使得用JFrame開啟圖片時也能顯示圖片下載狀況。這樣可行嗎?如果可行的話,程式碼應該怎麼修改?我把applet內部份程式碼複製到JFrame中,出現了以下的錯誤訊息:
testFrame4.java:45:
run() in testFrame4 cannot implement run() in java.lang.Runnable; overriding method is static
static void run(){
^
testFrame4.java:39:
non-static variable this cannot be referenced from a static context
get_img=new Thread(this);
^
2 errors
感覺我好像在玩拼圖,拼拼湊湊的。很多觀念我都不甚清楚,可能要麻煩大家協助我釐清,先說聲謝謝囉! 程式碼:
import javax.swing.JFrame;
import java.awt.*;
import java.io.*;
import java.applet.*;
import java.awt.Graphics;
import java.awt.image.*;
import java.net.URL;
import java.net.*; public class testFrame4 extends Component implements Runnable {
static Image _image;
static JFrame jf1;
static int imw,imh, pixels[];
//從applet加入的資料成員
static int IMAGE_ID=0;
static MediaTracker tracker;
static boolean finish=false;
static Thread get_img;
static String message="Image Loading...";
//construct with a specific image
public testFrame4(Image im) {
this._image = im;
prepareImage(_image,this);
}
//applet內的程式碼 原public改成static
static void start(){
if (!tracker.checkID(IMAGE_ID)){
get_img=new Thread(this);
get_img.start();
}
}
static void run(){
try{
tracker.waitForID(IMAGE_ID);
}
catch(InterruptedException e){}
if((tracker.statusID(IMAGE_ID,false)&MediaTracker.ERRORED)!=0)
message="Loading error";
else
finish=true;
}
//paint
public void paint(Graphics g) {
g.drawImage(_image,0,0,this);
imw = _image.getWidth(this);
imh = _image.getHeight(this);
//setSize(imw, imh);
jf1.setSize(new Dimension(imw 5,imh 30));
//applet 內的程式碼
tracker=new MediaTracker(this);
tracker.addImage(_image,IMAGE_ID);
}
static void handlesinglepixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
System.out.print("[" red "," green "," blue "]");
// Deal with the pixel as necessary...
} static void handlepixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return;
}
for (int j = 0; j < h; j ) {
System.out.println("第" j "列");
for (int i = 0; i < w; i ) {
handlesinglepixel(x i, y j, pixels[j * w i]);
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String args[]){
jf1 = new JFrame();
jf1.setTitle("*** Image Frame ***");
jf1.setBounds(0,0,1000,1000);
jf1.setVisible(true);
Image _image = Toolkit.getDefaultToolkit().getImage("doraemon.gif"); //256*287
testFrame4 cube = new testFrame4(_image);
jf1.getContentPane().add(cube);
jf1.show();
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
handlepixels(_image, 0, 0, imw, imh); //print RGB
start();
run();
}
}
|
neoart
版主 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
static 方法只可以使用static物件.
ex:
xxx class{ static int a; static void main(){ a=1234;//這樣也可以,因為a已經是宣告為static物件 //要不就是直接在這裡new一個物件 int b=0;//這也可以,因為是在static方法內產生. ........ } }interface 是一種契約,java.lang.Runnable介面宣告 interface Runnable{ public void run(); }那你的jframe既然宣告有implement Runnable時,你的jframe類別自然也要有 public void run(){ }的實作才行. 介面宣告該方法不是static,自然實作者也不是宣告static才行 (static與non-static的差別如果真的不知道的話,要去看書才行) 發表人 - neoart 於 2004/03/15 10:29:23 |
vespa
一般會員 發表:14 回覆:22 積分:7 註冊:2004-02-13 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |