私はこれを理解しようとして、過去1日半の間グーグルを検索してきました。ご覧のとおり、mouseClicked で変数 clickcolumnindex と clickrowindex を更新して、mouseClicked メソッドの replaint() メソッドを更新し、別の列/行内にペイント表示によって作成されたカウンターを表示しようとしています。
いろいろ試してみた結果、これは本当に簡単な修正かもしれないと思います。
助けていただければ幸いです。
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
class GridPanel extends JPanel implements MouseListener
{
int numCols;
int numRows;
int w;
int h;
int x;
int y;
int clickcolumnindex;
int clickrowindex;
public void mouseReleased(MouseEvent event)
{
System.out.println("Mouse released.");
}
public void mousePressed(MouseEvent event)
{
System.out.println("Mouse Pressed");
}
public void mouseClicked(MouseEvent event)
{
int clickcolumnindex = 7 * x / getWidth();
int clickrowindex = 5 * y / getHeight();
int x = event.getX();
int y = event.getY();
System.out.println("x position of click is: " +x);
System.out.println("y position of click is: " +y);
System.out.println("Click is in column " +clickcolumnindex);
System.out.println("Click is in row " +clickrowindex);
repaint();
}
public void mouseReleased(MouseEvent event) {
System.out.println("Mouse released.");
}
public void mousePressed(MouseEvent event) {
System.out.println("Mouse Pressed");
}
public void mouseClicked(MouseEvent event) {
int x = event.getX();
int y = event.getY();
System.out.println("x position of click is: " + x);
System.out.println("y position of click is: " + y);
System.out.println("Click is in column " + clickcolumnindex);
System.out.println("Click is in row " + clickrowindex);
repaint();
}
public void mouseEntered(MouseEvent event) {
System.out.println("Mouse entered.");
}
public void mouseExited(MouseEvent event) {
System.out.println("Mouse exited.");
}
public GridPanel(int nc, int nr) {
numCols = nc;
numRows = nr;
addMouseListener(this);
}
Rectangle getRect(int thisCol, int thisRow) {
// if input is out of range, return "null"
if (thisCol < 0 || thisRow < 0)
return null;
if (thisCol >= numCols || thisRow >= numRows)
return null;
// otherwise, make and return the Rectangle
int w = getWidth() / numCols;
int h = getHeight() / numRows;
int x = thisCol * w;
int y = thisRow * h;
Rectangle myRect = new Rectangle(x, y, w, h);
return myRect;
}
public void paint(Graphics g) {
g.setColor(Color.gray);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.black);
Graphics2D g2 = (Graphics2D) g;
// we'll use Graphics2D for it's "draw" method -
// neater than the Graphics "drawRect" suppled
// (which you could also use)
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
Rectangle r = getRect(i, j);
g2.draw(r);
}
}
g2.setColor(Color.blue);
Rectangle r1 = getRect(clickcolumnindex, clickrowindex);
g2.fillOval(r1.x, r1.y, r1.width, r1.height);
System.out.println("variable updates " + clickcolumnindex + clickrowindex);
}
// copied from the W2MouseEvents for convenience
// (so we can run the program from GridPanel class too!)
public static void main(String[] args) {
W2MouseEvents w = new W2MouseEvents();
w.setVisible(true);
}
}
W2MouseEvents.java:
import javax.swing.JFrame;
public class W2MouseEvents extends JFrame {
GridPanel myGridPanel;
public static void main(String[] args) {
W2MouseEvents w = new W2MouseEvents();
w.setVisible(true);
}
public W2MouseEvents() {
setTitle("Workshop 2 (Mouse Events): starting code");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocation(300, 300);
myGridPanel = new GridPanel(7, 5);
add(myGridPanel);
}
}