java - Minimal way to make a cleanable drawing area -


i got class resizable background. there paintings on background(using paint method , java2d). how can delete drawn every time background gets resize? (to draw again in correct places) there sort of transform can on already-drawn objects(like scaling fit image again)?

import java.awt.graphics; import java.awt.graphics2d; import java.awt.renderinghints; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jslider; import javax.swing.event.changeevent; import javax.swing.event.changelistener;  public class background extends jlabel implements changelistener  {     private imageicon background;     private bufferedimage image;      public background(jpanel parent){         super();         parent.add(this);         try {             image = imageio.read(new file("/example/background"));         } catch (ioexception e) {             e.printstacktrace();         }         this.background = new imageicon(image);         this.seticon(background);     }     public void statechanged(changeevent e) {         int value = ((jslider) e.getsource()).getvalue();         double scale = value / 100.0;         bufferedimage scaled = getscaledimage(scale);         this.seticon(new imageicon(scaled));         this.revalidate();     }     private bufferedimage getscaledimage(double scale) {         int w = (int) (scale * image.getwidth());         int h = (int) (scale * image.getheight());         bufferedimage bi = new bufferedimage(w, h, image.gettype());         graphics2d g2 = bi.creategraphics();         g2.setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bicubic);         affinetransform @ = affinetransform.getscaleinstance(scale, scale);         g2.drawrenderedimage(image, at);         g2.dispose();         return bi;      }     @override     public void paint(graphics g){         super.paint(g);         graphics2d graphobj = (graphics2d) g;         renderinghints rh = new renderinghints(renderinghints.key_antialiasing,renderinghints.value_antialias_on);rh.put(renderinghints.key_rendering,renderinghints.value_render_quality);         graphobj.setrenderinghints(rh);         graphobj.filloval(500, 500, 20, 20);         graphobj.finalize();     } } 

consider:

  • drawing in paintcomponent(...) override, not paint(...) override.
  • save list<point> each point normalized, 1000 1000 size.
  • then in paintcomponent method, iterate through each point in loop, scaling current component size, , drawing it.
  • you'll want scale image drawn in component in componentlistener, , call repaint().
  • or perhaps better, scale image drawn using graphics#drawimage(...) overload takes width , height parameters.

e.g.,

import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.malformedurlexception; import java.net.url;  import javax.imageio.imageio; import javax.swing.*;  public class mybackground extends jpanel {    private bufferedimage img;     public mybackground(bufferedimage img) {       this.img = img;    }     @override    protected void paintcomponent(graphics g) {       super.paintcomponent(g);       if (img != null) {          g.drawimage(img, 0, 0, getwidth(), getheight(), this);       }    }     private static void createandshowgui() {       string comfychair = "https://duke.kenai.com/comfychair/comfychairrad.png";       bufferedimage img;       try {          url url = new url(comfychair);          img = imageio.read(url);           mybackground mainpanel = new mybackground(img);           jframe frame = new jframe("mybackground");          frame.setdefaultcloseoperation(jframe.exit_on_close);          frame.getcontentpane().add(mainpanel);          frame.pack();          frame.setlocationbyplatform(true);          frame.setvisible(true);       } catch (malformedurlexception e) {          e.printstacktrace();       } catch (ioexception e) {          e.printstacktrace();       }    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

example 2:

import java.awt.graphics; import java.awt.graphics2d; import java.awt.point; import java.awt.renderinghints; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.point2d; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import java.util.arraylist; import java.util.list;  import javax.imageio.imageio; import javax.swing.*;  public class mybackground extends jpanel {    public static final double norm_const = 1.0;    private bufferedimage img;    private list<list<point2d>> normalizedpoints = new arraylist<list<point2d>>();    private list<point2d> pointsublist;     public mybackground(bufferedimage img) {       this.img = img;       mymouseadapter mymouseadapter = new mymouseadapter();       addmouselistener(mymouseadapter);       addmousemotionlistener(mymouseadapter);    }     @override    protected void paintcomponent(graphics g) {       super.paintcomponent(g);       if (img != null) {          g.drawimage(img, 0, 0, getwidth(), getheight(), this);       }       graphics2d g2 = (graphics2d) g;       g2.setrenderinghint(renderinghints.key_antialiasing,             renderinghints.value_antialias_on);        (list<point2d> pointlist : normalizedpoints) {          if (pointlist.size() > 1) {             (int = 1; < pointlist.size(); i++) {                point p1 = denormalize(pointlist.get(i - 1));                point p2 = denormalize(pointlist.get(i));                g2.drawline(p1.x, p1.y, p2.x, p2.y);             }          }       }        if (pointsublist != null && pointsublist.size() > 1) {          (int = 1; < pointsublist.size(); i++) {             point p1 = denormalize(pointsublist.get(i - 1));             point p2 = denormalize(pointsublist.get(i));             g2.drawline(p1.x, p1.y, p2.x, p2.y);          }       }    }     private point denormalize(point2d p2d) {       int x = (int) (p2d.getx() * getwidth() / norm_const);       int y = (int) (p2d.gety() * getheight() / norm_const);       return new point(x, y);    }     private class mymouseadapter extends mouseadapter {        @override       public void mousepressed(mouseevent e) {          point2d p = normalizepoint(e.getpoint());          pointsublist = new arraylist<>();          pointsublist.add(p);       }        @override       public void mousereleased(mouseevent e) {          point2d p = normalizepoint(e.getpoint());          pointsublist.add(p);          normalizedpoints.add(pointsublist);          pointsublist = null;          repaint();       }        @override       public void mousedragged(mouseevent e) {          point2d p = normalizepoint(e.getpoint());          pointsublist.add(p);          repaint();       }        private point2d normalizepoint(point point) {          double x = (norm_const * point.x) / getwidth();          double y = (norm_const * point.y) / getheight();          point2d result = new point2d.double(x, y);          return result;       }      }     private static void createandshowgui() {       string comfychair = "https://duke.kenai.com/comfychair/comfychairrad.png";       bufferedimage img;       try {          url url = new url(comfychair);          img = imageio.read(url);           mybackground mainpanel = new mybackground(img);           jframe frame = new jframe("mybackground");          frame.setdefaultcloseoperation(jframe.exit_on_close);          frame.getcontentpane().add(mainpanel);          frame.pack();          frame.setlocationbyplatform(true);          frame.setvisible(true);       } catch (malformedurlexception e) {          e.printstacktrace();       } catch (ioexception e) {          e.printstacktrace();       }    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -