java - Drawing on an Image -


so have program paint made in java , can draw few colours. default background of program white, i'd try load image , able draw on top of image. can load image fro reason wont show lines when try drawing on it. here code.

import java.awt.*; import java.awt.event.*; import java.io.file; import java.io.ioexception;  import javax.imageio.imageio; import javax.swing.*;  public class paint {  public static void main(string[] args) {     paintwindow frame = new paintwindow();     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setresizable(false);     frame.setvisible(true); } }  class paintwindow extends jframe {   public paintwindow() {     settitle("javshot edit");     setsize(668, 600);     setlocationrelativeto(null);      panel = new jpanel();     drawpad = new paddraw();      panel.setpreferredsize(new dimension(75, 68));      //creates new container     container content = this.getcontentpane();     content.setlayout(new borderlayout());      //sets panel left, paddraw in center     content.add(panel, borderlayout.west);     content.add(drawpad, borderlayout.center);      //add color buttons:     makecolorbutton(color.blue);     makecolorbutton(color.magenta);     makecolorbutton(color.red);     makecolorbutton(color.green);     makecolorbutton(color.black);     makecolorbutton(color.white);      //creates clear button     jbutton clearbutton = new jbutton("clear");      clearbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             drawpad.clear();         }     });      panel.add(clearbutton); }  /*  * makes button changes color  * @param color color used button  */ public void makecolorbutton(final color color) {     jbutton tempbutton = new jbutton();     tempbutton.setbackground(color);     tempbutton.setpreferredsize(new dimension(16, 16));     panel.add(tempbutton);     tempbutton.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             drawpad.changecolor(color);         }     }); }  private jpanel panel; private paddraw drawpad; }  class paddraw extends jcomponent {  //this gonna image draw on image image; //this we'll using draw on graphics2d graphics2d; //these gonna hold our mouse coordinates int currentx, currenty, oldx, oldy;  public paddraw() {     setdoublebuffered(false);     addmouselistener(new mouseadapter() {         //if mouse pressed sets oldx & oldy         //coordinates mouses x & y coordinates         public void mousepressed(mouseevent e) {             oldx = e.getx();             oldy = e.gety();         }     });      addmousemotionlistener(new mousemotionadapter() {         //while mouse dragged sets currentx & currenty mouses x , y         //then draws line @ coordinates         //it repaints , sets oldx , oldy currentx , currenty         public void mousedragged(mouseevent e) {             currentx = e.getx();             currenty = e.gety();              graphics2d.drawline(oldx, oldy, currentx, currenty);             graphics2d.drawline(oldx + 1, oldy + 1, currentx + 1, currenty + 1);             repaint();              oldx = currentx;             oldy = currenty;         }     }); }  //this painting bit //if has nothing on //it creates image size of window //sets value of graphics image //sets rendering //runs clear() method //then draws image public void paintcomponent(graphics g) {      try {         image = imageio.read(new file("c:\\users\\user\\desktop\\untitled.png"));         graphics2d = (graphics2d)image.getgraphics();         graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);     } catch (ioexception e) { }      if(image == null) {          image = createimage(getsize().width, getsize().height);         graphics2d = (graphics2d)image.getgraphics();         graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);          clear();     }       g.drawimage(image, 0, 0, null); }  //this clear //it sets colors white //then fills window white //thin sets color black public void clear() {     graphics2d.setpaint(color.white);     graphics2d.fillrect(0, 0, getsize().width, getsize().height);     graphics2d.setpaint(color.black);     repaint(); }  public void changecolor(color thecolor) {     graphics2d.setpaint(thecolor);     repaint(); } }  

i load image here:

image = imageio.read(new file("c:\\users\\user\\desktop\\untitled.png"));         graphics2d = (graphics2d)image.getgraphics();         graphics2d.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);     } catch (ioexception e) { } 

does know problem is?

painting destructive process. is, each time paintcomponent called, expected repaint ever need displayed screen, entirely scratch.

there (at least) 2 immediate issues approach

  1. you not calling super.paintcomponent, effect way in paint process updates screen, important have extended jcomponent transparent default , may compromise ability framework function correctly (leaving nasty paint artifacts on place) - remember, graphics shared resource. other components painted share resource, meaning end ever painted before being left on screen.
  2. you re-loading image every time paintcomponent called. means, ever painted graphics2d going lost.

i wouldn't bother setdoublebuffered(false) effect way in component updated , produce undesirable results.

i add each point want draw list of kind , paint list within paintcomponent method.

don't load resources within paintxxx method. these should prepared before hand.

i can see "trying" perform kind of double buffering, isn't how should done. going little other problems. start simple solution first.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -