import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.Image;
class SFish1 extends JFrame
{
JPanel panel = new JPanel();
Fish c;
public SFish1()
{
add(panel);
panel.setLayout(null);
c=new Fish(panel);
panel.add(c);
c.setBounds(100,200,150,100);
this.setTitle("副鱼");
this.setBounds(0,0,1500, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new SFish();}
}
class Fish extends Canvas implements ActionListener {
ImageIcon imageSFish = new ImageIcon("D:/java游戏编程/Fish/Fish/src/frame/fish.png");
Image imageSFish4= imageSFish.getImage();
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(imageSFish4,0,0,150,100,null);
}
// c.prepareImage(imageSFish4,ImageObserver observer);
private int TOP = 0;
private int BOTTOM;
private int LEFT = 0;
private int RIGHT;
private int xSpeed = 2;
private int ySpeed = 1;
Timer t = new Timer(1, this);
JPanel panel;
public void actionPerformed(ActionEvent e) { // 定义事件的行为
RIGHT = panel.getWidth();
BOTTOM = panel.getHeight();
Point currentPoint = this.getLocation(); // 获得当前的坐标
double x = currentPoint.getX();
double y = currentPoint.getY();
x = x + xSpeed;
y = y + ySpeed;
if ((x ) > RIGHT +10000|| x < 0) {
xSpeed = 0;ySpeed = 0;
}
if (y>=350)
{
ySpeed=-ySpeed;
}
if ((y ) > BOTTOM+10000) {
ySpeed = 0;xSpeed = 0;
}
currentPoint.setLocation(x, y);
this.setLocation(currentPoint);
}
// 构造函数一:参数为要添加的面板
public Fish(JPanel panel) {
this.panel = panel;
/*ImageIcon imageSFish = new ImageIcon("D:/java游戏编程/Fish/Fish/src/frame/fish.png");
Image immageSFish4= imageSFish.getImage();*/
t.start();
}
}
为什么我设置了第一个c.setBounds()的参数以后鱼出来的位置没有变化呢?哪位大神指点一下、φ(≧ω≦*)♪