调用重绘后,JPanel变得清晰(x,y,宽度,高度)

时间:2012-12-04 11:27:50

标签: awt

以下是我的代码部分:

public final class ImagePanel extends JPanel implements SwingConstants, Printable ,MouseListener {

private static final long serialVersionUID = 1L;
private static final int IMAGE_LEFT_INDENT = 30;
private Image image;
private int verticalAlignment = CENTER;
private int horizontalAlignment = CENTER;
private int image_dy;
private Color color;
private int vectorImageIndex = 0;
public int tempIndex;
   @Override
   public Dimension getPreferredSize() {
    if (image == null) {
        return super.getPreferredSize();
    } else {
        return new Dimension(image.getWidth(this) + 100, image.getHeight(this) + 100);
    }
}

   @Override
   public void update(Graphics g)
  {

  }

  @Override
  protected void paintComponent(Graphics g) {
     super.paintComponent(g);

    if (image == null) {
        return;
    }

    Insets insets = getInsets();
    int x = insets.left;
    int y = insets.top;

    int w = getWidth() - insets.left - insets.right;
    int h = getHeight() - insets.top - insets.bottom;

    int src_w = image.getWidth(null);
    int src_h = image.getHeight(null);

    double scale_x = ((double) w) / src_w;
    double scale_y = ((double) h) / src_h;

    double scale = Math.min(scale_x, scale_y);

    int dst_w = (int) (scale * src_w);
    int dst_h = (int) (scale * src_h);

    int dx = x + (w - dst_w) / 2;
    if (horizontalAlignment == LEFT) {
        dx = x;
    } else if (horizontalAlignment == RIGHT) {
        dx = x + w - dst_w;
    }

    int dy = y + (h - dst_h) / 2;
    if (verticalAlignment == TOP) {
        dy = y;
    } else if (verticalAlignment == BOTTOM) {
        dy = y + h - dst_h;
    }

    image_dy = dy;       

    if (previousRect != null) {
         g.setColor(color);
        g.drawRect((previousRect.x + getInsets().left), dy, previousRect.width, previousRect.height);
    } else {
        g.drawImage(image, dx, dy, src_w, src_h, null);
        g.setColor(Color.YELLOW);
        for (Rectangle rect : FontExtractor.vectorRectangle) {
            g.drawRect((rect.x + getInsets().left), dy, rect.width, rect.height);
        }
    }
}
  public void mouseClicked(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        int click_x = e.getX();
        int click_y = e.getY();
        for (Rectangle rect : FontExtractor.vectorRectangle) {
            if (((rect.x + getInsets().left) <= click_x && (rect.x + getInsets().left + rect.width) >= click_x) && (image_dy <= click_y && (image_dy + rect.height) >= click_y)) {
                if (previousRect != null) {
                    color = Color.YELLOW;
                    System.out.println("repaint 1");
                    repaint((previousRect.x + getInsets().left), image_dy, previousRect.width + 1, previousRect.height + 1);
                }
                previousRect = rect;
                color = Color.RED;
                System.out.println("repaint 2");
                repaint((rect.x + getInsets().left), image_dy, rect.width + 1, rect.height + 1);
                hasBreakCommandExecuted = true;
                break;
            }
        }
        if ((!hasBreakCommandExecuted) && (previousRect != null)) {
            color = Color.YELLOW;
            System.out.println("repaint 3");
            repaint((previousRect.x + getInsets().left), image_dy, previousRect.width + 1, previousRect.height + 1);
            previousRect = null;
        } else {
            hasBreakCommandExecuted = false;
        }
    }
}

}

我在这里做错了什么,当我调用重绘(Dimension)时,它会调用PaintComponent(Graphics g)并首先清除jpanel背景。请提一下问题在哪里。

1 个答案:

答案 0 :(得分:0)

这就是AWT / Swing的工作原理。每次调用重绘时,AWT / Swing都会清除该区域并开始从底部组件到顶部一直绘制该区域。现在,如果您的机器速度很慢,您实际上可以看到这种情况发生,这称为闪烁。为了避免Swing中的闪烁,请尝试JPanel.setDoubleBuffered(true)。