Adding an image to an exsisting JPanel inside of a JFrame -


so, i'm in ap computer science class, , our final project make program display bunch of different concepts have learned. 2 of displaying images, , adding buttons.

i decided make cheesy decision-based rpg shows if-else branching. figured out how menu in has start button, , opens input dialog box. can't figure out how add image same jframe button located on. know displays image either above or below button. learned how display images, examples extended classes display image. can't figure out how invoke sort of draw or bufferedimage method within existing code or put it. maybe make call different class has image code in it? here have far.

public class smashing extends jpanel {      public static void main(string[] args)     {      jframe frame = new jframe("input dialog box frame");     jbutton button = new jbutton("start nigel's adventure");           button.addactionlistener(new actionlistener()     {       public void actionperformed(actionevent ae)       {         string str = joptionpane.showinputdialog( "what should nigel do? :  enter cardinal direction ex. n");         if (str.equals("n"))         {           joptionpane.showmessagedialog(null, "nigel comes upon tree ");           string str2 = joptionpane.showinputdialog( "what should nigel do? :");           if (str2.equals("climb"))           joptionpane.showmessagedialog(null, "nigel climbs tree ");           if (str2.equals("s"))           joptionpane.showmessagedialog(null, "nigel returns strating position ");         }           if (str.equals("s"))         {             joptionpane.showmessagedialog(null, "nigel comes upon boulder ");             string str3 = joptionpane.showinputdialog( "what should nigel do? :");         }            }       });     jpanel panel = new jpanel();     panel.add(button);     frame.add(panel);     frame.setsize(400, 400);     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setvisible(true);   } } 

and imported bunch of packages already, didn't include in code, oh , regular application class, not applet. appreciated.

you can use jlabel store image read file or buffer:

imageicon image = new imageicon("path_to_image"); //or imageicon image = new imageicon (data) //where data byte[] data 

then create jlabel :

jlabel label = new jlabel(image)  

to decide how position image add jlabel jpanel desired layoutmanager.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -