java - How to deal with public void paint() method in JFrame -
java - How to deal with public void paint() method in JFrame -
my problem when utilize
public void paint(graphics g) {}
method draw string as
g.drawstring("hello java",0,0);
my total code is
import javax.swing.*; import java.awt.*; class test extends jframe { public void testing() { setsize(500,500); show(); setdefaultcloseoperation(jframe.exit_on_close); } public void paint(graphics g) { super.paint(g); g.drawstring("hello java"); } public static void main(string arg[]) { test t=new test(); t.testing(); } }
in jframe getting black screen without hello java beingness drawn please help me in advance
to display inherited frame correctly, paint method in inherited class should contain phone call of super.paint():
class myframe extends jframe { public void paint(graphics g) { super.paint(g); g.drawstring("hello java", 50, 50); } }
edit (painting in panel):
import java.awt.*; import javax.swing.*; public class custompaint { public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { jframe frame = new jframe("custompaint"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new mypanel()); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } }); } } class mypanel extends jpanel { public dimension getpreferredsize() { homecoming new dimension(320, 240); } public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; string msg = "hello java"; g2.setpaint(color.blue); int w = (getwidth() - g.getfontmetrics().stringwidth(msg)) / 2; g2.drawstring(msg, w, getheight() / 2); } }
java swing
Comments
Post a Comment