我做了一个JFrame
和背景和球员,但我不能移动球员请帮帮我!这是我的来源:它们是 1 个包装中的 3 个类。主课是真的吗?!这是我的大学项目,我很困惑........
package sportshead;
import javax.swing.*;
import java.awt.*;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
public class screen extends JFrame{
private Image back;
private Image player1;
private Image player2;
private Image goal1;
private Image goal2;
private Image ball;
Player1 p = new Player1();
public screen(){
setTitle("The title");
setSize(1500, 800);
setLocation(0,0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void loadPics(){
player1 = new ImageIcon("C:\\Test\\player1.png").getImage();
player2 = new ImageIcon("C:\\Test\\player2.png").getImage();
goal1 = new ImageIcon("C:\\Test\\goal1.png").getImage();
goal2 = new ImageIcon("C:\\Test\\goal2.png").getImage();
ball = new ImageIcon("C:\\Test\\ball.png").getImage();
back = new ImageIcon("C:\\Test\\Background.jpg").getImage();
repaint();
}
public void paint(Graphics g){
g.drawImage(back, 0, 0, null);
p.paint(g);
g.drawImage(player2, -400, 200, null);
g.drawImage(goal1, 400, 145, null);
g.drawImage(goal2, -500, 140, null);
g.drawImage(ball, -20, -200, null);
g.setColor(Color.BLACK);
g.drawLine(0, 603, 1400, 603);
}
}
package sportshead;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.java2d.pipe.DrawImage;
public class Player1 extends JFrame {
private Image image;
private int x=0;
private int y=0;
public int yMove =200;
public int xMove = 200;
public Player1(){
image = new ImageIcon("C:\\Test\\player1.png").getImage();
addKeyListener( new KeyList());
}
public void paint(Graphics g) {
g.drawImage(image, xMove, yMove, this);
}
public void moveLeft() {
x = xMove--;
}
public void moveRight() {
y = xMove++;
}
public void moveReset(int value) {
xMove = value;
}
// inner class
private class KeyList extends KeyAdapter {
public void keyPressed(KeyEvent k) {
if (k.getKeyCode() == KeyEvent.VK_LEFT){
moveLeft();
}
if (k.getKeyCode() == KeyEvent.VK_RIGHT){
moveRight();
}
}
}
}
package sportshead;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
public class Main extends JFrame{
public static void main(String [] args){
int i =0;
screen s = new screen();
s.loadPics();
Player1 p = new Player1();
for(i=0;i<50;i+=5){
p.xMove+=i;
p.yMove+=i;
p.repaint();
s.loadPics();
}
}