Я пытаюсь как-то «выделить» элемент плитки в игре, которую я делаю (Пасьянс Маджонг).Для этого я рисую объект Rectangle2D в той же позиции, что и плитка, и пытаюсь отобразить его при щелчке мыши.
Я могу заставить событие щелчка мыши работать и распознаватькогда плитки выбраны, но по какой-то причине прямоугольник не рисуется, когда я нахожусь в функции mousePressed.Кажется, я не могу понять, почему ...
Вот то, что я считаю соответствующим кодом - я могу расширить его при необходимости!
/* Above this, the positions of tiles are set */
if (content[i][y][x].isVisible()) {
/* Draws the image to screen at the appropriate location */
graphics.drawImage(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW,null);
}
/* Represents the area around a tile, so that you can determine
* whether appropriate area pressed within a tile */
final Rectangle2D rect = new Rectangle2D.Double(x*TILEW+TILEW/2+i*TILESKEW,(y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));
/* Set colour of border rectangle */
graphics.setColor(Color.red);
/* Store positions and sizes of tile objects */
final int xPos = x*TILEW+TILEW/2+i*TILESKEW;
final int yPos = (y+1)*TILEH/2-i*TILESKEW;
final int height = image.getHeight(null)+2;
/* This works - outside of the mouse event */
//graphics.drawRoundRect(xPos, yPos, width, height, 7, 7);
/* Mouse event */
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
/* Draw the rectangle to the screen -- Doesn't display! */
graphics.drawRoundRect(xPos, yPos, width, height, 7, 7);
}
Графический объект "graphics" передается в функцию:
public void paintComponent(final Graphics graphics) { ... }
Будем благодарны за любые предложения!Заранее благодарю за помощь!