版权声明: https://blog.csdn.net/eds124/article/details/82290885
package com.dinglin;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame implements KeyListener {
private int y = 0, x = 0;
private int edsf = 0;
private JPanel jp = new JPanel() {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.fillRect(Short.MIN_VALUE, Short.MIN_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
g.setColor(Color.GREEN);
g.fillRect(x + 50, y + 50, 1800, 900);
if (edsf == 0) {
g.setColor(Color.RED);
g.fillRect(900, 500, 10, 50);
g.fillRect(940, 500, 10, 50);
g.fillRect(910, 510, 30, 30);
g.setColor(Color.BLUE);
g.drawOval(915, 515, 20, 20);
g.fillRect(923, 490, 4, 25);
g.setColor(Color.RED);
g.fillRect(923, y + 50, 4, 420 - y);
} else if (edsf == 1) {
g.setColor(Color.RED);
g.fillRect(900, 500, 10, 50);
g.fillRect(940, 500, 10, 50);
g.fillRect(910, 510, 30, 30);
g.setColor(Color.BLUE);
g.drawOval(915, 515, 20, 20);
g.fillRect(923, 535, 4, 25);
g.setColor(Color.RED);
g.fillRect(923, y + 950, 4, -370 - y);
} else if (edsf == 2) {
g.setColor(Color.RED);
g.fillRect(900, 500, 50, 10);
g.fillRect(900, 540, 50, 10);
g.fillRect(910, 510, 30, 30);
g.setColor(Color.BLUE);
g.drawOval(915, 515, 20, 20);
g.fillRect(890, 523, 25, 4);
g.setColor(Color.RED);
g.fillRect(50 + x, 523, 820 - x, 4);
} else if (edsf == 3) {
g.setColor(Color.RED);
g.fillRect(900, 500, 50, 10);
g.fillRect(900, 540, 50, 10);
g.fillRect(910, 510, 30, 30);
g.setColor(Color.BLUE);
g.drawOval(915, 515, 20, 20);
g.fillRect(930, 523, 25, 4);
g.setColor(Color.RED);
g.fillRect(1850 + x, 523, -870 - x, 4);
}
}
};
public Main() {
this.setBounds(0, 0, 1910, 1040);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(jp);
this.addKeyListener(this);
this.setVisible(true);
}
public static void main(String[] args) {
Main m = new Main();
}
public void keyPressed(KeyEvent ke) {
int i = ke.getKeyCode();
if (i == KeyEvent.VK_E || i == KeyEvent.VK_UP || i == KeyEvent.VK_NUMPAD8) {
edsf = 0;
y += 50;
} else if (i == KeyEvent.VK_D || i == KeyEvent.VK_DOWN || i == KeyEvent.VK_NUMPAD2) {
edsf = 1;
y -= 50;
} else if (i == KeyEvent.VK_S || i == KeyEvent.VK_LEFT || i == KeyEvent.VK_NUMPAD4) {
edsf = 2;
x += 50;
} else if (i == KeyEvent.VK_F || i == KeyEvent.VK_RIGHT || i == KeyEvent.VK_NUMPAD6) {
edsf = 3;
x -= 50;
} else if (i == KeyEvent.VK_I) {
}
this.repaint();
}
public void keyReleased(KeyEvent ke) {
}
public void keyTyped(KeyEvent ke) {
}
}