Adding Canvas3D image to a JPanel or JFrame

My team has developed a 3D game board and we want to add it to a JPanel. The test code below works fine but we want to add this to a JPanel. Can you put a Canvas3D in a JPanel?
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.util.Enumeration;
import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
public class ProxyBoard extends Applet
     public class SimpleBehave extends Behavior
          private TransformGroup targetTG;
          private Transform3D rotation = new Transform3D();
          private double angle = 0.0;
          SimpleBehave(TransformGroup targetTG)
               this.targetTG = targetTG;
          public void initialize()
               this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
          public void processStimulus(Enumeration criteria)
               angle +=0.05;
               rotation.rotX(angle);
               targetTG.setTransform(rotation);
               this.wakeupOn(new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED));
     public ProxyBoard()
          setLayout(new BorderLayout());
          Canvas3D canvas3D = new Canvas3D(null);
          add("Center", canvas3D);
          SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
          simpleU.getViewingPlatform().setNominalViewingTransform();
          BranchGroup scene = createSceneGraph(simpleU);
          scene.compile();
          simpleU.addBranchGraph(scene);
     public BranchGroup createSceneGraph(SimpleUniverse su)
          BranchGroup boardBG = new BranchGroup();
          TransformGroup vpTrans = null;
          BoundingSphere mouseBounds = null;
          vpTrans = su.getViewingPlatform().getViewPlatformTransform();
          mouseBounds = new BoundingSphere(new Point3d(), 1000.0);
          KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans);
          keyNavBeh.setSchedulingBounds(mouseBounds);
          boardBG.addChild(keyNavBeh);
          MouseRotate myMouseRotate = new MouseRotate(MouseBehavior.INVERT_INPUT);
          myMouseRotate.setTransformGroup(vpTrans);
          myMouseRotate.setSchedulingBounds(mouseBounds);
          boardBG.addChild(myMouseRotate);
          MouseTranslate myMouseTranslate = new MouseTranslate(MouseBehavior.INVERT_INPUT);
          myMouseTranslate.setTransformGroup(vpTrans);
          myMouseTranslate.setSchedulingBounds(mouseBounds);
          boardBG.addChild(myMouseTranslate);
          MouseZoom myMouseZoom = new MouseZoom(MouseBehavior.INVERT_INPUT);
          myMouseZoom.setTransformGroup(vpTrans);
          myMouseZoom.setSchedulingBounds(mouseBounds);
          boardBG.addChild(myMouseZoom);
          Board board = new Board();
          Transform3D pegPositions[] = new Transform3D[8];
          TransformGroup pegPositionsTG[] = new TransformGroup[8];
          Pegs pegs[] = new Pegs[8];
          Transform3D translate = new Transform3D();
          translate.set(new Vector3f(0.0f, -1.0f, -5.0f));
          TransformGroup boardTGT1 = new TransformGroup(translate);
          TransformGroup boardTGR1 = new TransformGroup();
          boardTGR1.setCapability(boardTGR1.ALLOW_TRANSFORM_WRITE);
          for(int i = 0; i < 8; i++)
               pegs[i] = new Pegs();
               pegPositions[i] = new Transform3D();
               pegPositions.set(new Vector3f(-2.5f, 0, (float)(-i/4.0)));
               pegPositionsTG[i] = new TransformGroup(pegPositions[i]);
               pegPositionsTG[i].addChild(pegs[i].getTransformGroup());
               boardTGT1.addChild(pegPositionsTG[i]);               
          boardTGR1.addChild(board.getBoard());
          SimpleBehave myRotate = new SimpleBehave(boardTGR1);
          myRotate.setSchedulingBounds(new BoundingSphere());
          boardBG.addChild(myRotate);
          boardTGT1.addChild(boardTGR1);
          boardBG.addChild(boardTGT1);                    
          boardBG.compile();
          return boardBG;
     public static void main(String[] args)
          Frame frame = new MainFrame(new ProxyBoard(), 800, 600);
