0

I'm trying to resize my BufferedImage (my drawing area) without losing drawn figures on image and resume to draw after resizing.(Like in Paint.NET) I'm doing this with JSplitPane from edges and it's happening but when I want to resume to draw, the cursor (the pen) has been about 3 cm away from the drawn figure(shape) because the figures grow with image. It just gets better when I remove the whole picture. Before and After

I looked at the answers in Google and StackoverFlow, but it did not work with me.How can I get rid of this issue?

I tried like this so far:

Class UI

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

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

After this method I have

     `@Override
 protected void paintComponent(Graphics g) {
     super.paintComponent(g);
        if (masterImage != null) {
            g2D = (Graphics2D) masterImage.getGraphics();
            g2D.setComposite(AlphaComposite.Src); 
            bla,bla,bla`
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • Make a new `BufferedImage` of the size you want, paint the original `BufferedImage` to it (ie `Graphics#drawImage`), assign the new `BufferedImage` to point to the old one... – MadProgrammer Jan 11 '17 at 02:08
  • You never assign `scaledImage` to anything, so the `masterImage` (which is now the old one) is still been used – MadProgrammer Jan 11 '17 at 02:09
  • @MadProgrammer Thanks for answer, you are right, now i will try and inform u. – Coder ACJHP Jan 11 '17 at 02:11
  • @MadProgrammer for changing the master image with new scaled image i need to create new method returning BufferedImage.Because In my method it's directly drawing. – Coder ACJHP Jan 11 '17 at 02:19
  • 1
    Why not just assign `scaledImage` to `masterImage` within the method (and trigger a repaint) ... `masterImage = scaledImage;` – MadProgrammer Jan 11 '17 at 02:22
  • @MadProgrammer +1 for you.its a good idea now I'm thinking about this how can assign the scaled image to master but I can't because the scaled image is been drawing area it's not return BufferedImage.If I can change the method it will be – Coder ACJHP Jan 11 '17 at 02:25
  • @MadProgrammer THANK YOU SO MUCH i solved it like this look at to my answer. – Coder ACJHP Jan 11 '17 at 02:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132864/discussion-between-coder-acjhp-and-madprogrammer). – Coder ACJHP Jan 11 '17 at 02:37

1 Answers1

0

I solved it with creating new method to set the image :

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!
}`

The new method is :

public void setImage(BufferedImage img) { if (img != null) { g2D = (Graphics2D) img.getGraphics(); g2D.setComposite(AlphaComposite.Src); g2D.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); masterImage = img; repaint(); } } It's working like assigning the new image to old image.

Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • In `g2D.drawImage(image,0,0, null)`, where is 'image' defined??? Don't you test your code before posting it??? This is a downvotable answer, but I don't use to downvote. I prefer showing people's mistakes! – Apostolos Feb 21 '22 at 11:52