我正在尝试实现放大功能。
而我目前的问题是:每次点击放大,面板上都会出现一个新的放大图像。但原始图像仍在面板上。所以我尝试使用重绘方法。问题是,我可以看到新图像在屏幕上闪烁然后消失。我是否以错误的方式使用代码?目前代码是这样的:
双击放大:
panel_Show.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
int count = arg0.getClickCount();
if (count == 2 && !arg0.isConsumed()) {
int x = arg0.getX();
int y = arg0.getY();
zoomin(x, y);
}
}
放大方法:
protected void zoomin(int x, int y) {
if (allowZoomIn && zoomLevel < zoomLimit) {
zoomLevel = zoomLevel + 1;
int width = panel_Show.getWidth();
int height = panel_Show.getHeight();
centerX = centerX - (width/2 - x)/alpha;
centerY = centerY - (height/2 - y)/alpha;
alpha = alpha * zoomLevel;
paintSpace(zoomLevel);
}
}
画点:
protected void paintSpace(int level) {
panel_Show.repaint();
pointDrawer = (Graphics2D) panel_Show.getGraphics();
Iterator<Integer> keyIterator = xCordTable.keySet().iterator();
while (keyIterator.hasNext()) {
int id = keyIterator.next();
double xcord = xCordTable.get(id);
double ycord = yCordTable.get(id);
//below is just some logic code to control how to paint points
int size = freqTable.get(id);
paintStar(xcord, ycord, Color.BLUE, size * level);
}
}