java - How to see if a something in Graphics2D is off the frame -
i have paint
method have draw rectangle , move across screen , want when rectangle goes off screen move start of jframe. assume if(rectangle.isoffthescreen){ put on screen }
don't know how that. also, want know if can rect jframe frame = new jframe();
, rectangle. i'm sorry if confusing.
@madprogrammer here @ far
public class main extends jpanel { public static int place = -350; public rectangle rect; public int xdelta; public main() { rect = new rectangle(0, 75, 50, 50); xdelta = 4; timer timer = new timer(40, new actionlistener() { @override public void actionperformed(actionevent e) { rect.x += xdelta; if (rect.x + rect.width > getwidth() - 1) { rect.x = getwidth() - rect.width; xdelta *= -1; } else if (rect.x < 0) { rect.x = 0; xdelta *= -1; } repaint(); } }); timer.start(); } public void paint(graphics g) { super.paint(g); graphics2d g2d = (graphics2d) g.create(); random r = new random(); int r1; r1 = r.nextint(5); if (r1 == 0) { g2d.setcolor(color.white); } else if (r1 == 1) { g2d.setcolor(color.blue); } else if (r1 == 2) { g2d.setcolor(color.red); } else if (r1 == 3) { g2d.setcolor(color.green); } else if (r1 == 4) { g2d.setcolor(color.pink); } else { g2d.setcolor(color.cyan); } place += 50; rect = new rectangle(place, 100, 300, 200); g2d.draw(rect); g2d.fill(rect); g2d.dispose(); try { thread.sleep(400); } catch (exception e) { } repaint(); } }
and
public class frame { public static jframe frame; public static void main(string args[]){ frame = new jframe(); frame.setsize(500, 500); frame.setdefaultcloseoperation(jframe.exit_on_close); //frame.setresizable(false); frame.setvisible(true); frame.setlocationrelativeto(null); main m = new main(); m.setbackground(color.black); frame.add(m); } }
i still can't work.
first of, should avoid overriding paint
of top level containers , instead use extends jcomponent
(like jpanel
) , override it's paintcomponent
.
there number of reasons, in case, frame contains decoration sits inside viewable area.
the basic process check edge cases...
if (box.x + box.width > getwidth() - 1) { // boxes right edge beyond right edge of it's container } else if (box.x < 0) { // boxes left edge beyond left edge of it's container }
this checks see if box
's right edge beyond containers right edge , if box
's left edge beyond left edge of container.
it simple process included vertical checking well.
updated example
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class move { public static void main(string[] args) { new move(); } public move() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { private rectangle box; private int xdelta; public testpane() { box = new rectangle(0, 75, 50, 50); xdelta = 4; timer timer = new timer(40, new actionlistener() { @override public void actionperformed(actionevent e) { box.x += xdelta; if (box.x + box.width > getwidth() - 1) { box.x = getwidth() - box.width; xdelta *= -1; } else if (box.x < 0) { box.x = 0; xdelta *= -1; } repaint(); } }); timer.start(); } @override public dimension getpreferredsize() { return new dimension(200, 200); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); g2d.setcolor(color.blue); g2d.fill(box); g2d.dispose(); } } }
update op's example code
immediately, number of things worry me...
- overriding
paint
- calling
thread.sleep
inpaint
method - calling
repaint
withinpaint
method - creating new
rectangle
inpaint
method - the reliance on
place
variable
check out performing custom painting more details painting in swing
you should never block or perform long running processing event dispatching thread, prevent edt processing (amongst other things) paint requests , new events. calling thread.sleep
in paint
method, preventing swing updating screen.
check out concurrency in swing more details.
calling repaint
(or method might call repaint
) within paint
method sure way consume cpu cycle.
you may want check out painting in awt , swing more details paint process in swing
by creating new rectangle
in paint
method, throwing away changes timer
has made, stopping rectangle moving effectively...
the place
method not required. animation illusion of change on time, hence use of timer
, xdelta
.
updated based on code example
import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.random; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class move { public static void main(string[] args) { new move(); } public move() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(new main()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public static class main extends jpanel { public static int place = -350; public rectangle rect; public int xdelta; public main() { rect = new rectangle(0, 75, 50, 50); xdelta = 4; timer timer = new timer(40, new actionlistener() { @override public void actionperformed(actionevent e) { rect.x += xdelta; if (rect.x + rect.width > getwidth() - 1) { rect.x = getwidth() - rect.width; xdelta *= -1; } else if (rect.x < 0) { rect.x = 0; xdelta *= -1; } repaint(); } }); timer.start(); } @override public dimension getpreferredsize() { return new dimension(200, 200); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2d = (graphics2d) g.create(); random r = new random(); int r1; r1 = r.nextint(5); if (r1 == 0) { g2d.setcolor(color.white); } else if (r1 == 1) { g2d.setcolor(color.blue); } else if (r1 == 2) { g2d.setcolor(color.red); } else if (r1 == 3) { g2d.setcolor(color.green); } else if (r1 == 4) { g2d.setcolor(color.pink); } else { g2d.setcolor(color.cyan); } // place += 50; // rect = new rectangle(place, 100, 300, 200); g2d.draw(rect); g2d.fill(rect); g2d.dispose(); } } }
Comments
Post a Comment