在Java中创建第一个子类时遇到问题
我正在尝试为我的Java课程制作一个突破游戏程序,其中包括一组彩色块(砖块)。我已经成功地做到了这一点,但现在我的导师希望我们添加不同的砖块子类。他想要的子类之一是ColorBrick,它继承了Brick超类的所有行为,只是它有一个每5个刻度改变一次的颜色数组。构造函数采用颜色数组而不是单一颜色 这是我的Brick超类的内容:在Java中创建第一个子类时遇到问题,java,inheritance,subclass,superclass,Java,Inheritance,Subclass,Superclass,我正在尝试为我的Java课程制作一个突破游戏程序,其中包括一组彩色块(砖块)。我已经成功地做到了这一点,但现在我的导师希望我们添加不同的砖块子类。他想要的子类之一是ColorBrick,它继承了Brick超类的所有行为,只是它有一个每5个刻度改变一次的颜色数组。构造函数采用颜色数组而不是单一颜色 这是我的Brick超类的内容: package Breakout; import java.awt.Color; import java.awt.Graphics; public class
package Breakout;
import java.awt.Color;
import java.awt.Graphics;
public class Brick {
public int x, y, i, j;
public Color c;
Brick[][] brick;
public Brick() {
}
public Brick(Color c, Brick[][] brick, int i, int j) {
this.c = c;
this.brick = brick;
this.x = i * 40;
this.y = j * 10 + 50;
this.i = i;
this.j = j;
}
public void tick() {
}
public void paint(Graphics g) {
g.setColor(c);
g.fillRect(x, y, Breakout.brickWidth, Breakout.brickHeight);
}
public void hit(Ball b) {
if (b.yThen > y + Breakout.brickHeight) {
b.yv = -b.yv;
b.yNow = 2 * (y + Breakout.brickHeight) - b.yNow;
}
if (b.yThen < y) {
b.yv = -b.yv;
b.yNow = 2 * (y) - b.yNow;
}
if (b.xThen > x + Breakout.brickWidth) {
b.xv = -b.xv;
b.xNow = 2 * (x + Breakout.brickWidth) - b.xNow;
}
if (b.xThen < x) {
b.xv = -b.xv;
b.xNow = 2 * (x) - b.xNow;
}
brick[i][j] = null;
}
}
在这一点上,我碰到了一堵墙,我不知道从这里该做什么。我的指导老师说,出于某种原因,tick方法在超类中必须为空。如果它必须是空的,他为什么要我们把它放在那里?我也不确定应该在子类tick方法中添加什么。我的子类是否朝着正确的方向前进,或者到目前为止我所做的一切都是完全错误的?任何指导都将不胜感激
以下是我的主要突破代码(如果有用):
package Breakout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Breakout extends javax.swing.JFrame {
public static final int fieldHeight = 600;
public static final int fieldWidth = 400;
public int diameter = 5;
public int xNow = 200 - (diameter / 2);
public int yNow = 300 - (diameter - 2);
public int paddleWidth = 50;
public int paddleHeight = 5;
public int platform = 580;
public static int mousex;
public int mousey;
public static int brickWidth = 40;
public static int brickHeight = 10;
public Random randoms = new Random();
Brick[][] bricks = new Brick[arrayWide][arrayHigh];
public static boolean startUp;
public static int arrayWide = 10;
public static int arrayHigh = 5;
Ball ball = new Ball(200 - (diameter / 2), 20 /*300 - (diameter /2)*/, 0, 0, diameter, Color.white);
Paddle myPaddle = new Paddle(platform, paddleWidth, paddleHeight);
/**
* Creates new form Breakout
*/
public Breakout() {
initComponents();
clock.start();
}
public class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
if (startUp) {
ball.paint(g);
}
myPaddle.paint(g);
for (int i = 0; i < arrayWide; i++) {
for (int j = 0; j < arrayHigh; j++) {
if (bricks[i][j] != null) {
bricks[i][j].paint(g);
}
}
}
// Insert code to paint the scene here.
// Use methods in the Graphics class to do the painting
// Remember coordinates use (0,0) at the top left
}
}
public Timer clock = new Timer(50, new ActionListener() { // 50ms delay between ticks
public void actionPerformed(ActionEvent e) {
tick(); // Write a method named tick to advance your game
jPanel1.repaint();
}
}); // panel is the name of the JPanel that displays the game
public void launchball() {
}
public void tick() {
ball.move();
Brick brick2 = brickAt(ball);
if (brick2 != null) {
brick2.hit(ball);
}
myPaddle.move(mousex);
myPaddle.bounce(ball);
System.out.println();
}
public Brick brickAt(Ball b) {
int j = (int) (b.yNow - 50) / 10;
int i = (int) (b.xNow / 40);
if (i < arrayWide && i >= 0 && j < arrayHigh && j >= 0) {
return (bricks[i][j]);
}
return null;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
jPanel2 = new javax.swing.JPanel();
jPanel1 = new MyPanel();
jLabel1 = new javax.swing.JLabel();
start = new javax.swing.JButton();
org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
mouseMove(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 600, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Hobo Std", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(153, 51, 255));
jLabel1.setText("Breakout");
start.setFont(new java.awt.Font("Hobo Std", 0, 13)); // NOI18N
start.setForeground(new java.awt.Color(255, 51, 0));
start.setText("Start");
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(164, 164, 164)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(15, 15, 15)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(175, 175, 175)
.add(start)))
.addContainerGap(17, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(16, 16, 16)
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(start)
.addContainerGap(10, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void mouseMove(java.awt.event.MouseEvent evt) {
mousex = evt.getX();
mousey = evt.getY();
}
private void startActionPerformed(java.awt.event.ActionEvent evt) {
startUp = true;
ball.xNow = 200 - (diameter / 2);
ball.yNow = 300 - (diameter - 2);
ball.xv = 0;
ball.yv = 8;
for (int i = 0; i < arrayWide; i++) {
for (int j = 0; j < arrayHigh; j++) {
if ((j % 2 == 0 && i % 2 == 0) || (j % 2 == 1 && i % 2 == 1)) {
bricks[i][j] = new Brick(Color.magenta, bricks, i, j);
} else {
bricks[i][j] = new Brick(Color.gray, bricks, i, j);
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Breakout().setVisible(true);
}
});
}
包断开;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.Random;
导入javax.swing.JPanel;
导入javax.swing.Timer;
公共类突破扩展了javax.swing.JFrame{
公共静态最终int fieldHeight=600;
公共静态最终整型字段宽度=400;
公共内径=5;
公共int xNow=200-(直径/2);
公共int yNow=300-(直径-2);
公共宽度=50;
公共高度=5;
公共int平台=580;
公共静态鼠标;
公鼠;
公共静态int brickWidth=40;
公共静态整数砖八=10;
公共随机数=新随机数();
砖块[][]砖块=新砖块[arrayWide][arrayHigh];
公共静态布尔启动;
阵列范围内的公共静态int=10;
公共静态int阵列高=5;
球=新球(200-(直径/2),20/*300-(直径/2)*/,0,0,直径,颜色。白色);
桨我的桨=新桨(平台、桨宽、桨高);
/**
*创建新的窗体局部剖视图
*/
公众突破(){
初始化组件();
clock.start();
}
公共类MyPanel扩展了JPanel{
@凌驾
公共空间涂料(图g){
超级油漆(g);
如果(启动){
球形涂料(g);
}
我的桨。油漆(g);
for(int i=0;i=0&&j=0){
返回(砖块[i][j]);
}
返回null;
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jPopupMenu1=newjavax.swing.JPopupMenu();
jPanel2=newjavax.swing.JPanel();
jPanel1=新的MyPanel();
jLabel1=newjavax.swing.JLabel();
start=newjavax.swing.JButton();
org.jdesktop.layout.GroupLayout jPanel2Layout=新建org.jdesktop.layout.GroupLayout(jPanel2);
设置布局(jPanel2布局);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.添加(0,100,短。最大值)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.添加(0,100,短。最大值)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setBackground(新java.awt.Color(0,0,0));
addMouseMotionListener(新的java.awt.event.MouseMotionAdapter(){
public void mouseMoved(java.awt.event.MouseEvent evt){
mouseMove(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout=新建org.jdesktop.layout.GroupLayout(jPanel1);
setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.添加(0,400,短。最大值)
);
package Breakout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Breakout extends javax.swing.JFrame {
public static final int fieldHeight = 600;
public static final int fieldWidth = 400;
public int diameter = 5;
public int xNow = 200 - (diameter / 2);
public int yNow = 300 - (diameter - 2);
public int paddleWidth = 50;
public int paddleHeight = 5;
public int platform = 580;
public static int mousex;
public int mousey;
public static int brickWidth = 40;
public static int brickHeight = 10;
public Random randoms = new Random();
Brick[][] bricks = new Brick[arrayWide][arrayHigh];
public static boolean startUp;
public static int arrayWide = 10;
public static int arrayHigh = 5;
Ball ball = new Ball(200 - (diameter / 2), 20 /*300 - (diameter /2)*/, 0, 0, diameter, Color.white);
Paddle myPaddle = new Paddle(platform, paddleWidth, paddleHeight);
/**
* Creates new form Breakout
*/
public Breakout() {
initComponents();
clock.start();
}
public class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
if (startUp) {
ball.paint(g);
}
myPaddle.paint(g);
for (int i = 0; i < arrayWide; i++) {
for (int j = 0; j < arrayHigh; j++) {
if (bricks[i][j] != null) {
bricks[i][j].paint(g);
}
}
}
// Insert code to paint the scene here.
// Use methods in the Graphics class to do the painting
// Remember coordinates use (0,0) at the top left
}
}
public Timer clock = new Timer(50, new ActionListener() { // 50ms delay between ticks
public void actionPerformed(ActionEvent e) {
tick(); // Write a method named tick to advance your game
jPanel1.repaint();
}
}); // panel is the name of the JPanel that displays the game
public void launchball() {
}
public void tick() {
ball.move();
Brick brick2 = brickAt(ball);
if (brick2 != null) {
brick2.hit(ball);
}
myPaddle.move(mousex);
myPaddle.bounce(ball);
System.out.println();
}
public Brick brickAt(Ball b) {
int j = (int) (b.yNow - 50) / 10;
int i = (int) (b.xNow / 40);
if (i < arrayWide && i >= 0 && j < arrayHigh && j >= 0) {
return (bricks[i][j]);
}
return null;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPopupMenu1 = new javax.swing.JPopupMenu();
jPanel2 = new javax.swing.JPanel();
jPanel1 = new MyPanel();
jLabel1 = new javax.swing.JLabel();
start = new javax.swing.JButton();
org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(0, 0, 0));
jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
mouseMove(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 400, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(0, 600, Short.MAX_VALUE)
);
jLabel1.setFont(new java.awt.Font("Hobo Std", 1, 24)); // NOI18N
jLabel1.setForeground(new java.awt.Color(153, 51, 255));
jLabel1.setText("Breakout");
start.setFont(new java.awt.Font("Hobo Std", 0, 13)); // NOI18N
start.setForeground(new java.awt.Color(255, 51, 0));
start.setText("Start");
start.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(164, 164, 164)
.add(jLabel1))
.add(layout.createSequentialGroup()
.add(15, 15, 15)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(175, 175, 175)
.add(start)))
.addContainerGap(17, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(16, 16, 16)
.add(jLabel1)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
.add(start)
.addContainerGap(10, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void mouseMove(java.awt.event.MouseEvent evt) {
mousex = evt.getX();
mousey = evt.getY();
}
private void startActionPerformed(java.awt.event.ActionEvent evt) {
startUp = true;
ball.xNow = 200 - (diameter / 2);
ball.yNow = 300 - (diameter - 2);
ball.xv = 0;
ball.yv = 8;
for (int i = 0; i < arrayWide; i++) {
for (int j = 0; j < arrayHigh; j++) {
if ((j % 2 == 0 && i % 2 == 0) || (j % 2 == 1 && i % 2 == 1)) {
bricks[i][j] = new Brick(Color.magenta, bricks, i, j);
} else {
bricks[i][j] = new Brick(Color.gray, bricks, i, j);
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Breakout.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Breakout().setVisible(true);
}
});
}
private int i = 0;
public void tick(){
c = colors[i++ % colors.length];
}
Brick[][] wall = new Brick[5][5];