我打算制作一个非常简单的游戏,其中一个正方形沿着二维楼层移动。要做到这一点,我需要添加2个形状,但由于某种原因,它们不会出现在屏幕上。我认为绘制实际形状的代码存在问题,因为当我省略添加第二个形状的部分时,它仍然无法正常工作。如果有人可以指出我的代码中的错误,以便我可以修复它们(我一分钟不关心keylistener代码)那么这将非常有用。
主类:
import java.awt.*;
import javax.swing.*;
public class mainClass3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame window = new JFrame();
window.setSize(600,400);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.white);
window.setLayout(null);
Graphics3 DC = new Graphics3();
window.addKeyListener(DC);
window.add(DC);
Floor2 floor = new Floor2();
window.add(floor);
}
}
图形类(方块):
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Graphics3 extends JComponent implements KeyListener{
int x = 5;
int y = 200;
public void paintComponent(Graphics g) {
Rectangle rect = new Rectangle(x,y,100,100);
Graphics2D g2 =(Graphics2D)g;
rect.setLocation(x,y);
g2.setColor(Color.CYAN);
g2.fill(rect);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
x-=1;
}
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
其他图形类(发言权):
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.*;
import javax.swing.*;
public class Floor2 extends JComponent{
public void paintComponent2(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Rectangle floor1 = new Rectangle (0,300,600,100);
floor1.setLocation(permanentx,permanenty);
g2.setColor(Color.black);
g2.fill(floor1);
}
}
答案 0 :(得分:2)
window.setLayout(null);
是您的问题之一。布局管理API执行一些非常重要的工作,根据布局实现自动定位和调整组件,您刚刚离开并丢弃。
请查看Laying Out Components Within a Container了解详情。
另一个是试图以这种方式使用组件。
我建议稍微改变一下。而不是使用两个不同的组件,创建一个单独的组件,作为游戏表面"。这是所有相关绘画完成的地方,地板和矩形。这提供了一个更好的自包含环境,更容易控制和维护。
在&#34之前发布您的下一个问题之前,为什么我的KeyListener
无法正常工作" - 请查看How to use Key Bindings将解决的问题与KeyListener
答案 1 :(得分:1)
我在这里和那里改变了你的代码,但它终于有效了。我决定使用null
布局,因为在这种情况下它是合适的。无论如何,这是我的代码:
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class mainClass3 {
public static void main(final String[] arguments) {
JFrame frame = new JFrame();
// Never forget to manipulate swing objects under the event dispatch queue.
EventQueue.invokeLater(() -> {
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(600, 400);
// I'll use the null layout. Don't blame me.
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Specifies the position, as well as the width and height of the square.
final Rectangle rectangle01 = new Rectangle(5, 200, 100, 100);
// Determines the position and size of the floor.
final Rectangle rectangle02 = new Rectangle(0, 300, 600, 100);
// Now, here is the first big difference between your and my code: composition over inheritance.
@SuppressWarnings("serial")
final JPanel square = new JPanel() {
@Override
public void paintComponent(final Graphics graphics) {
super.paintComponent(graphics);
graphics.setColor(Color.RED);
graphics.fillRect(0, 0, rectangle01.width, rectangle01.height);
graphics.drawRect(0, 0, rectangle01.width, rectangle01.height);
}
};
// Let's also add a working key listener, shall we?
frame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent event) {
super.keyPressed(event);
switch (event.getKeyCode()) {
case KeyEvent.VK_D:
square.setLocation(square.getX() + 6, square.getY());
break;
case KeyEvent.VK_A:
square.setLocation(square.getX() - 6, square.getY());
break;
}
}
});
// Don't forget to set the bounds of a component when working with a null layout!
square.setBounds(rectangle01);
@SuppressWarnings("serial")
final JPanel floor = new JPanel() {
@Override
public void paintComponent(final Graphics graphics) {
super.paintComponent(graphics);
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, rectangle02.width, rectangle02.height);
graphics.drawRect(0, 0, rectangle02.width, rectangle02.height);
}
};
// And don't forget to set the bounds here too.
floor.setBounds(rectangle02);
// Now, just add all components to the frame. Voilà!
frame.add(square);
frame.add(floor);
});
}
}
只是想感谢你顺便回答我的一个问题。