java - graphics drawxxx method does not work -
i new graphics. got code open source.it should paint string "heeelo" on jframe,but not.can explain why not working , principle of paint method?why should edit jframe method not called main?
import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.event.keyadapter; import javax.swing.jframe; public class view extends jframe{ public view(){ this.setsize(new dimension(250, 250)); this.setvisible(true); this.setdefaultcloseoperation(jframe.exit_on_close); } public void paint(graphics g){ g.drawstring("heello", 10, 10); } public static void main(string []args){ new view(); } }
get rid of tutorial, not way painting.
first basic problems should invoke super.paint(...) when overriding method. secondly text won't show because being painted under title bar. need increase y offset:
super.paint(g); g.drawstring("heello", 10, 40);
and principle of paint method
read section swing tutorial on custom painting proper way this. override paintcomponent()
method of jpanel , add panel frame. should not custom painting on frame directly.
Comments
Post a Comment