问一个SWING里PANEL放大缩小的问题

时间:2023-02-05 19:58:18
用一个图标点击PANEL里的IMAGE,然后放大或缩小,但是,我的程序老是闪烁,能不能帮我解决这个问题,主要是刷新了两次

13 个解决方案

#1


建立buffer,在buffer中处理好图像后,再paint/draw到屏幕上。
参考SUN的FAQ
http://java.sun.com/docs/books/tutorial/2d/problems/index.html

#2


你能看看我的程序先,有点奇怪

#3


你把程序贴出来啊!

#4


应该不会啊,通常swing都用到了双缓冲,不应该有你说的问题的存在啊。

#5


mapWidth /= 1.35;
        mapHeight /= 1.35;
        reScale(1.0/1.35);
        setPreferredSize(new Dimension(mapWidth, mapHeight));
        scrollRectToVisible(new Rectangle((int)(pl.x/1.35-450), (int)(pl.y/1.35-325), 900, 650));
        revalidate();
        repaint()

#6


你的代码是在太有限,不知道问题出在哪儿?
你这样试试:
mapWidth /= 1.35; 
mapHeight /= 1.35; 
reScale(1.0/1.35); 
invalidate()
setPreferredSize(new Dimension(mapWidth, mapHeight)); 
scrollRectToVisible(new Rectangle((int)(pl.x/1.35-450), (int)(pl.y/1.35-325), 900, 650)); 
validate(); 
repaint()

#7


你的代码是在太有限,不知道问题出在哪儿?
你这样试试:
mapWidth /= 1.35; 
mapHeight /= 1.35; 
reScale(1.0/1.35); 
invalidate()
setPreferredSize(new Dimension(mapWidth, mapHeight)); 
scrollRectToVisible(new Rectangle((int)(pl.x/1.35-450), (int)(pl.y/1.35-325), 900, 650)); 
validate(); 
repaint()

#8


其实在scrollRectToVisible(*)里就会调用paintComponent,然后在repaint()里在再调用一次,我已经把paintComponent给屏蔽了,但是,有时还是会闪

#9


但是这样的话,滚动框就不显示了

#10


下面是代码:
  这是基类 jPanel1
  public void paintComponent(Graphics g) {
  System.out.println("a");
        if (! doPaint)
            return;
super.paintComponent(g);
        System.out.println("b");
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        Dimension d = getSize();

        at.setToIdentity();
        at.translate(d.width/2, d.height/2);
        at.scale(scaleX, scaleY);
        at.translate(translateX, translateY);

        AffineTransform saveXform = g2.getTransform();
        g2.transform(at);

        paintMap(g2);
        paintNetwork(g2);

        g2.setTransform(saveXform);
    }

    void click_actionPerformed(MouseEvent e){
    Point pl = e.getPoint();
        System.out.println(pl.x+","+pl.y+" : "+getX()+","+getY());
        doPaint = false;
        mapWidth *= 1.35;
        mapHeight *= 1.35;
        reScale(1.35);
        System.out.println("1");
        setPreferredSize(new Dimension(mapWidth, mapHeight));
        System.out.println("2");
        System.out.println("3");
        scrollRectToVisible(new Rectangle((int)(pl.x*1.35-450), (int)(pl.y*1.35-325), 900, 650));
        System.out.println("4");
        revalidate();
        doPaint = true;
        repaint();
  }
     以下是:
       private void jbInit() throws Exception {
    contentPanel.setLayout(borderLayout);
    contentPanel.setDoubleBuffered(false);

    mapToolBar.setFont(new java.awt.Font("Dialog", 0, 6));
    mapToolBar.setToolTipText("");
    border1 = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.lightGray,new Color(148, 145, 140),Color.lightGray),BorderFactory.createEmptyBorder(2,2,2,2));
    zoomInButton.setBorder(border1);
    zoomInButton.setToolTipText("zoom in");
    zoomInButton.setFocusPainted(false);
    zoomInButton.setMargin(new Insets(0, 0, 0, 0));
    zoomInButton.setRolloverEnabled(true);
    zoomInButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        zoomInButton_actionPerformed(e);
      }
    });
    zoomOutButton.setMargin(new Insets(0, 0, 0, 0));
    zoomOutButton.setFocusPainted(false);
    zoomOutButton.setToolTipText("zoom out");
    zoomOutButton.setBorder(border1);
    zoomOutButton.setRolloverEnabled(true);
    zoomOutButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        zoomOutButton_actionPerformed(e);
      }
    });
    mapToolBar.add(zoomInButton, null);
    mapToolBar.add(zoomOutButton, null);

    jMapScrollPane = new JScrollPane(jPanel1);
    jMapScrollPane.setPreferredSize(new Dimension(950, 700));
    jMapScrollPane.setDoubleBuffered(true);

    contentPanel.add(jMapScrollPane, BorderLayout.CENTER);
    contentPanel.add(mapToolBar, BorderLayout.NORTH);
    this.getContentPane().add(contentPanel, BorderLayout.NORTH);


    pack();
  }

#11


