Thursday, 15 August 2013

java - Graphics wont clear properly -



java - Graphics wont clear properly -

i having problems, want square (fly) drawn , redrawn show movement. fine in code when button pressed fly "move", old squares not delete. have tried enviromentpanel.repaint() updateui() , removeall() , cannot work, if utilize of them none of shapes appear , blank screen.

import java.util.random; public class fly implements runnable{ private int xposition; private int yposition; private boolean eaten; public fly(){ random randomgenerator = new random(); xposition = randomgenerator.nextint(690) + 10; yposition = randomgenerator.nextint(690) + 10; eaten = false; } public int getxposition() { homecoming xposition; } public void setxposition(int xposition) { this.xposition = xposition; } public int getyposition() { homecoming yposition; } public void setyposition(int yposition) { this.yposition = yposition; } public boolean iseaten() { homecoming eaten; } public void seteaten(boolean eaten) { this.eaten = eaten; } public void move(){ random randomgenerator = new random(); int xchange = -10 + randomgenerator.nextint(20); int ychange = -10 + randomgenerator.nextint(20); xposition = xposition + xchange; yposition = yposition + ychange; seek { thread.sleep(1000); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } move(); } @override public string tostring() { homecoming "fly [xposition=" + xposition + ", yposition=" + yposition + ", eaten=" + eaten + "]"; } @override public void run() { move(); } } import java.awt.color; import java.awt.flowlayout; import java.awt.graphics; import java.awt.panel; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.arraylist; import javax.swing.*; import java.awt.*; import javax.imageio.imageio; public class enviroment2 implements runnable,actionlistener{ private jframe frame; private jpanel enviromentpanel,totalgui,enviromentbuttonpanel; private jbutton newfrogbutton, resetbutton, hungrybutton; private jtextfield entername; private jlabel hungrylabel; private arraylist<frog> frogs; private arraylist<fly> flys; public enviroment2(){ totalgui = new jpanel(); flys = new arraylist<fly>(); frogs = new arraylist<frog>(); enviromentpanel = new jpanel(); enviromentbuttonpanel = new jpanel(); newfrogbutton = new jbutton("new frog"); entername = new jtextfield("enter name"); hungrybutton = new jbutton("hungry!"); resetbutton = new jbutton("reset"); frame = new jframe("[=] hungry cyber pet [=]"); jframe.setdefaultlookandfeeldecorated(true); frame.setcontentpane(runenviroment()); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(740, 800); frame.setvisible(true); } public jpanel runenviroment(){ totalgui.setlayout(null); enviromentpanel.setlayout(null); enviromentpanel.setlocation(10, 10); enviromentpanel.setsize(700, 700); enviromentpanel.setbackground(color.white); totalgui.add(enviromentpanel); flowlayout experimentlayout = new flowlayout(); enviromentbuttonpanel.setlayout(experimentlayout); enviromentbuttonpanel.setlocation(10, 710); enviromentbuttonpanel.setsize(700, 50); totalgui.add(enviromentbuttonpanel); newfrogbutton.setlocation(0, 0); newfrogbutton.setsize(120, 30); newfrogbutton.addactionlistener(this); enviromentbuttonpanel.add(newfrogbutton); entername.setlocation(140,0); entername.setsize(120,30); enviromentbuttonpanel.add(entername); hungrybutton.setlocation(280, 0); hungrybutton.setsize(120, 30); hungrybutton.addactionlistener(this); enviromentbuttonpanel.add(hungrybutton); resetbutton.setlocation(420, 0); resetbutton.setsize(120, 30); resetbutton.addactionlistener(this); enviromentbuttonpanel.add(resetbutton); totalgui.setopaque(true); homecoming totalgui; } public void draw(){ graphics paper = enviromentpanel.getgraphics(); (int = 0; <= flys.size()-1; i++){ system.out.println("hi"); paper.setcolor(color.black); paper.fillrect(flys.get(i).getxposition(), flys.get(i).getyposition(), 10, 10); } seek { thread.sleep(1000); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } draw(); } public void actionperformed(actionevent e) { if(e.getsource() == newfrogbutton){ frog frog = new frog(entername.gettext()); frogs.add(frog); fly fly = new fly(); thread t = new thread(fly); t.start(); flys.add(fly); showflys(); } else if(e.getsource() == hungrybutton){ } else if(e.getsource() == resetbutton){ frogs.clear(); flys.clear(); system.out.println(frogs); system.out.println(flys); } } public void showflys(){ (int = 0; <= flys.size()-1; i++){ system.out.println(flys.get(i)); } } @override public void run() { draw(); } }

this graphics paper = enviromentpanel.getgraphics() start of problems, not how custom painting done.

getgraphics returns graphics context used in lastly paint cycle, @ best it's snap shot, @ worst can null.

you should never maintain reference graphics context didn't create. can alter , painting on out of turn can produce unexpected results.

instead, should override paintcomponent method (probably in environmentpanel) , custom painting in it.

your sec problem violating thread rules of swing - should never create or modify ui component in thread other edt

you might take read through

performing custom painting painting in awt , swing concurrency in swing

java swing graphics jpanel

No comments:

Post a Comment