java - repaint() method not calling paintComponent() -
java - repaint() method not calling paintComponent() -
for programme want utilize open button open jfilechooser , select image , draw on jpanel on left side of applet, know file beingness retrieved when go repaint graphics context nil happens. in advance.
import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import javax.imageio.imageio; import javax.swing.jbutton; import javax.swing.jfilechooser; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities; import javax.swing.jframe; public class finalproject390 { public static void main(string[] args) { createandshowgui(); } private static void createandshowgui() { jframe f = new jframe("final project"); f.setsize(1025, 520); f.add(new graphicspanel()); f.add(new mainpanel()); f.setvisible(true); } } class mainpanel extends jpanel implements actionlistener { jbutton openbutton = new jbutton("open"); jbutton newbutton = new jbutton("new"); jbutton savebutton = new jbutton("save"); jbutton saveasbutton = new jbutton("save as"); jbutton drawlinebutton = new jbutton("draw line"); jbutton drawrectanglebutton = new jbutton("draw rectangle"); jbutton drawovalbutton = new jbutton("draw oval"); jbutton drawarcbutton = new jbutton("draw arc"); jbutton extractpixeldatabutton = new jbutton("extract pixel data"); jbutton convolutebutton = new jbutton("convolute"); jtextfield xposstartfield = new jtextfield("x-position start"); jtextfield yposstartfield = new jtextfield("y-position start"); jtextfield xposendfield = new jtextfield("x-position end"); jtextfield yposendfield = new jtextfield("y-position end"); jtextfield anglestartfield = new jtextfield("angle start"); jtextfield anglesizefield = new jtextfield("angle size"); public mainpanel() { openbutton.setbounds(660, 50, 100, 30); openbutton.addactionlistener(this); newbutton.setbounds(780, 50, 100, 30); savebutton.setbounds(900, 50, 100, 30); drawlinebutton.setbounds(660, 110, 160, 30); drawrectanglebutton.setbounds(840, 110, 160, 30); drawovalbutton.setbounds(660, 155, 160, 30); drawarcbutton.setbounds(840, 155, 160, 30); extractpixeldatabutton.setbounds(660, 220, 160, 30); convolutebutton.setbounds(840, 220, 160, 30); xposstartfield.setbounds(660, 280, 100, 30); yposstartfield.setbounds(660, 320, 100, 30); xposendfield.setbounds(660, 380, 100, 30); yposendfield.setbounds(660, 420, 100, 30); anglestartfield.setbounds(900, 280, 100, 30); anglesizefield.setbounds(900, 320, 100, 30); setlayout(null); setbackground(color.green); setbounds(641, 0, 370, 480); add(openbutton); add(newbutton); add(savebutton); add(drawlinebutton); add(drawrectanglebutton); add(drawovalbutton); add(drawarcbutton); add(extractpixeldatabutton); add(convolutebutton); add(xposstartfield); add(yposstartfield); add(xposendfield); add(yposendfield); add(anglestartfield); add(anglesizefield); } @override public void actionperformed(actionevent e) { javax.swing.jfilechooser filechooser = new jfilechooser(); bufferedimage bin = null, bi = null; graphicspanel gpanel = new graphicspanel(); filechooser.setcurrentdirectory(new file(system .getproperty("user.home"))); int result = filechooser.showopendialog(null); if (result == javax.swing.jfilechooser.approve_option) { file selectedfile = filechooser.getselectedfile(); system.out.println("selected file: " + selectedfile.getabsolutepath()); seek { bin = imageio.read(new file(selectedfile.getabsolutepath())); } grab (ioexception e1) { e1.printstacktrace(); } gpanel.setimg(bin); } } } class graphicspanel extends jpanel { bufferedimage bi = null, bin = null; public graphicspanel() { setlayout(null); setbackground(color.blue); setsize(640, 480); setlocation(0, 0); } public void setimg(bufferedimage b) { this.bi = b; repaint(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(bi, 0, 0, null); } }
this looks homework assignment, rather outright reply code, instead please consider following:
in methodcreateandshowgui() instantiate graphicspanel object , add together jframe in actionperformed() method, instantiate graphicspanel object (which never added jframe) , phone call setimage(). your image not display because graphicspanel command has been added jframe not same graphicspanel command set image on.
i hope plenty help prepare code.
java image swing graphics paintcomponent
Comments
Post a Comment