java applection小程序 怎么在paint里面添加图片??
发布网友
发布时间:2022-05-05 04:04
我来回答
共3个回答
热心网友
时间:2022-06-29 04:55
直接复制以前写的小程序 源码给你。
你自己看看吧。
package com.bjsxt.tank;
import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 这个类的作用是坦克游戏的主窗口
* @author shenzhuan
*
*/
public class TankClient extends Applet{ //Frame ,
/**
* 整个坦克游戏的宽度
*/
AudioClip audio;
AudioClip audioStart;
public static final int GAME_WIDTH = Integer.parseInt(PropertyMgr.getProperty("GameWidth"));
public static final int GAME_HEIGHT = Integer.parseInt(PropertyMgr.getProperty("GameHeight"));
private static final String Blood = null;
private static int lev = 1;
private static Toolkit tk = Toolkit.getDefaultToolkit();
//草地 与 墙的图片
private static Image glassImages = tk.getImage(Tank.class.getClassLoader().getResource("images/glass.gif"));
private static Image wallImages = tk.getImage(Tank.class.getClassLoader().getResource("images/wall.gif"));
/**
* 画出自己的坦克
* 原:Dir 为STOP 启动游戏后需点击我界面 方能控制移动-》》》修改为U 依旧需要点击方加入监听
*/
Tank myTank = new Tank(500,500,true,Direction.STOP,this);
/**
* 定义几道墙
*/
ImageBySrc wallOne = new ImageBySrc(wallImages,100,200,50,50),
wallTwo = new ImageBySrc(wallImages,250,450,400,5),
wallThree = new ImageBySrc(wallImages,700,700,15,200),
wallFour = new ImageBySrc(wallImages,800,800,100,100),
wallFive = new ImageBySrc(wallImages,160,260,5,150),
glassOne = new ImageBySrc(glassImages,200,290,100,50);
List<Explode> explodes = new ArrayList<Explode> ();
List<Missile> missiles = new ArrayList<Missile> ();
List<Tank> tanks = new ArrayList<Tank>();
Image offScreenImage = null;
Blood_bak b = new Blood_bak();
public void paint(Graphics g) {
/**
*显示子弹-爆炸-坦克数量
*以及坦克生命值
*/
g.drawString("missiles count:"+missiles.size(), 10, 50);
g.drawString("explodes count:"+explodes.size(),10, 70) ;
g.drawString("enemey count:"+tanks.size(),10, 100) ;
g.drawString("myTank's life:"+myTank.getLife(),10, 110) ;
/**
* 这里指定当敌方坦克数目为零时
* 再次添加敌方坦克数目
*/
while(tanks.size() <=0 ) {
audioStart.play();
if(lev == 1){
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProseTankCount1")); i++) {
tanks.add(new Tank(10+65*(i+5),100,false,Direction.D,this));
}
lev ++;
System.out.println("第"+lev+"关");
}else if(lev ==2 ){
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProseTankCount2")); i++) {
tanks.add(new Tank(10+65*(i+7),950,false,Direction.R,this));
}
lev ++;
System.out.println("第"+lev+"关");
}else if(lev ==3 ){
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProseTankCount3")); i++) {
tanks.add(new Tank(10+80*(i+5),850,false,Direction.L,this));
}
lev ++;
System.out.println("第"+lev+"关");
}else if(lev ==4 ){
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProseTankCount4")); i++) {
tanks.add(new Tank(10+80*(i+5),850,false,Direction.L,this));
}
lev ++;
System.out.println("第"+lev+"关");
}else {
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProseTankCount5")); i++) {
tanks.add(new Tank(10+80*(i+5),850,false,Direction.L,this));
}
lev ++;
System.out.println("第"+lev+"关");
}
}
/**
* 子弹类的加入
* 打击坦克
* 能否穿墙
*/
for(int i=0; i<missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(wallOne);
m.hitWall(wallTwo);
m.hitWall(wallThree);
m.hitWall(wallFour);
//m.hitWall(w6);
m.draw(g);
/* 如果保留下面方法一次两子弹
* if(!m.isLive()) missiles.remove(m);
else m.draw(g);
*/
}
for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
/**
* 敌方坦克
* 能否穿墙
*/
for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
t.collidesWithWall(wallOne);
t.collidesWithWall(wallTwo);
t.collidesWithWall(wallThree);
//t.collidesWithWall(w4);
t.collidesWithWall(wallFive);
t.collidesWithWall(glassOne);
//t.collidesWithTanks(tanks);
//不能穿墙
//t.collidesWithWall(wallImages);
t.draw(g);
}
/**
* 下面方法让自己坦克 根据敌方坦克多少迅移
*/
for(int i=0; i<tanks.size(); i++) {
Tank t = myTank;
t.draw(g);
}
/**
* 我方坦克的加入
* 以及画出各种墙
*/
myTank.draw(g);
myTank.eat(b);
//w1.draw(g);
//w2.draw(g);
//w3.draw(g);
////w4.draw(g);
//w5.draw(g);
//w6.draw(g);
b.draw(g);
//右上两块草与墙
g.drawImage(wallOne.wallImages, wallOne.x, wallOne.y, wallOne.w, wallOne.h , null);
g.drawImage(glassOne.wallImages, glassOne.x, glassOne.y, glassOne.w, glassOne.h , null);
//
//画 草地与墙图
g.drawImage(glassImages,500,100,null);
g.drawImage(glassImages,660,170,null);
g.drawImage(glassImages,800,300,null);
g.drawImage(glassImages,850,370,null);
g.drawImage(wallImages,400,500,null);
g.drawImage(wallImages,600,500,null);
g.drawImage(wallImages,550,500,20,20,null);
//g.draw3DRect(100, 100, 55 , 55,true);
//g.draw3DRect(100, 200, 55 , 55,false);
}
/**
* 图像闪烁的处理
* 画图
* 颜色
* 关闭键
* 可见
*/
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH,GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.darkGray);
gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage,0,0,null);
}
/**
* 本方法显示坦克主窗口
* 一开始添加敌方坦克数目
*/
public void init() {
//声音所需
audio = getAudioClip(getDocumentBase(), "config\\DjMusic.wav"); // startMusic.mid
audioStart = getAudioClip(getDocumentBase(), "config\\startMusic.mid"); // startMusic.mid
audio.loop();
Properties props = new Properties();
try {
props.load(this.getClass().getClassLoader().getResourceAsStream("config/tank.properties"));
} catch (IOException e1) {
e1.printStackTrace();
}
int initTankCount =Integer.parseInt(PropertyMgr.getProperty("initTankCount"));
for(int i=0; i<initTankCount; i++) {
tanks.add(new Tank(40+100*(i+2),100,false,Direction.D,this));
}
/**
* 设置游戏窗口属性
*/
this.setLocation(1,1);
this.setSize(GAME_WIDTH,GAME_HEIGHT);
//this.setTitle("坦克大战");
/**
* 设置可关闭按钮
* 不可改变大小
* 颜色
* 关闭键
* 可见
*/
//this.addWindowListener(new WindowAdapter(){
////重写父类的方法 双击右键source- override Implements methods
//public void windowClosing(WindowEvent e) {
//System.exit(0);
//}
//});
//this.setResizable(false);
this.setBackground(Color.BLACK);
this.addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
//声音所需
private class PaintThread implements Runnable{
/**
* 所有成员动的速度
*/
public void run() {
while(true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* KeyMonitor键盘的引入
* @author shenzhuan
* 我方坦克方向键控制方向
*/
private class KeyMonitor extends KeyAdapter {
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
/**
* main方法
* @param args
*/
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.init();
}
}
热心网友
时间:2022-06-29 04:55
第一种方法:
Image image=getToolkit().getImage("a.png");
第二种方法:
URL url=getClass().getResource("a.png");
ImageIcon imageIcon=new ImageIcon(url);
Image image=imageIcon.getImage();
第三种:
Image image= imageIO.read(getClass.getClassLoader.getResource('a.png'));
最后使用画笔对象画就可以了
g.drew(图片对象,起点X,起点Y,width,height,null);
热心网友
时间:2022-06-29 04:56
好像有一个属性石 background 可以直接加载图片的