为什么用java的URL定位资源图片需要刷新界面才能显示

来源:互联网 发布:usb协议分析软件 编辑:程序博客网 时间:2024/06/06 09:53
package gameFile;


import javax.swing.*;


import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import java.util.HashMap;


@SuppressWarnings("serial")
public class G1P4 extends JFrame {
final int WIDTH = 900, HEIGHT = 650;


final int UP = 0, RIGHT = 1, DOWN = 2, LEFT = 3;


int p1Direction = UP;


int p2Direction = UP;


boolean winnerChosen = false;


int p1Laps = 0, p2Laps = 0;


// BackGroundMusic bgm=new BackGroundMusic("e://111.wav");
HashMap<String, Rectangle> border = new HashMap<String, Rectangle>();
double p1Speed = .5, p2Speed = .5;
Rectangle left = new Rectangle(0, 0, WIDTH / 9, HEIGHT);
Rectangle right = new Rectangle((WIDTH / 9) * 9, 0, WIDTH / 9, HEIGHT);


Rectangle top = new Rectangle(0, 0, WIDTH, HEIGHT / 9);
Rectangle bottom = new Rectangle(0, (HEIGHT / 9) * 9, (WIDTH / 9) * 10,
HEIGHT / 9);
Rectangle center = new Rectangle((int) ((WIDTH / 9) * 2.5),
(int) ((HEIGHT / 9) * 2.5), (int) ((WIDTH / 9) * 5),
(HEIGHT / 9) * 4);


Rectangle obstacle = new Rectangle(WIDTH / 2, (int) ((WIDTH / 9) * 5.3),
WIDTH / 10, (int) (HEIGHT / 4.2));


Rectangle obstacle2 = new Rectangle((int) (WIDTH / 3),
(int) ((HEIGHT / 9) * 5), WIDTH / 10, HEIGHT / 4);
Rectangle obstacle3 = new Rectangle((int) (2 * WIDTH / 3),
(int) ((HEIGHT / 9) * 5), WIDTH / 10, HEIGHT / 4);


Rectangle obstacle4 = new Rectangle(WIDTH / 3, (int) ((HEIGHT / 9)),
WIDTH / 15, HEIGHT / 9);
Rectangle obstacle5 = new Rectangle(WIDTH / 2, (int) ((WIDTH / 9) * 1.3),
WIDTH / 30, HEIGHT / 4);


Rectangle finish = new Rectangle(WIDTH / 9, HEIGHT / 2 - HEIGHT / 9,
(int) (WIDTH / 9 * 1.5), HEIGHT / 70);
Rectangle line = new Rectangle(WIDTH / 9, HEIGHT / 2, WIDTH / 30,
HEIGHT / 140);
Rectangle line1 = new Rectangle(WIDTH / 9 + (int) ((WIDTH / 9) * 1.5 / 2),
HEIGHT / 2 + HEIGHT / 10, (int) ((WIDTH / 9) * 1.5) / 2,
HEIGHT / 140);


Rectangle p1 = new Rectangle(WIDTH / 9, HEIGHT / 2, WIDTH / 30, WIDTH / 30);
Rectangle p2 = new Rectangle(WIDTH / 9 + ((int) (WIDTH / 9 * 1.5 / 2)),
HEIGHT / 2 + HEIGHT / 10, WIDTH / 30, WIDTH / 30);


Image myImage[] = new Image[8];


URL url = null;
Image img;
public G1P4() {
super("Radical Racing");
this.setSize(WIDTH / 9 * 10, HEIGHT / 9 * 10);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


border.put("left", left);
border.put("right", right);
border.put("top", top);
border.put("bottom", bottom);
border.put("center", center);
border.put("obstacle", obstacle);
border.put("obstacle2", obstacle2);
border.put("obstacle3", obstacle3);
border.put("obstacle4", obstacle4);
border.put("obstacle5", obstacle5);


System.out.println(getClass().getResource(""));
try {
url = this.getClass().getResource("p11.png");

img=Toolkit.getDefaultToolkit().getImage(url);
img.getSource();
System.out.println("LAILE"+img.getHeight(null));
System.out.println("LAILE"+img.getHeight(null));
} catch (Exception e) {
e.printStackTrace();
}
// JOptionPane.showMessageDialog(null, "Welcome hero!!!");
try{
URL eng=this.getClass().getResource("111.wav");
AudioClip snd=JApplet.newAudioClip(eng);
snd.play();

System.out.println(eng);
}catch(Exception e){
e.printStackTrace();
}
}


@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img, 0, 0, null);


// this.doHitCheck();
}


public static void main(String[] args) {
new G1P4();
}

}

//图片放在bin/gameFile目录下,应该不是路径问题

0 0
原创粉丝点击