如何通过Java动态调整绘图区域的大小?

时间:2017-01-11 01:39:11

标签: java graphics bufferedimage

我试图调整 BufferedImage (我的绘图区域)的大小而不会丢失图像上的绘制数字,并在调整大小后恢复绘制。(例如 Paint.NET )我用JSplitPane从边缘做这件事并且它正在发生但是当我想恢复绘制时,光标(笔)距离的大约3厘米绘制的图形(形状),因为数字与图像一起增长。当我删除整个图片时,它会变得更好。 Before After

我查看了Google和StackoverFlow中的答案,但它不适用于我。如何摆脱这个问题呢?

到目前为止,我尝试过这样做:

班级用户界面

jSplitPane.addPropertyChangeListener(JSplitPane.DIVIDER_LOCATION_PROPERTY, (PropertyChangeEvent evt) -> { double imageHeight = jSplitPane.getDividerLocation(); double imageWidth = imageHeight * 1.6; top.setDividerLocation((int) imageWidth); drawGround.changeImageSizeDynmcally((int) imageWidth, (int) imageHeight); drawGround.repaint(); });

类DrawGround

     BufferedImage masterImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
    //image is master image drawing at first time.

    public void changeImageSizeDynmcally(int w, int h) {
      AREA_WIDTH = w;
      AREA_HEIGHT = h;
      repaint();

      BufferedImage scaledImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2D = scaledImage.createGraphics();
      g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2D.drawImage(masterImage, 0, 0, null);
      g2D.dispose();
}

在这个方法之后我有

     `@Override
 protected void paintComponent(Graphics g) {
     super.paintComponent(g);
        if (masterImage != null) {
            g2D = (Graphics2D) masterImage.getGraphics();
            g2D.setComposite(AlphaComposite.Src); 
            bla,bla,bla`

1 个答案:

答案 0 :(得分:0)

我通过创建设置图像的新方法解决了这个问题:

item     | sum
---------+------
car      | 2
car      | 1 => still in the different row
jet      | 1 
car, jet | 1

新方法是:

public void changeImageSizeDynmcally(int x, int y) { AREA_WIDTH = x; AREA_HEIGHT = y; repaint(); scaledImage = new BufferedImage(AREA_WIDTH, AREA_HEIGHT, BufferedImage.TYPE_INT_ARGB); Graphics2D g2D = scaledImage.createGraphics(); g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2D.drawImage(image,0,0, null); g2D.dispose(); setImage(scaledImage); //this is a new method! }` 它的工作方式就像将新图像分配给旧图像一样。