我正在尝试使用显示敌人(红色圆圈),玩家(橙色钻石)和基座(轮廓黑色矩形)的多个JPanel向我的JFrame“框架”添加多个类但它们无法正确显示。我有四个班,“Assignment6”(作业和我的主要名称),“Enemy”(创造敌人的类),“玩家”(创建和移动玩家的类)和“Base”(创造敌人无法进入的基础的类。我还没有在“基地”中为敌人的接触碰撞制定方法。
Assignment6:
import javax.swing.*;
import java.awt.Color;
import java.awt.FlowLayout;
public class Assignment6 extends JFrame {
public static void main(String[] args){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
panel1.add(new Base());
panel2.add(new Enemy());
panel3.add(new Player());
JFrame frame = new JFrame("Assignment 6");
frame.setLayout(new FlowLayout());
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setSize (1000, 1000);
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel3);
frame.setVisible (true);
}
}
敌人:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class Enemy extends JComponent {
public Enemy(){
setPreferredSize(new Dimension(100, 100));
}
public int createEnemyX(){
int enemyX = (int)(100 * Math.random());
return enemyX;
}
public int createEnemyY(){
int enemyY = (int)(100 * Math.random());
return enemyY;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.RED);
g.fillOval(createEnemyX(), createEnemyY(), 100, 100);
}
}
播放器:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
public class Player extends JComponent implements KeyListener{
public int height = getHeight();
public int width = getWidth();
public int x = 500;
public int y = 500;
public Player(){
setPreferredSize(new Dimension(100, 100));
addKeyListener(this);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Color orange = new Color(210, 105,30);
g.setColor(orange);
int[]x1 = {x-50, x, x+50, x};
int[]y1 = {y, y-50, y, y+50};
g.fillPolygon(x1, y1, 4);
}
@Override
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_LEFT)
x -= 10;
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
x += 10;
else if (e.getKeyCode() == KeyEvent.VK_UP)
y -= 10;
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
y += 10;
repaint();
}
@Override
public void keyReleased(KeyEvent e){
;
}
@Override
public void keyTyped(KeyEvent e){
;
}
}
基地:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
public class Base extends JComponent {
public Base(){
setPreferredSize(new Dimension(200, 200));
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(400,400,200,200);
}
}
一旦我运行项目,敌人(红色圆圈)出现但被白色矩形切断。我假设调用了paintComponent方法,但它们没有被渲染。
我已经将这个问题和答案用作我代码的主要基础: How to add multiple classes to a single JFrame
答案 0 :(得分:4)
您的整体程序设计看起来是错误的并且需要重新完成,特别是我正在谈论让您的可绘制实体类,Base,Enemy和Player各自扩展JComponent,因为它允许这些要在其JComponent及其JComponent中绘制的组件,而不是其他任何组件。代替:
draw(Graphics g)
。 关于问题:
我目前正在尝试制作Drawable接口,我应该在该方法中添加什么?
接口在方法中没有任何代码。任何实现该接口的类都需要具有允许该类对象自行绘制的代码。
如果我有DrawingPanel类,我甚至需要Drawable接口吗?
是的,因为DrawingPanel会包含Drawable对象的集合,并会在paintComponent
方法中绘制它们,如上所述。