我正在进行一项学校作业,我需要制作一个JComponent,使一些对象(在我的Player类中定义)移动。为了做到这一点,我需要使用KeyListener接口的keyPressed函数,但我无法让我的程序调用它。我知道这是因为按下按键时,按键开头的println不会打印。
以下是相关代码。 BackgroundMain.java
package edu.hiram.cs.cpsc172;
import java.awt.Color;
import javax.swing.JFrame;
/**
*
* @author tricksimon
* Class that creates the JFrame, and gives instructions for the frame.
*/
public class BackgroundMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// Learned in Stackoverflow
frame.setTitle("Game Background");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BackgroundComponent background = new BackgroundComponent();
frame.add(background);
frame.setVisible(true);
}
}
BackgroundComponent.java
package edu.hiram.cs.cpsc172;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.Polygon;
import javax.swing.JComponent;
/**
*
* @author Simonpj
* Class that makes the many parts of the background.
*/
public class BackgroundComponent extends JComponent implements KeyListener {
private long lastTime;
private int upArrow;
private int downArrow;
private int enterKey;
private int leftArrow;
private int rightArrow;
private int spaceKey;
private Player player;
public BackgroundComponent(){
player = new Player(200, 200, Color.BLUE);
setFocusable(true);
requestFocus();
}
public void paintComponent(Graphics g) {
upArrow=38;
downArrow = 40;
enterKey = 10;
leftArrow = 37;
rightArrow = 39;
spaceKey = 32;
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.yellow);
//Grass for the scene
Rectangle box = new Rectangle(0, 528, 2000, 1000);
g2.draw(box);
Color green = new Color(0, 255, 0);
g2.setColor(Color.green);
g2.fill(box);
//Rectangle representing the sky
Rectangle box1 = new Rectangle(0, 0, 2000, 530);
Color cyan = new Color(0, 255, 255);
g2.setColor(Color.cyan);
g2.draw(box1);
g2.fill(box1);
//Rectangle representing the house
Rectangle box2 = new Rectangle(0, 530, 600, 500);
g2.draw(box2);
g2.setColor(Color.black);
g2.fill(box2);
//Rectangle representing the door of the house
Rectangle box3 = new Rectangle(265, 650, 80, 150);
g2.draw(box3);
Color brown = new Color(139, 69, 19);
g2.setColor(brown);
g2.fill(box3);
//Ellipse representing the door knob
Ellipse2D.Double ellipse1 = new Ellipse2D.Double(330, 710, 10, 10);
g2.draw(ellipse1);
g2.setColor(Color.black);
g2.fill(ellipse1);
//Ellipse representing the sun
Ellipse2D.Double ellipse = new Ellipse2D.Double(1320, -20, 150, 150);
g2.setColor(Color.yellow);
g2.draw(ellipse);
g2.fill(ellipse);
//Create a polygon for the roof of the house
int[] xCoords = new int[]{0, 300, 600};
int[] yCoords = new int[]{530, 5, 530};
Polygon poly = new Polygon(xCoords, yCoords, 3);
g2.setColor(brown);
g2.fillPolygon(poly);
//Windows
Rectangle box4 = new Rectangle(100, 600, 70, 70);
g2.draw(box4);
g2.setColor(Color.blue);
g2.fill(box4);
Rectangle box5 = new Rectangle(126, 600, 15, 71);
g2.setColor(Color.black);
g2.draw(box5);
g2.fill(box5);
Rectangle box6 = new Rectangle(100, 628, 70, 15);
g2.setColor(Color.black);
g2.draw(box6);
g2.fill(box6);
Rectangle box7 = new Rectangle(450, 600, 70, 70);
g2.draw(box7);
g2.setColor(Color.blue);
g2.fill(box7);
Rectangle box8 = new Rectangle(476, 600, 15, 71);
g2.setColor(Color.black);
g2.draw(box8);
g2.fill(box8);
Rectangle box9 = new Rectangle(450, 628, 70, 15);
g2.setColor(Color.black);
g2.draw(box9);
g2.fill(box9);
// Drawing a car
//Main Car
Rectangle box10 = new Rectangle(750, 655, 300, 100);
g2.setColor(Color.blue);
g2.draw(box10);
g2.fill(box10);
Rectangle box11 = new Rectangle(830, 620, 150, 50);
g2.draw(box11);
g2.fill(box11);
//Wheels
Ellipse2D.Double ellipse2 = new Ellipse2D.Double(795, 725, 50, 50);
g2.setColor(Color.black);
g2.draw(ellipse2);
g2.fill(ellipse2);
Ellipse2D.Double ellipse3 = new Ellipse2D.Double(950, 725, 50, 50);
g2.setColor(Color.black);
g2.draw(ellipse3);
g2.fill(ellipse3);
Ellipse2D.Double ellipse4 = new Ellipse2D.Double(963, 738, 25, 25);
g2.setColor(Color.white);
g2.draw(ellipse4);
g2.fill(ellipse4);
Ellipse2D.Double ellipse5 = new Ellipse2D.Double(807.5, 738, 25, 25);
g2.setColor(Color.white);
g2.draw(ellipse5);
g2.fill(ellipse5);
//Text for the background
Font courierBold20 = new Font("Courier", Font.BOLD, 20);
g2.setColor(Color.black);
g2.setFont(courierBold20);
g2.drawString("My Game Background", 650, 270);
UFO u = new UFO(320,240, Color.BLUE);
u.draw(g2);
UFO u1 = new UFO(60,30, Color.RED);
u1.draw(g2);
UFO u2 = new UFO(900,180, Color.magenta);
u2.draw(g2);
Player p = new Player(700, 644, Color.DARK_GRAY);
p.draw(g2);
Player p1 = new Player(390, 644, Color.DARK_GRAY);
p1.draw(g2);
player.draw(g2);
}
@Override
public void keyPressed(KeyEvent e) {
System.out.println("check");
if (e.getKeyCode() == upArrow){
player.increaseSize();
}
if (e.getKeyCode() == downArrow){
player.decreaseSize();
}
if (e.getKeyCode() == enterKey){
player.resetSize();
}
if (e.getKeyCode() == leftArrow){
player.moveLeft();
}
if (e.getKeyCode() == rightArrow){
player.moveRight();
}
if (e.getKeyCode() == spaceKey){
player.newColor();
}
player.move(e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Player.java
package edu.hiram.cs.cpsc172;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.util.Random;
/**
*
* @author Simonpj
* Class that creates a player.
*/
public class Player {
private int x;
private int y;
private Color userColor;
private double size;
private Random rand;
/**
* Constructor for the Player class
* @param x X Coordinate for Player
* @param y Y Coordinate for Player
* @param userColor Color of the Player
*/
public Player(int x, int y, Color userColor) {
this.x=x;
this.y=y;
this.userColor=userColor;
this.size=1.0;
this.rand = new Random();
}
/**
* Draws the Player
* @param g Graphics object
*/
public void draw(Graphics2D g) {
Graphics2D g2 = (Graphics2D)g;
g2.scale(size, size);
//Body
Rectangle rect = new Rectangle(x, y, 45, 75);
g2.setColor(userColor);
g2.fill(rect);
g2.draw(rect);
//Head
Ellipse2D.Double ellipse = new Ellipse2D.Double(x + 4, y - 35, 35, 35);
g2.setColor(Color.WHITE);
g2.fill(ellipse);
//Leg
Rectangle rect1 = new Rectangle(x + 12, y + 76, 20, 45);
g2.setColor(Color.WHITE);
g2.fill(rect1);
g2.draw(rect1);
//Arms
Rectangle rect3 = new Rectangle(x + 16, y + 15, 13, 45);
g2.setColor(Color.WHITE);
g2.fill(rect3);
g2.draw(rect3);
//reset scale
g2.scale(1.0/size, 1.0/size);
}
public void changeColor(Color color) {
userColor=color;
}
public void move(int keyPressed) {
System.out.println(keyPressed); //comment out this line once you know the ints for the keys you need
}
public void increaseSize() {
size*=1.1;
}
public void decreaseSize() {
size*=0.9;
}
public void resetSize() {
size = 1.0;
}
public void moveLeft() {
//poly.translate(-2,0);
this.x -= 2;
}
public void moveRight() {
//poly.translate(2,0);
this.x += 2;
}
public void newColor() {
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r, g, b);
userColor = randomColor;
}
}
答案 0 :(得分:0)
您必须将对象添加为keyListener,可能在构造函数中添加:
addKeyListener(this);
答案 1 :(得分:0)
我没有看到您曾在addKeyListener
内拨打BackgroundComponent
。您需要这样做才能调用keyPressed
方法。