为什么用java的URL定位资源图片需要刷新界面才能显示
2015-03-08 17:35
567 查看
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目录下,应该不是路径问题
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目录下,应该不是路径问题
相关文章推荐
- 图片上传回显,需要刷新文件夹才能显示
- 获取服务器Url图片资源,并显示在ImageView中 Android
- Java使用URL读取网络图片资源 并保存在本地
- Rx_java(4)Rx_java2中的线程控制 从网络下载图片并显示在界面小案例
- java&nbsp;文件打开图片并显示的界面和…
- 用background-image:url();属性设置背景图片,为什么总是显示不出来?
- 图片上传至Web项目下指定文件夹,需刷新项目下文件夹才能显示(已解决)
- 浏览器页面数据保存后不显示,需要重新刷新页面才能正常显示的解决方法
- 关于如何在jsp通过javascript获取表单中的值,以及为什么登陆界面刷新以后图片都不见了得原因
- android下需要怎么样配置或者操作才能在界面里显示状态栏(status bar)
- androidstudio3.0资源文件提示很慢,资源文件R文件不刷新,每次clean后才能显示控件id,findviewbyid没反应反映过慢
- 为什么中文需要2次encodeURI(url)才能保证不出现乱码
- 图解GPS原理 为什么需要4颗卫星才能定位,而不是3颗
- VS2013显示卡顿,需要最小化后重新打开解决方案等视图才能刷新
- 上传图片到服务器,服务器保存并且接收到了,但是必须刷新文件夹或者重新部署文件才能在客户端显示图片
- 用Eclipse Java图形用户界面加入图片,只有最大/小化才能显示图片。
- 为什么Java有GC还需要自己来关闭某些资源?
- java中怎样在界面中显示图片?
- 为什么在R文件中,图片的资源Id没有显示
- 当java swing界面启动无法正常显示,当调整JFrame大小后才能全部显示子组件