对不起,大哥,我对这个不通

#12


关注!

#13


在我点击放大的时候,居然在jpanel的左上方出现一个滚动框

#1


建立buffer,在buffer中处理好图像后,再paint/draw到屏幕上。
参考SUN的FAQ
http://java.sun.com/docs/books/tutorial/2d/problems/index.html

#2


你能看看我的程序先,有点奇怪

#3


你把程序贴出来啊!

#4


应该不会啊,通常swing都用到了双缓冲,不应该有你说的问题的存在啊。

#5


mapWidth /= 1.35;
        mapHeight /= 1.35;
        reScale(1.0/1.35);
        setPreferredSize(new Dimension(mapWidth, mapHeight));
        scrollRectToVisible(new Rectangle((int)(pl.x/1.35-450), (int)(pl.y/1.35-325), 900, 650));
        revalidate();
        repaint()

#6


你的代码是在太有限,不知道问题出在哪儿?
你这样试试:
mapWidth /= 1.35; 
mapHeight /= 1.35; 
reScale(1.0/1.35); 
invalidate()
setPreferredSize(new Dimension(mapWidth, mapHeight)); 
scrollRectToVisible(new Rectangle((int)(pl.x/1.35-450), (int)(pl.y/1.35-325), 900, 650)); 
validate(); 
repaint()

#7


你的代码是在太有限,不知道问题出在哪儿?
你这样试试:
mapWidth /= 1.35; 
mapHeight /= 1.35; 
reScale(1.0/1.35); 
invalidate()
setPreferredSize(new Dimension(mapWidth, mapHeight)); 
scrollRectToVisible(new Rectangle((int)(pl.x/1.35-450), (int)(pl.y/1.35-325), 900, 650)); 
validate(); 
repaint()

#8


其实在scrollRectToVisible(*)里就会调用paintComponent,然后在repaint()里在再调用一次,我已经把paintComponent给屏蔽了,但是,有时还是会闪

#9


但是这样的话,滚动框就不显示了

#10


下面是代码:
  这是基类 jPanel1
  public void paintComponent(Graphics g) {
  System.out.println("a");
        if (! doPaint)
            return;
super.paintComponent(g);
        System.out.println("b");
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        Dimension d = getSize();

        at.setToIdentity();
        at.translate(d.width/2, d.height/2);
        at.scale(scaleX, scaleY);
        at.translate(translateX, translateY);

        AffineTransform saveXform = g2.getTransform();
        g2.transform(at);

        paintMap(g2);
        paintNetwork(g2);

        g2.setTransform(saveXform);
    }

    void click_actionPerformed(MouseEvent e){
    Point pl = e.getPoint();
        System.out.println(pl.x+","+pl.y+" : "+getX()+","+getY());
        doPaint = false;
        mapWidth *= 1.35;
        mapHeight *= 1.35;
        reScale(1.35);
        System.out.println("1");
        setPreferredSize(new Dimension(mapWidth, mapHeight));
        System.out.println("2");
        System.out.println("3");
        scrollRectToVisible(new Rectangle((int)(pl.x*1.35-450), (int)(pl.y*1.35-325), 900, 650));
        System.out.println("4");
        revalidate();
        doPaint = true;
        repaint();
  }
     以下是:
       private void jbInit() throws Exception {
    contentPanel.setLayout(borderLayout);
    contentPanel.setDoubleBuffered(false);

    mapToolBar.setFont(new java.awt.Font("Dialog", 0, 6));
    mapToolBar.setToolTipText("");
    border1 = BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.lightGray,new Color(148, 145, 140),Color.lightGray),BorderFactory.createEmptyBorder(2,2,2,2));
    zoomInButton.setBorder(border1);
    zoomInButton.setToolTipText("zoom in");
    zoomInButton.setFocusPainted(false);
    zoomInButton.setMargin(new Insets(0, 0, 0, 0));
    zoomInButton.setRolloverEnabled(true);
    zoomInButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        zoomInButton_actionPerformed(e);
      }
    });
    zoomOutButton.setMargin(new Insets(0, 0, 0, 0));
    zoomOutButton.setFocusPainted(false);
    zoomOutButton.setToolTipText("zoom out");
    zoomOutButton.setBorder(border1);
    zoomOutButton.setRolloverEnabled(true);
    zoomOutButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        zoomOutButton_actionPerformed(e);
      }
    });
    mapToolBar.add(zoomInButton, null);
    mapToolBar.add(zoomOutButton, null);

    jMapScrollPane = new JScrollPane(jPanel1);
    jMapScrollPane.setPreferredSize(new Dimension(950, 700));
    jMapScrollPane.setDoubleBuffered(true);

    contentPanel.add(jMapScrollPane, BorderLayout.CENTER);
    contentPanel.add(mapToolBar, BorderLayout.NORTH);
    this.getContentPane().add(contentPanel, BorderLayout.NORTH);


    pack();
  }

#11


对不起,大哥,我对这个不通

#12


关注!

#13


在我点击放大的时候,居然在jpanel的左上方出现一个滚动框