关于JPanel重绘的问题

时间:2012-04-19 20:20:21

标签: java swing

我正在尝试实现放大功能。

我目前的问题是:每次单击放大时,面板上都会出现一个新的缩放图像。但原始图像仍在面板上。所以我尝试使用重绘方法。问题是,我可以看到屏幕上闪烁的新图像然后消失。我是以错误的方式使用代码吗? 目前代码是这样的:

双击放大:

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

1 个答案:

答案 0 :(得分:2)

您必须使用panel.remove(image)删除原始图像。然后,您必须添加新图像,然后repaint()

建议缩放我宁愿使用CardLayout并将原始图像保存在一张卡中