所以这是我的代码:
package project;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class Project extends JFrame {
String thing = "none";
int x = 100;
int y = 100;
int w = 1600;
int h = 800;
int ww = 5;
int hh = 5;
public class AL extends KeyAdapter{
public void keyPressed(KeyEvent e) {
int KeyCode = e.getKeyCode();
if (KeyCode == e.VK_NUMPAD1){
downleft();
if (thing == "none" || thing == "d" || thing == "dr" || thing == "l" || thing == "r" ||
thing == "ul" || thing == "u" || thing == "ur") {
thing = "dl";
System.out.println("Drawing down to the left!");
}
}
if (KeyCode == e.VK_NUMPAD2){
down();
if (thing == "none" || thing == "dl" || thing == "dr" || thing == "l" || thing == "r" ||
thing == "ul" || thing == "u" || thing == "ur") {
thing = "d";
System.out.println("Drawing down!");
}
}
if (KeyCode == e.VK_NUMPAD3){
downright();
if (thing == "none" || thing == "dl" || thing == "d" || thing == "l" || thing == "r" ||
thing == "ul" || thing == "u" || thing == "ur") {
thing = "dr";
System.out.println("Drawing down to the right!");
}
}
if (KeyCode == e.VK_NUMPAD4){
left();
if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "r" ||
thing == "ul" || thing == "u" || thing == "ur") {
thing = "l";
System.out.println("Drawing to the left");
}
}
if (KeyCode == e.VK_NUMPAD6){
right();
if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
thing == "ul" || thing == "u" || thing == "ur") {
thing = "r";
System.out.println("Drawing to the right!");
}
}
if (KeyCode == e.VK_NUMPAD7){
upleft();
if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
thing == "r" || thing == "u" || thing == "ur") {
thing = "ul";
System.out.println("Drawing up to the left!");
}
}
if (KeyCode == e.VK_NUMPAD8){
up();
if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
thing == "r" || thing == "ul" || thing == "ur") {
thing = "u";
System.out.println("Drawing up!");
}
}
if (KeyCode == e.VK_NUMPAD9){
upright();
if (thing == "none" || thing == "dl" || thing == "d" || thing == "dr" || thing == "l" ||
thing == "r" || thing == "ul" || thing == "u") {
thing = "ur";
System.out.println("Drawing up to the right!");
}
}
if (KeyCode == e.VK_B){
ww += 5;
hh += 5;
System.out.println("Resized! It is now " + ww + " by " + hh + "!");
if (ww > 150) {
ww = 150;
hh = 150;
System.out.println("The size went too high! Resized back to 150 by 150!");
}
}
if (KeyCode == e.VK_S){
ww -= 5;
hh -= 5;
System.out.println("Resized! It is now " + ww + " by " + hh + "!");
if (ww < 5) {
ww = 5;
hh = 5;
System.out.println("The size went too low! Resized back to 5 by 5!");
}
}
}
}
public void downleft(){
x-=5;
y+=5;
}
public void down(){
y+=5;
}
public void downright(){
x+=5;
y+=5;
}
public void left(){
x-=5;
}
public void right(){
x+=5;
}
public void upleft(){
x-=5;
y-=5;
}
public void up(){
y-=5;
}
public void upright(){
x+=5;
y-=5;
}
public Project(){
addKeyListener(new AL());
setTitle("Escher Sketch");
setResizable(false);
setVisible(true);
setSize(w, h);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
g.fillRect(x, y, ww, hh);
repaint();
}
public static void main(String[] args) {
new Project();
}
}
出于某种原因,当我跑它时,它只是一个黑色的窗口,左上角有一个小的白色矩形。我多次阅读代码,看起来它应该可以工作。它正在我的另一台计算机上工作,然后我尝试将该文件放在这台计算机上,但它没有。如果有人能帮助我那会很棒。
答案 0 :(得分:0)
将您的代码从Project()
部分更改为:
public Project(){
addKeyListener(new AL());
setTitle("Escher Sketch");
setResizable(false);
setVisible(true);
setSize(w, h);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new MyGraphics());
}
public static void main(String[] args) {
new Project();
}
class MyGraphics extends JPanel {
public void paintComponent(Graphics g) {
g.fillRect(x, y, ww, hh);
repaint();
}
}
}
答案 1 :(得分:0)
为什么你不使用Panel而不是将绘画应用到JFrame本身
这里有一些参考:
JFrame, JPanel, paintComponent how to
http://www.java2s.com/Code/JavaAPI/javax.swing/JPanelpaintComponentGraphicsg.htm
repaint();