我正在努力完成我的计算机科学课程的最终项目。我们在课堂上使用java,我对它的控件/概念非常熟悉。我已经制作了一个版本的游戏洪水 - 然而,分配的要求是OOP,继承/接口和多态。我已经有其他组件和我的游戏工作,但我不知道如何包括那些。此外,有时它会出现故障并且不会切换应该切换的随机方块,但我无法找到任何问题
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class FloodIt implements ActionListener
{
Font clicksFont = new Font("Helvetica", Font.BOLD, 30);
Font promptFont = new Font("Helvetica", Font.PLAIN, 18);
Font newGameFont = new Font("Helvetica", Font.ITALIC, 25);
Font titleFont = new Font("Helvetica", Font.BOLD|Font.ITALIC, 40);
JFrame frame;
JPanel contentPane, main, grid, colours;
JLabel prompt, clicks, result, title;
JButton yellow, orange, cyan, pink, green, magenta, newGame;
Random rand = new Random();
String[] grids = {"cyanmini.jpg", "greenmini.png", "magentamini.jpg", "orangemini.png", "pinkmini.jpg", "yellowmini.png"};
JLabel[][] coloursArray = new JLabel[14][14];
int numClicks = 0;
boolean filled = false;
public FloodIt()
{
//Create and set up the frame
frame = new JFrame("Flood-It");
frame.setBounds(600,100,10,10);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a content pane jpanel
contentPane = new JPanel();
contentPane.setBackground(new Color(210,250,230));
contentPane.setBorder(BorderFactory.createEmptyBorder(20,50,20,50));
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
//////////////////////////////////////////////
//Create a new main jpanel
main = new JPanel();
main.setLayout(new FlowLayout(FlowLayout.CENTER));
main.setBackground(new Color(210,250,230));
main.setPreferredSize(new Dimension(670, 225));
main.setMinimumSize(new Dimension(670, 225));
main.setMaximumSize(new Dimension(670, 225));
contentPane.add(main);
//Create, and add label to main jpanel
title = new JLabel("F L O O D - I T");
title.setFont(titleFont);
title.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
main.add(title);
//Create, and add label to main jpanel
prompt = new JLabel("Click a colour button to change the flood colour. Fill the board with a single colour.");
prompt.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
prompt.setFont(promptFont);
main.add(prompt);
//Create, and add label to main jpanel
clicks = new JLabel(String.valueOf(numClicks) +"/30");
clicks.setFont(clicksFont);
clicks.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
main.add(clicks);
//Create, and add label to main jpanel
result = new JLabel("");
result.setFont(clicksFont);
result.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
main.add(result);
//Creates a button and adds to main jpanel
newGame = new JButton("New Game");
newGame.setFont(newGameFont);
newGame.setActionCommand("newGame");
newGame.addActionListener(this);
main.add(newGame);
////////////////////////////////////////////////
//Create a grid jpanel
grid = new JPanel();
grid.setBorder(BorderFactory.createLineBorder(Color.black, 3));
grid.setBackground(new Color(210,250,230));
grid.setLayout(new GridLayout(14,14,0,0));
grid.setPreferredSize(new Dimension(350, 350));
grid.setMinimumSize(new Dimension(350, 350));
grid.setMaximumSize(new Dimension(350, 350));
contentPane.add(grid);
for (int r=0; r < 14; r++)
{
for (int c=0; c < 14; c++)
{
coloursArray[r][c]= new JLabel(new ImageIcon(grids[rand.nextInt(6)]));
grid.add(coloursArray[r][c]);
}
}
///////////////////////////////////////////////
//Create a new colours jpanel
colours = new JPanel();
colours.setBackground(new Color(210,250,230));
colours.setBorder(BorderFactory.createEmptyBorder(20,50,20,50));
contentPane.add(colours);
//Creates a button
yellow = new JButton(new ImageIcon("yellow.png"));
yellow.setAlignmentX(JButton.CENTER_ALIGNMENT);
yellow.setActionCommand("yellow");
yellow.addActionListener(this);
yellow.setPreferredSize(new Dimension(65, 65));
colours.add(yellow);
//Creates a button
orange = new JButton(new ImageIcon("orange.png"));
orange.setAlignmentX(JButton.CENTER_ALIGNMENT);
orange.setActionCommand("orange");
orange.addActionListener(this);
orange.setPreferredSize(new Dimension(65, 65));
colours.add(orange);
//Creates a button
cyan = new JButton(new ImageIcon("cyan.jpg"));
cyan.setAlignmentX(JButton.CENTER_ALIGNMENT);
cyan.setActionCommand("cyan");
cyan.addActionListener(this);
cyan.setPreferredSize(new Dimension(65, 65));
colours.add(cyan);
//Creates a button
pink = new JButton(new ImageIcon("pink.jpg"));
pink.setAlignmentX(JButton.CENTER_ALIGNMENT);
pink.setActionCommand("pink");
pink.addActionListener(this);
pink.setPreferredSize(new Dimension(65, 65));
colours.add(pink);
//Creates a button
green = new JButton(new ImageIcon("green.png"));
green.setAlignmentX(JButton.CENTER_ALIGNMENT);
green.setActionCommand("green");
green.addActionListener(this);
green.setPreferredSize(new Dimension(65, 65));
colours.add(green);
//Creates a button
magenta = new JButton(new ImageIcon("magenta.jpg"));
magenta.setAlignmentX(JButton.CENTER_ALIGNMENT);
magenta.setActionCommand("magenta");
magenta.addActionListener(this);
magenta.setPreferredSize(new Dimension(65, 65));
colours.add(magenta);
///////////////////////////////////////////////
//Add content pane to frame
frame.setContentPane(contentPane);
//Size and then display the frame.
frame.pack();
frame.setVisible(true);
}
private void changeSurrounding(int row, int col, String nColour, String oColour)
{
if(row < 14 && col < 14)
{
if(String.valueOf(coloursArray[row][col].getIcon()).equals(oColour))
{
if(nColour.equals("yellowmini.png"))
{
coloursArray[row][col].setIcon(new ImageIcon("yellowmini.png"));
}
else if(nColour.equals("orangemini.png"))
{
coloursArray[row][col].setIcon(new ImageIcon("orangemini.png"));
}
else if(nColour.equals("greenmini.png"))
{
coloursArray[row][col].setIcon(new ImageIcon("greenmini.png"));
}
else if(nColour.equals("pinkmini.jpg"))
{
coloursArray[row][col].setIcon(new ImageIcon("pinkmini.jpg"));
}
else if(nColour.equals("cyanmini.jpg"))
{
coloursArray[row][col].setIcon(new ImageIcon("cyanmini.jpg"));
}
else if(nColour.equals("magentamini.jpg"))
{
coloursArray[row][col].setIcon(new ImageIcon("magentamini.jpg"));
}
changeSurrounding(row+1,col, nColour, oColour);
changeSurrounding(row,col+1, nColour, oColour);
}
}
}
//Does actions according to which button has been clicked.
//Pre: none
//Post:Apropriate actions have been done
public void actionPerformed(ActionEvent event)
{
String eventName = event.getActionCommand();
String newColour;
String oldColour = String.valueOf(coloursArray[0][0].getIcon());
int total = 0;
for (int r=0; r < 14; r++)
{
for (int c=0; c < 14; c++)
{
if(String.valueOf(coloursArray[r][c].getIcon()).equals(String.valueOf(coloursArray[0][0].getIcon())))
{
total+=1;
}
}
}
if(total == 194)
{
filled = true;
}
else
{
filled = false;
}
if (eventName.equals("yellow"))
{
numClicks+=1;
clicks.setText(String.valueOf(numClicks) +"/30");
newColour = "yellowmini.png";
changeSurrounding(0, 0, newColour, oldColour);
if(numClicks >= 30)
{
yellow.setVisible(false);
orange.setVisible(false);
pink.setVisible(false);
magenta.setVisible(false);
green.setVisible(false);
cyan.setVisible(false);
if(filled == true && numClicks == 30)
{
result.setText("You Won :)");
}
else
{
result.setText("You Lost :(");
}
}
}
else if (eventName.equals("orange"))
{
numClicks+=1;
clicks.setText(String.valueOf(numClicks) +"/30");
newColour = "orangemini.png";
changeSurrounding(0,0, newColour, oldColour);
if(numClicks >= 30)
{
yellow.setVisible(false);
orange.setVisible(false);
pink.setVisible(false);
magenta.setVisible(false);
green.setVisible(false);
cyan.setVisible(false);
if(filled == true && numClicks == 30)
{
result.setText("You Won!");
}
else
{
result.setText("You Lost");
}
}
}
else if (eventName.equals("cyan"))
{
numClicks+=1;
clicks.setText(String.valueOf(numClicks) +"/30");
newColour = "cyanmini.jpg";
changeSurrounding(0,0, newColour, oldColour);
if(numClicks >= 30)
{
yellow.setVisible(false);
orange.setVisible(false);
pink.setVisible(false);
magenta.setVisible(false);
green.setVisible(false);
cyan.setVisible(false);
if(filled == true && numClicks == 30)
{
result.setText("You Won!");
}
else
{
result.setText("You Lost");
}
}
}
else if (eventName.equals("pink"))
{
numClicks+=1;
clicks.setText(String.valueOf(numClicks) +"/30");
newColour = "pinkmini.jpg";
changeSurrounding(0, 0, newColour, oldColour);
if(numClicks >= 30)
{
yellow.setVisible(false);
orange.setVisible(false);
pink.setVisible(false);
magenta.setVisible(false);
green.setVisible(false);
cyan.setVisible(false);
if(filled == true && numClicks == 30)
{
result.setText("You Won!");
}
else
{
result.setText("You Lost");
}
}
}
else if (eventName.equals("green"))
{
numClicks+=1;
clicks.setText(String.valueOf(numClicks) +"/30");
newColour = "greenmini.png";
changeSurrounding(0,0, newColour, oldColour);
if(numClicks >= 30)
{
yellow.setVisible(false);
orange.setVisible(false);
pink.setVisible(false);
magenta.setVisible(false);
green.setVisible(false);
cyan.setVisible(false);
if(filled == true && numClicks == 30)
{
result.setText("You Won!");
}
else
{
result.setText("You Lost");
}
}
}
else if (eventName.equals("magenta"))
{
numClicks+=1;
clicks.setText(String.valueOf(numClicks) +"/30");
newColour = "magentamini.jpg";
changeSurrounding(0,0, newColour, oldColour);
if(numClicks >= 30)
{
yellow.setVisible(false);
orange.setVisible(false);
pink.setVisible(false);
magenta.setVisible(false);
green.setVisible(false);
cyan.setVisible(false);
if(filled == true && numClicks == 30)
{
result.setText("You Won!");
}
else
{
result.setText("You Lost");
}
}
}
else if(eventName.equals("newGame"))
{
numClicks = 0;
clicks.setText(String.valueOf(numClicks) +"/30");
result.setText("");
for (int r=0; r < 14; r++)
{
for (int c=0; c < 14; c++)
{
grid.remove(coloursArray[r][c]);
coloursArray[r][c]= new JLabel(new ImageIcon(grids[rand.nextInt(6)]));
grid.add(coloursArray[r][c]);
}
}
yellow.setVisible(true);
orange.setVisible(true);
pink.setVisible(true);
magenta.setVisible(true);
green.setVisible(true);
cyan.setVisible(true);
}
}
private static void runGUI()
{
JFrame.setDefaultLookAndFeelDecorated(true);
FloodIt start = new FloodIt();
}
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
runGUI();
}
});
}
}
答案 0 :(得分:0)
除非您必须编写自己的接口,否则您已经实现了ActionListener接口。因此,要添加多态性和继承,您需要拆分FloodIt类。转到模型,视图和控制器模式。在您这样做之后,将更容易实现所需的方法。你可以做一个抽象方形超类,然后做它的不同颜色的子类。之后,您可以将Squares放在2D数组中,然后根据调用的有色方块调用方法。通过这种方式,您实现了继承和多态,因为这些方法是在运行时选择的。