java程序加入图片,麻烦各位帮忙看一下,为什么图片不能显示?
TankClient:importjava.awt.*;importjava.awt.event.*;importjavax.swing.JFrame;publiccla...
TankClient:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class TankWar extends JFrame{
private static final long serialVersionUID = 1L;
maintank mk=new maintank(50,50,this);
public void paint(Graphics g) {
Image img=getToolkit().getImage("Image/mainU.gif");
int w=img.getWidth(this);
int l=img.getHeight(this);
g.drawImage(img, 20,20,w,l,this);
mk.draw(g);
}
public void launchFrame(){
this.setLocation(400,300);
this.setSize(800,600);
this.setTitle("TankWar");
/*关闭窗口*/
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
/*不改变窗口大小*/
this.setResizable(false);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args){
TankWar tw=new TankWar();
tw.launchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();//内部类
try{
Thread.sleep(70);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
maintank:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class maintank extends JFrame{
private Image tankL;
private Image tankR;
private Image tankU;
private Image tankD;
private int x,y;//初始位置
TankWar tw;
public maintank(int x,int y){
this.x=x;
this.y=y;
}
public maintank(int x,int y,TankWar tw){
this(x,y);
this.tw=tw;
}
public void draw(Graphics g){
Image img=getToolkit().getImage("Image/mainU.gif");
int w=img.getWidth(this);
int l=img.getHeight(this);
g.drawImage(img, 20,20,w,l,this);
g.fillOval(x, y, w, l);
}
private void loadImage(){
tankU=loadImg("Image/mainU.gif");
tankD=loadImg("Image/mainD.gif");
tankL=loadImg("Image/mainL.gif");
tankR=loadImg("Image/mainR.gif");
}
public Image loadImg(String imgUrl){
ImageIcon icon = new ImageIcon(getClass().getResource(imgUrl));
return icon.getImage();
}
} 展开
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class TankWar extends JFrame{
private static final long serialVersionUID = 1L;
maintank mk=new maintank(50,50,this);
public void paint(Graphics g) {
Image img=getToolkit().getImage("Image/mainU.gif");
int w=img.getWidth(this);
int l=img.getHeight(this);
g.drawImage(img, 20,20,w,l,this);
mk.draw(g);
}
public void launchFrame(){
this.setLocation(400,300);
this.setSize(800,600);
this.setTitle("TankWar");
/*关闭窗口*/
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
/*不改变窗口大小*/
this.setResizable(false);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args){
TankWar tw=new TankWar();
tw.launchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();//内部类
try{
Thread.sleep(70);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
maintank:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class maintank extends JFrame{
private Image tankL;
private Image tankR;
private Image tankU;
private Image tankD;
private int x,y;//初始位置
TankWar tw;
public maintank(int x,int y){
this.x=x;
this.y=y;
}
public maintank(int x,int y,TankWar tw){
this(x,y);
this.tw=tw;
}
public void draw(Graphics g){
Image img=getToolkit().getImage("Image/mainU.gif");
int w=img.getWidth(this);
int l=img.getHeight(this);
g.drawImage(img, 20,20,w,l,this);
g.fillOval(x, y, w, l);
}
private void loadImage(){
tankU=loadImg("Image/mainU.gif");
tankD=loadImg("Image/mainD.gif");
tankL=loadImg("Image/mainL.gif");
tankR=loadImg("Image/mainR.gif");
}
public Image loadImg(String imgUrl){
ImageIcon icon = new ImageIcon(getClass().getResource(imgUrl));
return icon.getImage();
}
} 展开
2016-12-09
展开全部
图片应该提前加载好
在paint中载入图片、再显示,就很容易显示不到图片
~~~~~
在paint中载入图片、再显示,就很容易显示不到图片
~~~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询