faz
faz

Reputation: 313

Questions about JPanel repaint

I am trying to implement a zoom-in function.

And my current problem is: Every time I clicked zoom-in, a new zoomed image would appear on the panel. But the original image is still on the panel. So I tried using the repaint method. The problem is, I can see the new image flashed on the screen and then disappears. Am I using the code in the wrong way? Currently the code is like this:

Double click to zoom in:

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

The zoom in method:

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

Painting the points:

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

Upvotes: 0

Views: 99

Answers (1)

Nitin Chhajer
Nitin Chhajer

Reputation: 2339

You have to use panel.remove(image) to remove the original image. You got to then add a new image and then repaint()

Suggestion for zooming i will rather use a CardLayout and keep the original image safe in one card

Upvotes: 2

Related Questions