swing - JAVA - Help drawing on an extending canvas -
swing - JAVA - Help drawing on an extending canvas -
so, have class called mainclass in extends canvas. trying let's draw filled rectangle on canvas without overriding paint method. there way or must override paint method , set want draw there?
import java.awt.canvas; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jframe; public class mainclass extends canvas { mainclass() { jframe mainwindow = new jframe("main window"); mainwindow.setvisible(true); mainwindow.setsize(500, 500); mainwindow.setdefaultcloseoperation(jframe.exit_on_close); mainwindow.add(this); } public void paint(graphics g) { super.paint(g); } public static void main(string[] args) { mainclass temp = new mainclass(); graphics g = (graphics2d)temp.getgraphics(); g.setcolor(color.red); g.fillrect(0, 0, 400, 400); temp.repaint(); } } the thought have class , can graphic object of canvas , draw on straight , repaint. or maybe thinking of passing shapes , objects method painting @ position me.
don't utilize canvas. instead extend jpanel and override jpanel's paintcomponent method, not paint method (and why not override methods? objection painting within of them?). google java swing painting tutorials , go through them. here's link. by calling getgraphics() on component, obtain short-lived graphics context might work right @ time, fail work if repaints occur (and these not under control), leading either programme graphics failure or nullpointerexception. should avoid doing unless have dire need , know you're doing. "know you're doing", read book filthy rich clients haase , guy. note draw straight on bufferedimage using graphics object derived image, still should draw bufferedimage in paintcomponent(...) method override.
java swing canvas paint extends
Comments
Post a Comment