我正在做一个绘图板,我有几个问题。
- 每当我尝试使用它时,它都不会自动更新。广告我通常必须调整屏幕大小才能更新。
- 我怎样才能做类似 mouseDragged 函数的事情,我可以在其中不断获得 x 和 y 坐标?
这是代码:
import java.awt.geom.*;
class griddedInput extends JComponent implements MouseListener
{
int SIZE = 10;
int scSize = 300;
int sSize = scSize/SIZE;
boolean [][] grid = new boolean[sSize][sSize];
public griddedInput(boolean grid[][])
{
grid=grid;
setPreferredSize(new Dimension(scSize,scSize));
addMouseListener(this);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
int x, y;
for(y = 0; y < sSize; y ++) {
for(x = 0; x < sSize; x ++) {
if(grid[y][x])
g2.setColor(Color.BLACK);
else
g2.setColor(Color.WHITE);
g2.fillRect((x * SIZE), (y * SIZE), sSize, sSize);
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
int squareX = (int)e.getX() / SIZE;
int squareY = (int)e.getY() / SIZE;
grid[squareY][squareX] = !grid[squareY][squareX];
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}