1

I add a tabbedpane in my frame and call tab.add(" ",new Img()) that extends Img with JPanel ..

The question is: Could I add JScrollPane in that JPanel and drawImage as JPanel's background and also to do additional drawing on that image such as making route on background image(such as map) because I want to apply Prim's algorithm on those route...

And also if I wish to add additional panel on tabbedpane like above, how could I control those tab actions..

The sample code is like that...

If you have any idea on Prim's algorithm and graph algorithm please help me! Thanks!

public class MainFrame extends JFrame {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuFile = new JMenu();
    private JMenuItem menuFileExit = new JMenuItem();
    private JPanel jPanel1 = new JPanel();
    private JLabel lbl1=new JLabel();
    private JLabel lbl2=new JLabel();
    private JPanel jPanel2 = new JPanel();
    private JTabbedPane jTabbedPane1 = new JTabbedPane();
    private JPanel originalgraph = new JPanel();
    private JPanel zebuthiri = new JPanel();
    private JPanel dekhinathiri = new JPanel();
    private JPanel oattayathiri = new JPanel();
    private JPanel pobbathiri = new JPanel();
    private JPanel zeyathiri = new JPanel();
    int weight[][] = null;
    public MainFrame(int [][]w) {
        this.weight=w;
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setJMenuBar( menuBar );
        this.getContentPane().setLayout(null);
        Toolkit tk=getToolkit();
        Dimension size=tk.getScreenSize();
        this.setSize( new Dimension(size) );
        this.getContentPane().setBackground(Color.CYAN);
        menuFile.setText( "File" );
        menuFileExit.setText( "Exit" );
        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
        jPanel1.setBounds(new Rectangle(0, 0, 1365, 160));
        jPanel1.setLayout(null);
        lbl1.setBounds(new Rectangle(0, 0, 1365, 160));
        lbl1.setIcon(new ImageIcon("5.jpg"));
        lbl2.setIcon(new ImageIcon("b.jpg"));
        jPanel2.setBounds(new Rectangle(0, 630, 1365, 160));
        lbl2.setBounds(new Rectangle(0, 0, 1365, 160));
        jPanel2.setLayout(null);
        jTabbedPane1.setBounds(new Rectangle(0, 160, 1365, 470));

        menuFile.add( menuFileExit );
        menuBar.add( menuFile );
        jPanel1.add(lbl1);
        jPanel2.add(lbl2);


        jTabbedPane1.addTab("Zebu Thiri",new zebuthiri(weight));
        jTabbedPane1.addTab("Original Graph",new originalgraph(weight));
        jTabbedPane1.addTab("Dekhina Thiri",new dekhinathiri(weight));
        jTabbedPane1.addTab("Oattaya Thiri",new oattayathiri(weight));
        jTabbedPane1.addTab("Pobba Thiri",new pobbathiri(weight));
        jTabbedPane1.addTab("Zeya Thiri",new zeyathiri(weight));
        this.getContentPane().add(jTabbedPane1, null);
        this.getContentPane().add(jPanel2, null);
        this.getContentPane().add(jPanel1, null);
    }

    void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }
    public static void main(String args[]){
        int w[][]=new int [100][100];
        MainFrame f=new MainFrame(w);
        f.setVisible(true);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Soe Win
  • 11
  • 1
  • 3

1 Answers1

2

please read How to add an image to a JPanel?

in small lines the image can be added on JPanel like this

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
            // handle exception...
       }
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null); 
                    // see javadoc for more info on the parameters
    }
}

In the example above the image is loaded in the constructor of the class and it is then drawn by the paintComponent(...) which is called by default after executing the constuctor

Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
  • 1
    1) A simpler solution would be to use a JLabel with an ImageIcon. It also takes care of the asynchronous loading of the image. 2) _paintComponent(...) which is called by default after executing the constuctor_ This is clearly an incorrect statement. `paintComponent` is eventually called by the RepaintManager (or if you call one of the paintXXX method yourself) when required (ie, when a repaint event comes on the Event queue).3) Always print the stacktrace of the exception (to a log if preferred). Not logging a stacktrace can be a nightmare to debug code – Guillaume Polet Jul 13 '12 at 08:29
  • 2
    -1 for copying an _incorrect_ implementation ;-) Well, will not really downvote: with the many upvotes the original received it's hard not to make the same mistake ... (see my comment there, in short: paintComponent _must_ fill the entire area if the panel reports being opaque) – kleopatra Jul 13 '12 at 09:09
  • 1
    Please do respect the Principle of `Encapsulation`, while overriding the methods of the `super Class`. `paintComponent(...)` has the Access Specifier as `protected` and not `public` :-), though I had upvoted this a long time back, I just found this mistake to be rectified, just now :-) – nIcE cOw Jul 13 '12 at 09:22