My friend and i are very new to java programming and were trying to make a simple game. were having problems making our images transparent. Also the images streak across when we move them. Any help would be verymuch.
我的朋友和我是java编程的新手,并试图制作一个简单的游戏。有问题使我们的图像透明。当我们移动图像时,图像也会划过。任何帮助都会非常多。
package keelbird;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.TimerTask;
import javax.imageio.ImageIO;
public class KeelBird extends JApplet implements KeyListener, ActionListener {
// establish all key variables
private static final long serialVersionUID = 1L;
int changey, changex, changez;
int dump = 1500;
int x = 20, y = 180, z = 250, r = 700, hx = 700, hy = 0;
int gx = 720, gy = 0, ix = 740, iy = 0, jx = 760, jy = 0;
int qx = dump, qy = dump;
int j = dump, k = dump;
int backx = dump, backy = dump;
int rx = 160, ry = 0, grx = dump, gry = dump;
int thealth = 5;
int reload = 0;
int rely = 0;
javax.swing.Timer timer;
BufferedImage yo, bird, explode, health, win, green, red, relImage;
public void init() {
// initialize applet processes, create applet
start();
setSize(800, 400);
setFocusable(true);
setFocusTraversalKeysEnabled(true);
addKeyListener(this);
repaint();
}
public void start() {
// start timer
if (timer == null) {
timer = new javax.swing.Timer(2, this);
timer.start();
} else {
timer.restart();
}
}
public void fire() {
// fire missle method
for(int j = 0; j < 100; j++) {
changex = 4;
repaint();
}
}
public void refire() {
// bring missle back
x = 20;
y = 180;
backx = 0;
backy = 0;
changex = 0;
System.out.println("RELOAD");
repaint();
}
public void paint(Graphics g) {
// paint graphics
int width = 80;
int height = 40;
yo = null;
bird = null;
explode = null;
health = null;
win = null;
BufferedImage background = null;
try {
File u = new File("the missel.png");
yo = ImageIO.read(u);
File w = new File("the dang bird.png");
bird = ImageIO.read(w);
File v = new File("explotions.png");
explode = ImageIO.read(v);
File t = new File("killdatbird.png");
background = ImageIO.read(t);
File h = new File("healthbar.png");
health = ImageIO.read(h);
File q = new File("winner.png");
win = ImageIO.read(q);
File re = new File("red.png");
red = ImageIO.read(re);
File gre = new File("green.png");
green = ImageIO.read(gre);
File rel = new File("reload.png");
relImage = ImageIO.read(rel);
} catch (IOException e) {
System.err.println("You made a Boo-boo");
}
g.drawImage(relImage, 20, rely, 40, 16, null);
g.drawImage(red, rx, ry, 80, 20, null);
g.drawImage(green, grx, gry, 80, 20, null);
g.drawImage(background, backx, backy, 800, 400, null);
g.drawImage(win, qx, qy, 800, 400, null);
g.drawImage(yo, x , y, width, height, null);
g.drawImage(bird, r, z, 40, 40, null);
g.drawImage(explode, j, k, 95, 95, null);
g.drawImage(health, hx,hy , 20, 10, this);
g.drawImage(health, gx,gy , 20, 10, this);
g.drawImage(health, ix,iy , 20, 10, this);
g.drawImage(health, jx,jy , 20, 10, this);
repaint();
}
public void keyPressed(KeyEvent e) {
// move missle up and down
if (e.getKeyCode() == KeyEvent.VK_UP) {
changey = -1;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
changey = 1;
}
// fire the missle
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
fire();
}
// allow reloading of missle
if (reload == 0) {
if(e.getKeyCode() == KeyEvent.VK_R) {
// do nothing
}
}
if (reload == 1) {
if (e.getKeyCode() == KeyEvent.VK_R) {
refire();
java.util.Timer tim = new java.util.Timer();
tim.schedule(new TimerTask() {
public void run() {
backx = dump;
backy = dump;
repaint();
reload = 0;
}
}, 100);
if (thealth == 0) {
thealth = 5;
qx = dump;
qy = dump;
hx = 700;
hy = 0;
gx = 720;
gy = 0;
ix = 740;
iy = 0;
jx = 760;
jy = 0;
}
}
}
repaint(1);
}
public void keyTyped(KeyEvent e) {
// do nothing
}
public void keyReleased(KeyEvent e) {
// no change in missle position if key is released
changey = 0;
}
public void actionPerformed(ActionEvent e) {
changez = (int) (((Math.random() * 4) - 2) * 2);
// set bounds for missle
if (y > 400) {
changey = 0;
y = 400;
}
if (y < 0 ) {
changey = 0;
y = 0;
}
if (x > 700) {
changex = 0;
}
// if max is hit
if (x == 680) {
j = x;
k = y - 20;
x = dump;
y = dump;
changex = 0;
java.util.Timer time = new java.util.Timer();
time.schedule(new TimerTask()
{
public void run() {
j = dump;
k = dump;
repaint();
reload = 1;
}}, 1000);
}
// set bounds for bird
if (z < 0) {
z = 0;
changez = 0;
}
if (z > 370) {
z = 370;
changez = 0;
}
if (reload == 0) {
rx = 160;
ry = 0;
grx = dump;
gry = dump;
repaint();
}
if (reload == 1) {
grx = 160;
gry = 0;
rx = dump;
ry = dump;
repaint();
}
// if bird is hit
if (Math.abs(k - z) <= 30) {
if(Math.abs(j - 700) <= 30) {
// hit casts missle and bird away
changez = 0;
x = dump;
y = dump;
z = dump;
r = dump;
// initiate timer after hit
java.util.Timer time = new java.util.Timer();
time.schedule(new TimerTask()
{
public void run() {
// throw explotion to side
j = dump;
k = dump;
// bird respawns
z = 250;
r = 700;
if (thealth == 4){
jx = dump;
jy = dump;
}
if (thealth == 3){
ix = dump;
iy = dump;
}
if (thealth == 2){
gx = dump;
gy = dump;
}
if (thealth == 1){
hx = dump;
hy = dump;
}
if (thealth == 0) {
qx = 0;
qy = 0;
grx = dump;
gry = dump;
rx = dump;
ry = dump;
rely = dump;
}
System.out.println("GOOD JOB!!!");
repaint();
reload = 1;
}},
1000);
thealth = thealth - 1;
repaint();
}
}
// constant changes in position
x = changex + x;
y = changey + y;
z = changez + z;
repaint(1);
}
}
1 个解决方案
#1
2
This is not an issue with your images but the fact that you have broken the paint
chain by not calling super.paint
这不是你的图像的问题,而是你没有调用super.paint打破了画颜链的事实
One of the jobs of paint
is to clear the Graphics
context ready for painting. This is important as the Graphics
context is a shared resource, meaning that everything that was painted before you will still be on the Graphics
.
绘画的一个工作是清除图形上下文准备好绘画。这很重要,因为Graphics上下文是一个共享资源,这意味着在您之前绘制的所有内容仍然在Graphics上。
Painting occurs for any number of reasons, repeatedly. You should avoid loading resources within any paint
method, you should also avoid calling repaint
or any method that might call repaint
from within any paint
method. This sets up a nasty recursive loop that will eventually consume your CPU cycles.
绘画多次出现有多种原因。您应该避免在任何paint方法中加载资源,还应该避免调用repaint或任何可能在任何paint方法中调用repaint的方法。这会设置一个讨厌的递归循环,最终会消耗你的CPU周期。
Preferably, you should avoid overriding paint
, especially of top level containers. Instead, you should move you paint logic (and probably the rest of your program) to something like a JPanel
and override it's paintComponent
method instead and do your painting there.
最好避免油漆覆盖,尤其是顶层容器。相反,您应该将绘制逻辑(可能是程序的其余部分)移动到像JPanel这样的东西,然后覆盖它的paintComponent方法,然后在那里进行绘制。
You can then add the panel to whatever top level container you like, making it much more portable
然后,您可以将面板添加到您喜欢的任何*容器中,使其更加便携
Take a look at Performing Custom Painting for more details...
看看Performing Custom Painting了解更多详情......
#1
2
This is not an issue with your images but the fact that you have broken the paint
chain by not calling super.paint
这不是你的图像的问题,而是你没有调用super.paint打破了画颜链的事实
One of the jobs of paint
is to clear the Graphics
context ready for painting. This is important as the Graphics
context is a shared resource, meaning that everything that was painted before you will still be on the Graphics
.
绘画的一个工作是清除图形上下文准备好绘画。这很重要,因为Graphics上下文是一个共享资源,这意味着在您之前绘制的所有内容仍然在Graphics上。
Painting occurs for any number of reasons, repeatedly. You should avoid loading resources within any paint
method, you should also avoid calling repaint
or any method that might call repaint
from within any paint
method. This sets up a nasty recursive loop that will eventually consume your CPU cycles.
绘画多次出现有多种原因。您应该避免在任何paint方法中加载资源,还应该避免调用repaint或任何可能在任何paint方法中调用repaint的方法。这会设置一个讨厌的递归循环,最终会消耗你的CPU周期。
Preferably, you should avoid overriding paint
, especially of top level containers. Instead, you should move you paint logic (and probably the rest of your program) to something like a JPanel
and override it's paintComponent
method instead and do your painting there.
最好避免油漆覆盖,尤其是顶层容器。相反,您应该将绘制逻辑(可能是程序的其余部分)移动到像JPanel这样的东西,然后覆盖它的paintComponent方法,然后在那里进行绘制。
You can then add the panel to whatever top level container you like, making it much more portable
然后,您可以将面板添加到您喜欢的任何*容器中,使其更加便携
Take a look at Performing Custom Painting for more details...
看看Performing Custom Painting了解更多详情......