需要一些关于大学作业的指导。所以,如果你能给我指点,而不是实际的解决方案,那将是非常感谢的。
任务是修改现有程序,以便当鼠标悬停在绘图板的黑暗方块上时,只有黑暗方块被更改为不同的颜色。当鼠标离开黑暗的方块时,它会回到原来的颜色。
我已经包括了两门课,我觉得这两门课对解决这个任务很重要。
谢谢你的帮助。我在这个问题上已经抓了一段时间了。部分任务是只编辑Board类,而不编辑其他类。
public class Board extends JPanel implements MouseListener
{
private Square square;
private Square[][] squares;
private ArrayList<DraughtsPiece> lightPieces;
private ArrayList<DraughtsPiece> darkPieces;
public Board(int numRows)
{
super(new GridLayout(numRows, numRows));
squares = new Square[numRows][numRows];
lightPieces = new ArrayList<DraughtsPiece>();
darkPieces = new ArrayList<DraughtsPiece>();
setupBoard(numRows);
setupPieces(numRows);
allocatePieces();
}
private void setupBoard(int numRows)
{
boolean lightSquare = true;
for (int row = 0; row < numRows; row++)
{
for (int col = 0; col < numRows; col++)
{
if (lightSquare)
{
squares[row][col] = new Square(Square.LIGHT_SQUARE_COLOUR,
row + 1, col + 1);
}
else
{
squares[row][col] = new Square(Square.DARK_SQUARE_COLOUR,
row + 1, col + 1);
addMouseListener(this);
}
add(squares[row][col]);
lightSquare = !lightSquare;
}
lightSquare = !lightSquare;
}
}
private void setupPieces(int numRows)
{
int numPieces = ((numRows * numRows) - (2 * numRows)) / 4;
for (int i = 0; i < numPieces; i++)
{
DraughtsPiece p = new
DraughtsPiece(DraughtsPiece.LIGHT_PIECE_COLOUR);
lightPieces.add(p);
p = new DraughtsPiece(DraughtsPiece.DARK_PIECE_COLOUR);
darkPieces.add(p);
}
}
private void allocatePieces()
{
int row = squares.length - 1;
int col = 0;
for (DraughtsPiece p : lightPieces)
{
squares[row][col].setPiece(p);
col += 2;
if (col >= squares[0].length)
{
col = row % 2;
row--;
}
}
row = 0;
col = squares[0].length - 1;
for (DraughtsPiece p : darkPieces)
{
squares[row][col].setPiece(p);
col -= 2;
if (col < 0)
{
row++;
col = squares[0].length - 1 - (row % 2);
}
}
}
我知道可以使用鼠标适配器来删除MouseListener的空方法,尽管我们还没有学到这一点。我还没有在这段代码中包含未使用的方法。
public void mouseExited(MouseEvent e)
{
if (e.getSource().equals(square))
{
square.exit();
}
}
public void mouseEntered(MouseEvent e)
{
if (e.getSource().equals(square))
{
square.enter();
}
}
这里是广场班
public class Square extends JPanel
{
public static final Color LIGHT_SQUARE_COLOUR = new Color(0xdfdfdf);
public static final Color DARK_SQUARE_COLOUR = new Color(0x333333);
public static final Color SELECTED_DARK_SQUARE_COLOUR = Color.YELLOW;
private Color background;
private int row, column;
private Color selectedBackground;
private DraughtsPiece piece;
private boolean selected = false;
public Square(Color c, int row, int col)
{
super(new BorderLayout());
background = c;
this.row = row;
this.column = col;
setBackground(background);
if (background == DARK_SQUARE_COLOUR)
{
selectedBackground = SELECTED_DARK_SQUARE_COLOUR;
}
piece = null;
}
public int getRow()
{
return row;
}
public int getColumn()
{
return column;
}
public void setPiece(DraughtsPiece piece)
{
if (piece == null && this.piece != null)
{
remove(this.piece);
this.piece.setSquare(null);
this.piece = null;
}
else if (piece != null && this.piece == null)
{
this.piece = piece;
piece.setSquare(this);
add(piece);
}
}
public DraughtsPiece getPiece()
{
return piece;
}
protected void enter()
{
setBackground(selectedBackground);
}
protected void exit()
{
setBackground(background);
}
protected void setSelected(boolean b)
{
selected = b;
setBackground(b == false ? background : selectedBackground);
}
}
发布于 2017-04-05 15:59:20
您总是比较同一个Square
对象(称为square
),它从未被使用过。
您应该直接使用作为事件源的Square
,例如:
public void mouseExited(MouseEvent e)
{
if (e.getSource() instanceof Square)
{
((Square)e.getSource()).exit();
}
}
https://stackoverflow.com/questions/43235973
复制相似问题