gpt4 book ai didi

java - Eclipse Indigo 错误

转载 作者:行者123 更新时间:2023-12-01 14:39:34 28 4
gpt4 key购买 nike

我已经运行 Eclipse Indigo 几个月了,我遇到了一个我似乎无法找到答案的错误。我正在创建一个小型 2D 横向卷轴游戏,类似于马里奥、旧塞尔达等游戏。

我打算向我的父亲展示我在程序中添加了哪些新功能。我爸爸没有上楼在电脑上查看我的程序,而是决定使用 sudo 屏幕查看工具来进入它,但我不确定。我们以前用过这个,基本上它的作用就是让你看到家里另一台电脑的屏幕(在同一个IP接口(interface)上),并且你也可以使用这台电脑。

我不想这样给我爸爸看我的程序,所以我让他上楼来。他做到了,从那时起,eclipse 将不会在您的程序中显示 JFrame 内部的任何图形。它将显示诸如文字(写在屏幕上)之类的内容,但不会显示任何图形。例如您的背景图像、您的角色或其他任何内容。我确信这不是我的编码问题,因为在我爸爸进行屏幕查看之前,我已经测试并玩了好几次游戏(顺便说一句,我们都在 linux mint 12 上)。

我认为这个错误与屏幕查看有关。

如果我能得到一些帮助,我会很高兴。任何都会很棒。谢谢。

-此问题已解决*

董事会

package External;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Board extends JPanel implements ActionListener, Runnable {
Dude p;
Image img;
Timer time;
int v = 172;
Thread animator;

boolean a = false;
boolean done2 = false;

public Board() {
p = new Dude();
addKeyListener(new AL());
setFocusable(true);
ImageIcon i = new ImageIcon ("/home/clark/Desktop/Swindle_test_background.png");
img = i.getImage();
time = new Timer (3, this);
time.start();
}

public void actionPerformed(ActionEvent e) {
p.move();
repaint();
}
public void paint(Graphics g) {
if (p.dy == 1 && done2 == false)
{
done2 = true;
animator = new Thread(this);
animator.start();
}
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
if ((p.getX() - 590) % 2400 == 0)
p.nx = 0;
if ((p.getX() - 1790) % 2400 == 0)
p.nx2 = 0;
g2d.drawImage(img, 985-p.nx2, 0, null);
if (p.getX() >= 921)
g2d.drawImage(img, 985-p.nx, 0, null);
g2d.drawImage(p.getImage(), 75, v, null);


}

private class AL extends KeyAdapter {
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}

}
boolean h = false;
boolean done = false;

public void cycle() {
if (h == false)
v--;
if (v == 125)
h = true;
if (h == true && v <= 172 ) {
v++;
if (v == 172) {
done = true;

}
}
}




public void run() {

long beforeTime, timeDiff, sleep;

beforeTime = System.currentTimeMillis();

while (done == false) {

cycle();

timeDiff = System.currentTimeMillis() - beforeTime;
sleep = 10 - timeDiff;

if (sleep < 0)
sleep = 2;
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
System.out.println("interrupted");
}

beforeTime = System.currentTimeMillis();
}
done = false;
h = false;
done2 = false;
}

}

哥们

package External;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Dude {
int x, dx, y, nx2, nx, dy;
Image Swindle_Man_Right;
ImageIcon r = new ImageIcon("/home/clark/Desktop/Swindle_Man_Right.png");
ImageIcon l = new ImageIcon("/home/clark/Desktop/Swindle_Man_Left.png");
ImageIcon j = new ImageIcon("/home/clark/Desktop/Swindle_Man_Jump.png");

public Dude() {
Swindle_Man_Right = l.getImage();
x = 75;
nx2 = 685;
nx = 0;
y = 172;

}


public void move() {
x = x + dx;
nx2 = nx2 + dx;
nx = nx + dx;
}
public int getX() {
return x;
}
public int getY() {
return y;
}

public Image getImage() {
return Swindle_Man_Right;
}

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{ dx = -1;
Swindle_Man_Right = l.getImage();
}
if (key == KeyEvent.VK_RIGHT)
{dx = 1;
Swindle_Man_Right = r.getImage();
}

if (key == KeyEvent.VK_UP)
{dy = 1;
Swindle_Man_Right= j.getImage();
} }

public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();

if (key == KeyEvent.VK_LEFT)
dx = 0;

if (key == KeyEvent.VK_RIGHT)
dx = 0;

if (key == KeyEvent.VK_UP)
{dy = 0;
Swindle_Man_Right= r.getImage();}
}
}

框架

package External;

import javax.swing.*;

public class Frame {

public Frame() {
JFrame frame = new JFrame("Swindle [version 0.1.9]");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700,390);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
new Frame();
}

}

最佳答案

据我所知,您还没有向框架添加任何内容。

在我用自己的图形替换图形后,我能够让它运行......

enter image description here

public class Frame {

public static void main(String[] args) {
new Frame();
}

public Frame() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Swindle [version 0.1.9]");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// This is kind of important...
frame.add(new Board());

frame.setSize(700, 390);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

虽然我只快速浏览了代码,但我建议您不要使用 ImageIcon 加载图像,而是使用 ImageIO API 。除了支持更多的图像格式之外,它在无法加载图像时会抛出更多错误。

我也会避免使用 KeyListener 而是使用 key bindings 。它们不会遇到与 KeyListener

相同的焦点问题

关于java - Eclipse Indigo 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16115659/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com