2012-04-19 50 views
0

我想實現放大功能。關於JPanel重繪的問題

而我目前的問題是:每次我點擊放大,面板上都會出現一個新的縮放圖像。但原始圖像仍在面板上。所以我嘗試使用重繪方法。問題是,我可以看到屏幕上閃過的新圖像然後消失。我是否以錯誤的方式使用代碼? 目前的代碼是這樣的:

雙擊放大:

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); 
    } 
} 

回答

2

你有使用panel.remove(image)刪除原始圖像。你得再添加新的圖像,然後repaint()

建議進行縮放我更喜歡使用CardLayout,並保持原始圖像安全合一

+0

如果您正在繪圖,你將不得不畫或' fillRectangle()'清除面板上需要繪製新對象的空間。但隨着卡的佈局,它會更好。 – 2012-04-19 20:34:31