Java redrawing duplicate image -


so ive been doing bit of java , have run bit of problem. have been playing around 2d drawing , added image project.

the problem when window gets resized, redraws , duplicates image. have made little workaround, not ideal... why image duplicate?

before: http://i.imgur.com/pmhrzbq.png

(window resized)

after: http://i.imgur.com/bhsvvzz.png

code

main.java

import javax.swing.jframe;   public class main {     public static void main(string [] args) throws interruptedexception     {         jframe f = new jframe("title");         f.setdefaultcloseoperation(jframe.exit_on_close);         canvas testing = new canvas();          f.add(testing);         f.setsize(800, 600);         f.setvisible(true);     } } 

canvas.java

import java.awt.*; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.*;  public class canvas extends jpanel {     public void paintcomponent (graphics g)     {            super.paintcomponent(g);         this.setbackground(color.white);          g.setcolor(color.black);         g.fillrect(25, 25, 100, 30);          g.setcolor(new color(190,81,215));         g.fillrect(25, 68, 10, 10);          g.setcolor(color.red);         g.drawstring("matt da best", 100, 10);               try             {                 bufferedimage image = imageio.read(new file("c:/face.png"));                 jlabel piclabel = new jlabel(new imageicon(image));                 system.out.println("added pic");                 add(piclabel);             }             catch (ioexception e)             {                 e.printstacktrace();             }            }  } 

  1. don't load image in paintcomponent(graphics) method! declared class attribute , loaded in constructor.
  2. don't add components in paintcomponent method either! trigger repaint.

this should work more reliably..

import java.awt.*; import java.awt.image.bufferedimage; import javax.swing.*;  public class main {     public static void main(string [] args) throws interruptedexception     {         jframe f = new jframe("title");         f.setdefaultcloseoperation(jframe.exit_on_close);         canvas testing = new canvas();          f.add(testing);         f.setsize(800, 600);         f.setvisible(true);     } }  class canvas extends jpanel {      bufferedimage image;      canvas() {         image = new bufferedimage(200,200,bufferedimage.type_int_rgb);         jlabel piclabel = new jlabel(new imageicon(image));         system.out.println("added pic");         add(piclabel);     }      public void paintcomponent (graphics g)     {         super.paintcomponent(g);         this.setbackground(color.white);          g.setcolor(color.black);         g.fillrect(25, 25, 100, 30);          g.setcolor(new color(190,81,215));         g.fillrect(25, 68, 10, 10);          g.setcolor(color.red);         g.drawstring("matt da best", 100, 10);     } } 

other tips

  • have canvas return preferred size , pack() frame rather set size it.
  • start , update swing gui on edt.
  • don't give custom class same name j2se class. can confusing. maybe call jcanvas.

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -