java - Why is rectangle not rendering to screen -
public class rec extends jframe { public rec (){ jframe jframe = new jframe(); jframe.setsize(500, 500); jframe.setvisible(true); } public void render (graphics g){ g.setcolor(color.red); g.fillrect(0,0,50,50); } public static void main(string[] args) { rec frame = new rec(); frame.render(g); } }
why not work? aware may need paintcomponent
, if how go doing this? great, thank you!
the thing painting in jframe not should doing. better (in instance) set contentpane jpanel
, , paint inside jpanel
.
take short snippet example:
import java.awt.*; import javax.swing.*; public class rec { public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { jframe rec = new jframe(); rec.setsize(50, 150); rec.setcontentpane(new jpanel() { @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.red); g.fillrect(0, 0, 50, 50); } }); rec.setvisible(true); } }); } }
result:
Comments
Post a Comment