/*-------------Main Source Container------------------*/
public class Proxy extends JFrame
     implements MouseMotionListener
     private JDesktopPane myDesktop;
     private JPanel panel;
     private JLabel statusBar, position;
     private JSlider zSlide, zSlide1;
     private ImageIcon test;
     private int i;
     public Proxy()
          super("Proxy Board Prototype 1.2.2");
          i=0;
          statusBar = new JLabel();
          getContentPane();
          myDesktop = new JDesktopPane();
          getContentPane().add(myDesktop);
public static void main(String args[])
          Proxy app = new Proxy();
          //new MainFrame( new Proxy(), 800, 600 );
//          app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
/*-------------JPanel I want to hold to the 3D Graphics--------*/
class Session extends JPanel
     implements MouseMotionListener
     private ImageIcon test;
     private JLabel position;
     private JSlider zSlide;
     private JPanel panel, gPanel;
     static int openFrameCount = 0;
     public Session()
          //super("", true, true, true, true);
          setLayout(new FlowLayout());
          openFrameCount++;
     //setTitle("Untitled Message " + openFrameCount);
ProxyBoard pb = new ProxyBoard();//<-Graphic Class
gPanel = new JPanel();
gPanel.add(pb);//<-I want the 3D Graphic here
          test = new ImageIcon("Layout.jpg");
          JLabel pic = new JLabel(test);
          addMouseMotionListener( this );
          zSlide = new JSlider(SwingConstants.VERTICAL, 600, 10);
          zSlide.setMajorTickSpacing(25);
          zSlide.setPaintTicks(true);
          zSlide.setToolTipText("Zoom");
          panel.add(zSlide, BorderLayout.NORTH);
          panel.add(gPanel, BorderLayout.CENTER);//<-Graphic Here
          panel.add(position, BorderLayout.SOUTH);
          add(panel);
          //pack();
          setSize(200,200);
          setVisible (true);

Yes you can. Just use the add method of JPanel. However note this
http://www.j3d.org/faq/swing.html (specially read this: http://java.sun.com/products/jfc/tsc/articles/mixing/)
when mixing Lightweight (JPanel) and Heavyweight (Canvas3D) components.

