过去一天半我一直在搜索谷歌,试图解决这个问题。 正如您所看到的,我正在尝试使用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);
}
}
答案 0 :(得分:3)
至少有两个问题:
变量不会神奇地自动更新。
如果更改了x
,则右侧没有变量(假设它们在同一范围内)的变量不会更新。
澄清:
x
假设您想要全局更改int x = 4;
int var = x + 1; // var == 5
x = 5 // var still is 5, not 6
和x
“(如”班级实例的属性“),您需要解决的第二个问题是
y
这里发生的是你宣布两个新的本地变量public void mouseClicked(MouseEvent event)
{
int x = event.getX();
int y = event.getY();
// ...
}
和x
,但无论你赋予它们什么价值,你的“全局”变量y
并且x
不会改变
除非你遗漏声明y
。
如果您在其中一个mouseClicked / mousePressed / ...方法中重新计算1> int
和clickColumnIndex
的值,则不会出现此问题。
修改强>
我看到你已将clickRowIndex
及其兄弟的计算移至clickColumnIndex
方法。但如上所述,您正在创建两个新变量,隐藏“全局”变量。只需删除mouseClicked
。
答案 1 :(得分:1)
在声明鼠标事件之前,您可以设置clickcolumnindex。您需要在鼠标侦听器中设置它。
换句话说,这是毫无意义的:
class GridPanel extends JPanel implements MouseListener
{
int numCols;
int numRows;
int w;
int h;
int x;
int y;
int clickcolumnindex = 7 * x / getWidth();
int clickrowindex = 5 * y / getHeight();
因为此时x是0,所以getWidth()(这里没有得到除零的错误)。
您想要在MouseListener的mousePressed(...)
方法中设置clickXXXindex。我更喜欢使用mousePressed(...)
而不是mouseClicked(...)
,因为前者在按下鼠标后移动鼠标并不是那么挑剔。
此外,您还希望在JPanel的paintComponent(...)
方法中进行绘制,而不是paint(...)
方法。
答案 2 :(得分:1)
陈述:
int clickcolumnindex = 7 * x / getWidth();
int clickrowindex = 5 * y / getHeight();
不允许在构造函数,方法或静态初始化程序块之外。
正如HoverMeister所指出的那样,这些只会被分配一次。为什么不使用单独的方法来更新这些变量和后续的repaint()?