我有一个 java 应用程序,它允许用户在 jScrollPane 中放置图像。该应用程序显示一个框,该框与网格对齐,以显示将放置图块的位置以及有关选项和在整个窗格中的位置的一些信息。我在运行 Lion 的 Mac 上运行该程序。当使用触控板水平滚动时,绘制在光标周围的信息会更新并随光标移动。但是,在垂直滚动时,指示将放置图块的位置的信息和框拖动而不是随着光标的位置更新而不是更新,直到鼠标移动为止。此外,使用箭头键垂直或水平滚动时,不会重新绘制信息和框。
我假设问题在于它没有调用paint方法,但是我不知道如何在滚动时调用paint方法,或者为什么在仅使用触控板水平滚动时它似乎仍然调用它。下面是初始化 jScrollPane 和绘制方法的代码。
滚动窗格:
public class Map extends JFrame
{
private static final long serialVersionUID = 8527533451039189417L;
final int AREA_SIZE_X = 100;
final int AREA_SIZE_Y = 100;
JFrame sWindow;
mapPanel mPanel;
JScrollPane mScrollPane;
Map() throws IOException
{
mPanel = new mapPanel(sWindow);
this.getContentPane().setLayout(new BorderLayout());
mScrollPane = new JScrollPane(mPanel);
this.getContentPane().add(mScrollPane);
mPanel.requestFocus();
this.setTitle("Map maker");
this.setLocation(400, 0);
this.setResizable(false);
this.pack();
}
}
油漆方法:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, AREA_SIZE_X * 20, AREA_SIZE_Y * 20);
mouseLocation = this.getMousePosition();
if (this.hasFocus() && this.contains(mouseLocation))
{
g.drawImage(bFS.get(sWindow.getSelected()), mouseLocation.x - 20, mouseLocation.y - 20, this);
g.setColor(Color.red);
g.drawRect(((mouseLocation.x - 10) / 20) * 20, ((mouseLocation.y - 10) / 20) * 20, 20, 20);
String xDisp = "x: " + ((mouseLocation.x - 10) / 20);
String yDisp = "y: " + ((mouseLocation.y - 10) / 20);
g.drawString(xDisp, mouseLocation.x - 20, mouseLocation.y + 25);
g.drawString(yDisp, mouseLocation.x - 20, mouseLocation.y + 40);
String layerDisp = "Layer: " + sWindow.isLayered();
g.drawString(layerDisp, mouseLocation.x - 20, mouseLocation.y + 55);
int tempCounter = 0;
while (tempCounter < 4 && tiles[((mouseLocation.x - 10) / 20)][((mouseLocation.y - 10) / 20)][tempCounter] != -1)
{
tempCounter++;
}
String layerLevelDisp = "Num Layers: " + tempCounter;
g.drawString(layerLevelDisp, mouseLocation.x - 20, mouseLocation.y + 70);
}
}