Similar Messages

  • Adding an image to a JPanel

    Can some one tell me how to place an image file (.gif, .jpeg etc) to a JPanel when the user selects it from a JFileChooser???
    Please help me...it is getting urgent and this stoopid book doesn't help me growls
    Something to do with ImageIcon and a paintComponent method??? shrugs

    reason I posted a new msg wasn't cos I didn't get a reply...more that what I asked for was not really what I wanted (wasn't specific enough).
    When I looked, my old msg wasn't on the main screen so I thought it would be ok to made a new one.
    But I'm sorry, I won't do it again :)

  • Adding background image to JPanal...............PLEASE PLEASE HELP

    Hi,
    i am very very stuck in adding a image to a JPanel. here is my code could any one please show me where and how to add an image to this please. a sample code would be prefered.
    Thanks
    * card.java
    public class card extends javax.swing.JFrame {
    /** Creates new form card */
    public card() {
    initComponents();
    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    private void initComponents() {
    java.awt.GridBagConstraints gridBagConstraints;
    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jPanel2 = new javax.swing.JPanel();
    getContentPane().setLayout(new java.awt.GridBagLayout());
    setTitle("Card Game");
    addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent evt) {
    exitForm(evt);
    jPanel1.setLayout(new java.awt.GridBagLayout());
    jPanel1.setBackground(new java.awt.Color(255, 255, 255));
    jLabel1.setIcon(new javax.swing.ImageIcon("card game implementation\\headingF1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    gridBagConstraints.insets = new java.awt.Insets(5, 145, 0, 205);
    jPanel1.add(jLabel1, gridBagConstraints);
    jButton1.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\helpbuttonF.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = -36;
    gridBagConstraints.ipady = -10;
    gridBagConstraints.insets = new java.awt.Insets(83, 40, 130, 40);
    jPanel1.add(jButton1, gridBagConstraints);
    jButton2.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\newgame1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.ipadx = -39;
    gridBagConstraints.ipady = -12;
    gridBagConstraints.insets = new java.awt.Insets(213, 40, 0, 40);
    jPanel1.add(jButton2, gridBagConstraints);
    jButton3.setIcon(new javax.swing.ImageIcon("card game implementation\\Nigat\\quit1.jpg"));
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    gridBagConstraints.ipadx = -39;
    gridBagConstraints.ipady = -12;
    gridBagConstraints.insets = new java.awt.Insets(80, 40, 70, 40);
    jPanel1.add(jButton3, gridBagConstraints);
    jPanel2.setLayout(new java.awt.GridBagLayout());
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridheight = 2;
    gridBagConstraints.ipadx = 420;
    gridBagConstraints.ipady = 400;
    gridBagConstraints.insets = new java.awt.Insets(43, 60, 40, 0);
    jPanel1.add(jPanel2, gridBagConstraints);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 0;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 1);
    getContentPane().add(jPanel1, gridBagConstraints);
    pack();
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
    System.exit(0);
    * @param args the command line arguments
    public static void main(String args[]) {
    new card().show();
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration

    My images loaded up just fine. I left them in and commented out. Try using the single forward slash below...
    public class CardTest extends javax.swing.JFrame {
        /** Creates new form card */
        public CardTest() {
            initComponents();
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
        private void initComponents() {
            java.awt.GridBagConstraints gridBagConstraints;
            jPanel1 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            getContentPane().setLayout(new java.awt.GridBagLayout());
            setTitle("Card Game");
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            jPanel1.setLayout(new java.awt.GridBagLayout());
            jPanel1.setBackground(new java.awt.Color(255, 255, 255));
            jLabel1.setIcon(new javax.swing.ImageIcon(
                            //"images/T1.gif"));
                            "card game implementation/headingF1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.gridwidth = 2;
            gridBagConstraints.insets = new java.awt.Insets(5, 145, 0, 205);
            jPanel1.add(jLabel1, gridBagConstraints);
            jButton1.setIcon(new javax.swing.ImageIcon(
                             //"images/T2.gif"));
                             "card game implementation/Nigat/helpbuttonF.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.ipadx = -36;
            gridBagConstraints.ipady = -10;
            gridBagConstraints.insets = new java.awt.Insets(83, 40, 130, 40);
            jPanel1.add(jButton1, gridBagConstraints);
            jButton2.setIcon(new javax.swing.ImageIcon(
                             //"images/T3.gif"));
                             "card game implementation/Nigat/newgame1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.ipadx = -39;
            gridBagConstraints.ipady = -12;
            gridBagConstraints.insets = new java.awt.Insets(213, 40, 0, 40);
            jPanel1.add(jButton2, gridBagConstraints);
            jButton3.setIcon(new javax.swing.ImageIcon(
                             //"images/T4.gif"));
                             "card game implementation/Nigat/quit1.jpg"));
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 1;
            gridBagConstraints.gridy = 2;
            gridBagConstraints.ipadx = -39;
            gridBagConstraints.ipady = -12;
            gridBagConstraints.insets = new java.awt.Insets(80, 40, 70, 40);
            jPanel1.add(jButton3, gridBagConstraints);
            jPanel2.setLayout(new java.awt.GridBagLayout());
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 1;
            gridBagConstraints.gridheight = 2;
            gridBagConstraints.ipadx = 420;
            gridBagConstraints.ipady = 400;
            gridBagConstraints.insets = new java.awt.Insets(43, 60, 40, 0);
            jPanel1.add(jPanel2, gridBagConstraints);
            gridBagConstraints = new java.awt.GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = 0;
            gridBagConstraints.insets = new java.awt.Insets(0, 0, 1, 1);
            getContentPane().add(jPanel1, gridBagConstraints);
            pack();
        /** Exit the Application */
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
         * @param args the command line arguments
        public static void main(String args[]) {
            new CardTest().setVisible(true);
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        // End of variables declaration
    }

  • Help! Read raw Image data from a file and display on the JPanel or JFrame.

    PLEASE HELP, I want to Read Binary(Raw Image)data (16 bit integer) from a file and display on the JPanel or JFrame.

    Hey,
    I need to do the same thing. Did you find a way to do that?
    Could you sent me the code?
    It's urgent, please.
    My e-mail is [email protected]

  • How do you set an image into the background of a JPanel or JFrame?

    How do you set an image into the background of a JPanel or JFrame?

    Something like this, Ive thrown in an ImageIcon on a
    button too for good measure.
    import java.awt.*;
    import javax.swing.*;
    public class JFrameImage extends JFrame {
    public JFrameImage() {
    Container c    = getContentPane();
    JPanel panel = new JPanel(){
                 public void paintComponent(Graphics g)     {
    ImageIcon img = new
    = new ImageIcon("background.jpg");
                      g.drawImage(img.getImage(), 0, 0, null);
                      super.paintComponent(g);
            panel.setOpaque(false);
    ImageIcon icon = new ImageIcon("onButton.jpg");
    JButton button = new JButton(icon);
    panel.add(button);
    c.add(panel);
    public static void main(String[] args) {
    JFrameImage frame = new JFrameImage();
    frame.setSize(200,200);
    frame.setVisible(true);
    Going totally fancy pants
    ImageIcon bigImage = new ImageIcon(bgImage.getImage().getScaledInstance(getWidth(), getHeight(),Image.SCALE_REPLICATE));
    g.drawImage(bigImage.getImage(), 0, 0, this); Will scale the image to the size of the panel
    whereas
    for (int y = 0; y  < getHeight(); y = y + image.getHeight(null))
    for (int x = 0; x< getWidth(); x = x + image.getWidth(null))
    g.drawImage(image, x, y, this); Will give a tiled effect
    Try tiling with an animated gif and bring your processor to a standstill.

  • Trouble using images on a jpanel

    Hi!
    First off, I'm new to java so don't shoot me for asking a probably dumb question! I've been searching for the past 2 hrs on how to add an image to my jpanel.....problem is I'm completely confussed! I've found how to add one to an applet, but then to be told adding to an application is different...i thought i'd figured it all out but it's still not working.
    I've declared the image....and tried adding it to a label....but it didnt work.
    I'm sooo confused and getting frustated, somebody please help!
    Thanks,
    JenanaBanana

    Hi,
    You should try searching over the forum before posting....
    Here is a link to a similar post in this forum...
    http://forum.java.sun.com/thread.jspa?threadID=550457&messageID=2688013
    And here is some clumsy code from myside. It works beautifully though:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    public class ImagePanel {
    //Adding an image to a frame
    private static void addimage(JFrame aframe){
              //Create ImageIcon
              ImageIcon aimage = new ImageIcon("C:\\myimage.gif");
              //Create JPanel
              JPanel apanel = new JPanel(new FlowLayout());
              //Add mage to JPanel... you will need to do it through a JLabel to achieve this
              apanel.add(new JLabel(aimage));
              //Add the panel istself to the Frame
              aframe.getContentPane().add(apanel, BorderLayout.CENTER);
    //showpanel method
    private static void showpanel(){
              try{
                   //I like Windows Look And Feel
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
                   //Create a frame and set the Close operation.
                   JFrame myframe = new JFrame("Image on Panel");
                   myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   //add the image component to the frame
                   addimage(myframe);
                   //display
                   myframe.pack();
                   myframe.setVisible(true);
              catch(Exception e){
                   System.out.println("Application Error. Printing Stack Trace: ");
                   e.printStackTrace();
                   System.exit(-1);
    //Main Class
         public static void main(String[] args) {
              //Create Runnable mythread
              Runnable mythread = new Runnable() {
                   public void run() {
                        showpanel();
              //run it
              SwingUtilities.invokeLater(mythread);
    Let me know if any of this helps...
    Thanks,
    -Vamsi

  • Problem with JPanel in JFrame

    hai ashrivastava..
    thank u for sending this one..now i got some more problems with that screen .. actually i am added one JPanel to JFrame with BorderLayout at south..the problem is when i am drawing diagram..the part of diagram bellow JPanel is now not visible...and one more problem is ,after adding 6 ro 7 buttons remaing buttons are not vissible..how to increase the size of that JPanel...to add that JPanel i used bellow code
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    f.getContentPane().add(BorderLayout.SOUTH, panel);

    Hi
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    // Add this line to ur code with ur requiredWidth and requiredHeight
    panel.setPreferredSize(new Dimension(requiredWidth,requiredHeight));
    f.getContentPane().add(BorderLayout.SOUTH, panel);
    This should solve ur problem
    Ashish

  • Animated GIF Image on a JPanel.

    How can I display an animated GIF image on a JPanel? It should animate after displaying.
    Regards

    I think this code should display an animated GIF image on a JPanel.
    -Mani
    import javax.swing.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.*;
    public class Animation {
    public static void main(String args[]) {
    JLabel imageLabel = new JLabel();
    JLabel headerLabel = new JLabel();
    JFrame frame = new JFrame("JFrame Animation");
    JPanel jPanel = new JPanel();
    //Add a window listner for close button
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    // add the header label
    headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16));
    headerLabel.setText("Animated Image!");
    jPanel.add(headerLabel, java.awt.BorderLayout.NORTH);
    //frame.getContentPane().add(headerLabel, java.awt.BorderLayout.NORTH);
    // add the image label
    ImageIcon ii = new ImageIcon("d:/dog.gif");
    imageLabel.setIcon(ii);
    jPanel.add(imageLabel, BorderLayout.CENTER);
    frame.getContentPane().add(jPanel, java.awt.BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
    }

  • How to add an image in a JPanel

    Hi All,
    How to add an image in a JPanel and make it display.
    Thanks,

    I have tried with the below code. If I there is any fault please correct me.
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class HomePage extends JFrame implements     ActionListener {
        JButton cmdClick;
        JLabel label;
        JPanel homePanel = new JPanel();
        JPanel headPanel = new JPanel();
        JPanel btPanel = new JPanel();
        private JPanel mainPanel = new JPanel(new CardLayout());
        CardLayout cl;
        CalScenario calcFrame = null;
        public HomePage() {
           setTitle("Test Kit");
           setSize( 1008,399);
           setBackground( Color.gray );
           setResizable(false);
           Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
           Rectangle window = getBounds();
           setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
           setVisible(true);
            homePanel.setLayout(new BorderLayout());
            headPanel.setPreferredSize(new Dimension(1008,153));
            label = new JLabel("Main menu");
            headPanel.add(label);
            headPanel.setBackground(Color.CYAN);
            ImageIcon icon = new ImageIcon("images/slash.gif");
            JLabel imglabel = new JLabel();
            imglabel.setIcon(icon);
            headPanel.add(label);
            this.getContentPane().add(headPanel);
            btPanel.setBackground(Color.ORANGE);
            cmdClick = new JButton("Click here");
            btPanel.add(cmdClick);
            cmdClick.addActionListener(this);
            homePanel.add("North",headPanel);
            homePanel.add("West",btPanel);
            calcFrame = new CalScenario(mainPanel);
            mainPanel.add(homePanel, "HomePanel");
            mainPanel.add(calcFrame, "CalcFrame");
            cl = (CardLayout) (mainPanel.getLayout());
            add(mainPanel);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        public void actionPerformed(ActionEvent source)  {
          if (source.getSource() == (JButton) cmdClick) {
                cl.show(mainPanel, "CalcFrame");
        public static void main( String args[]) {
             HomePage homeFrame = new HomePage();
             homeFrame.setVisible(true);
    }

  • Embedding PDF File into JPanel or JFrame

    Does anyone know how to embedd a PDF file or document into a JPanel or JFrame.

    I can suggest you ways
    1)pdf to Image
    2)pdf to html
    http://www.verypdf.com/pdf2htm/
    Free online service
    http://www.gohtm.com/

  • Beginners Questions about Multiple JPanels in JFrame and event handling

    I am a newbie with SWING, and even a newerbie in Event Handling. So here goes.
    I am writing a maze program. I am placing a maze JPanel (MazePanel) at the center of a JFrame, and a JPanel of buttons (ButtonPanel) on the SOUTH pane. I want the buttons to be able to re-randomize the maze, solve the maze, and also ouput statistics (for percolation theory purposes). I have the backbone all done already, I am just creating the GUI now. I am just figuring out EventHandlers and such through the tutorials, but I have a question. I am adding an ActionListener to the buttons which are on the ButtonPanel which are on JFrame (SOUTH) Panel. But who do I make the ActionListener--Basically the one doing the work when the button is pressed. Do I make the JFrame the ActionListener or the MazePanel the ActionListener. I need something which has access to the maze data (the backbone), and which can call the Maze.randomize() function. I'm trying to make a good design and not just slop too.
    Also I was wondering if I do this
    JButton.addActionListener(MazePanel), and lets say public MazePanel implments ActionListenerdoesn't adding this whole big object to another object (namely the button actionlistener) seem really inefficient? And how does something that is nested in a JPanel on JFrame x get information from something nested in another JPanel on a JFrame x.
    Basically how is the Buttons going to talk to the maze when the maze is so far away?

    I'm not an expert, but here's what I'd do....
    You already have your business logic (the Maze classes), you said. I'm assuming you have some kind of public interface to this business logic. I would create a new class like "MazeGui" that extends JFrame, and then create the GUI using this class. Add buttons and panels as needed to get it to look the way you want. Then for each button that does a specific thing, add an anonymous ActionListener class to it and put whatever code you need inside the ActionListener that accesses the business logic classes and does what it needs to.
    This is the idea, though my code is totally unchecked and won't compile:
    import deadseasquirrels.mazestuff.*;
    public class MazeGui extends JFrame {
      JPanel buttonPanel = new JPanel();
      JPanel mazePanel = new JPanel();
      JButton randomizeB = new JButton();
      JButton solveB = new JButton();
      JButton statsB = new JButton();
      // create instanc(es) of your Maze business logic class(es)
      myMaze = new MazeClass();
      // add the components to the MazeGui content pane
      Component cp = getContentPane();
      cp.add(); // this doesn't do anything, but in your code you'd add
                // all of your components to the MazeGui's contentpane
      randomizeB.addActionListener(new ActionListener {
        void actionPerformed() {
          Maze newMaze = myMaze.getRandomMazeLayout();
          mazePanel.setContents(newMaze); // this is not a real method!
                                          // it's just to give you the idea
                                          // of how to manipulate the JPanel
                                          // representing your Maze diagram,
                                          // you will probably be changing a
                                          // subcomponent of the JPanel
      solveB.addActionListener(new ActionListener {
        void actionPerformed() {
          Solution mySolution = myMaze.getSolution();
          mazePanel.setContents(mySolution); // again, this is not a real
                                             // method but it shows you how
                                             // the ActionListener can
                                             // access your GUI
      // repeat with any other buttons you need
      public static void main(String[] args) {
        MazeGui mg = new MazeGui();
        mg.setVisible(true);
        // etc...
    }

  • How do you change the image in a JPanel?

    Hey! (firstly, i need to use image on a JPanel, label&icon not an option).
    I've made my JPanel class
        public class DrawingPanel extends JPanel {
            Image img;
            DrawingPanel() {
                Image img = Toolkit.getDefaultToolkit().getImage("13.jpg"); //default image at startup
            public void paintComponent (Graphics g) {
                super.paintComponent (g);
                // Use the image width & height to find the starting point
                int imgX = getSize ().width/2 - img.getWidth (this);
                int imgY = getSize ().height/2 - img.getHeight (this);
                //Draw image centered in the middle of the panel   
                g.drawImage (img, 0, 0, this);
            } // paintComponent
        } // DrawingPanelAnd I've made my instance of it and added it to the Frame:
        DrawingPanel imagePanel = new DrawingPanel();Now, I need to change the image on the JPanel according to the user's input. I have the string of the location of the new image. How do I update the JPanel to display this new image? Any help greatly appreciated!!

    Well... You could just add a method in your class which sets the image according to the new image name you provided. Something like this:
        public class DrawingPanel extends JPanel {
            Image img;
            DrawingPanel() {
                img = Toolkit.getDefaultToolkit().getImage("13.jpg"); //default image at startup
            public void setImage(String imageName) {
                    img = Toolkit.getDefaultToolkit().getImage(imageName); //Loading the desired image
            public void paintComponent (Graphics g) {
                super.paintComponent (g);
                // Use the image width & height to find the starting point
                int imgX = getSize ().width/2 - img.getWidth (this);
                int imgY = getSize ().height/2 - img.getHeight (this);
                //Draw image centered in the middle of the panel   
                g.drawImage (img, 0, 0, this);
            } // paintComponent
        } // DrawingPanel

  • Embeding PDF file into JPanel or JFrame

    Do any one have an idea on how to embed a PDF file into JPanel or JFrame?

    I can suggest you ways
    1)pdf to Image
    2)pdf to html
    http://www.verypdf.com/pdf2htm/
    Free online service
    http://www.gohtm.com/

  • How to draw an Image on a JPanel?

    Hi all,
    Can any one give code for drawing an image on a JPanel?
    thanks,
    amar

    That's for the JLabel right? ... For a JPanel on a JFrame, you could do something like this:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    import java.net.URL;
    public class TestImagePaint  extends JFrame {
      private BjPanel bjp;
      public TestImagePaint( String imageName )   throws IllegalArgumentException {
        if ( imageName == null  ||  !( new File( imageName ).isFile() ) ) {
          throw new IllegalArgumentException( "\nIn TestImagePaint constuctor"
                                             +"\t IllegalArgumentException:" );
        bjp = new BjPanel( imageName );
        getContentPane().add( bjp );
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setBounds( 100, 100, 400, 300 );
        setVisible( true );
      public static void main( String[] argv ) {
        new TestImagePaint( argv[0] );
      public class BjPanel  extends JPanel {
        private URL   url;
        private Image image;
        public BjPanel( String imageName ) {
          try {
            url   = BjPanel.class.getResource( imageName );
            image = Toolkit.getDefaultToolkit().getImage( url );
            repaint();
          catch( Exception e ) {
            System.out.println( "Can't get Image: "+imageName+"\n\t"+e );
            System.exit( -1 );
        public void paint(Graphics g) {
          g.drawImage( image, 0, 0, this );
    }

  • How to use a backgroud image in a JPanel.....

    hi,
    How can I display a background image for a JPanel.....
    I have searched this forum but all solutions I found are where JPanel at the end has to be displayed on a frame in an application...
    I need to use JPanel on Applet.......
    Actually I am switching JPanels on JApplet as screens for a game....
    I need to display background image for one of the JPanel class that has to be called by an Applet class....
    Thanks a lot for any help.

    Thanks a lot for your help.
    How do we specify URL to use an image from net. Is itt like I open an image file on some website & use its address? Kindly add here some website too so that its more clear for me.
    Thanks a lot for any help & all help your provided previously.
    String imageFilePath = "urlOfMyImageFile"; // I am asking about parameter to be used here
    ImageIcon ii = new ImageIcon(imageFilePath);
    Image image = ii.getImage();

Maybe you are looking for