Jump to content
  • Advertisement
Sign in to follow this  
GogromaT

[java] Adding custom draw Image to JPanel - to JTabbedPane

This topic is 3169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I hope I am in a right forum. Its my first semester of Java and I was asked to write a swing application that would show 20 images, and was able to zoom, rotate, invert colors of images, ... well do at least 3 options. So I have lots of issues, but main is how to add an image that I am overriding with paintComponent(Graphics g) to the JPanel, that would be a part of JTabbedPane. So here is my full code (sorry for too many libraries, I know I should use much less): And the main problem is in lines 75 - 83 and line 212: The problem is that program is loading well, but image does not appear in the specified tabbed pane...
//----first line starts at the next line------
package alexviewer;

import java.net.URL;
import java.applet.Applet;



import java.awt.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;


import javax.swing.JTabbedPane;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.ImageIcon;




public class AlexFrame extends JPanel
                       implements ActionListener, ChangeListener {

    
    JTabbedPane tabbedPane= new JTabbedPane();
    

    protected JButton b1, b2, b3;
    protected JPanel p1, p2, p3;
    //
    //private Image image;
    //
    protected JButton images_btn[];
    protected JLabel label;
    
    /*
    //
    AffineTransform identity = new AffineTransform();

    private URL getURL(String filename) {
    URL url = null;
    try {
    url = this.getClass().getResource(filename);
    }
    catch(Exception e){}
    return url;
    }

    public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    AffineTransform trans = new AffineTransform();
    trans.setTransform(identity);
    trans.rotate( Math.toRadians(45) );
    g2d.drawImage(image, trans, this);
    }
    

    //Image image = new ImageIcon("images/21.jpg").getImage();
    */

    //Swing Image drawing
    JPanel drawingArea = new JPanel(){
    Image image=Toolkit.getDefaultToolkit().getImage("images/21.jpg");
    //Drawing Image
    public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawImage(image,0,0,this);
    JLabel mylabel = new JLabel("my label");
    }   
    };
    
    public AlexFrame(){

        images_btn = new JButton[20];//20 JButtons
        ImageIcon icon[] = new ImageIcon[20];
        icon[0] = createImageIcon("images/1.jpg");
        icon[1] = createImageIcon("images/2.jpg");
        icon[2] = createImageIcon("images/3.jpg");
        icon[3] = createImageIcon("images/4.jpg");
        icon[4] = createImageIcon("images/5.jpg");
        icon[5] = createImageIcon("images/6.jpg");
        icon[6] = createImageIcon("images/7.jpg");
        icon[7] = createImageIcon("images/8.jpg");
        icon[8] = createImageIcon("images/9.jpg");
        icon[9] = createImageIcon("images/10.jpg");
        icon[10] = createImageIcon("images/11.jpg");
        icon[11] = createImageIcon("images/12.jpg");
        icon[12] = createImageIcon("images/13.jpg");
        icon[13] = createImageIcon("images/14.jpg");
        icon[14] = createImageIcon("images/15.jpg");
        icon[15] = createImageIcon("images/16.jpg");
        icon[16] = createImageIcon("images/17.jpg");
        icon[17] = createImageIcon("images/18.jpg");
        icon[18] = createImageIcon("images/19.jpg");
        icon[19] = createImageIcon("images/20.jpg");

        for(int i=0; i<20; i++){
          images_btn = new JButton(icon); 
          images_btn.setPreferredSize(new Dimension(240, 160)); 
          images_btn.addActionListener(this);
        }
        
        ImageIcon icon21 = createImageIcon("images/edit.jpg");
        ImageIcon icon22 = createImageIcon("images/zoom.jpg");

        //Making icons smaller
        Image img = icon21.getImage();  
        Image p = img.getScaledInstance(25, 25,  java.awt.Image.SCALE_SMOOTH);  
        ImageIcon icon_edit = new ImageIcon(p);

        img = icon22.getImage();  
        p = img.getScaledInstance(25, 25,  java.awt.Image.SCALE_SMOOTH);  
        ImageIcon icon_zoom = new ImageIcon(p);


  
        b1 = new JButton("Invert Colors", icon_edit);
        Font font = b1.getFont().deriveFont(Font.PLAIN);
        b1.setFont(font);
        b1.setVerticalTextPosition(AbstractButton.BOTTOM);
        b1.setHorizontalTextPosition(AbstractButton.CENTER);
        b1.setMnemonic(KeyEvent.VK_D);
        b1.setActionCommand("edit");
        

        b2 = new JButton("Zoom", icon_zoom);
        Font font2 = b2.getFont().deriveFont(Font.PLAIN);
        b2.setFont(font2);
        b2.setForeground(new Color(0xffffdd));
        b2.setVerticalTextPosition(AbstractButton.BOTTOM);
        b2.setHorizontalTextPosition(AbstractButton.CENTER);
        b2.setMnemonic(KeyEvent.VK_M);
        b2.setActionCommand("zoom");
        b2.setEnabled(true);


        //Same Zoom Button in Tab Pane 2
        b3 = new JButton("Zoom", icon_zoom);
        Font font3 = b3.getFont().deriveFont(Font.PLAIN);
        b3.setFont(font2);
        b3.setForeground(new Color(0xffffdd));
        b3.setVerticalTextPosition(AbstractButton.BOTTOM);
        b3.setHorizontalTextPosition(AbstractButton.CENTER);
        b3.setMnemonic(KeyEvent.VK_M);
        b3.setActionCommand("zoom");
        b3.setEnabled(true);
        
        //Listen for actions on buttons 1 and 3.
        b1.addActionListener(this);
        b2.addActionListener(this);

        //Creating two panels
        p1 = new JPanel();  p1.setPreferredSize(new Dimension(1300, 740));
        p2 = new JPanel();  p2.setPreferredSize(new Dimension(450, 75));
        p3 = new JPanel();


        // Adding image buttons to the top panel;
        for(int j=0; j<20; j++)
        {
           p1.add(images_btn[j]);
        }

        //Add contol buttons to the lower panel
        p1.add(b1);
        p1.add(b2);
        p2.add(b3);


        //Adding panels to this container
        this.setLayout(new BorderLayout());

        
        add(p1, BorderLayout.CENTER);
        //add(p2, BorderLayout.SOUTH);

        
        tabbedPane.addTab("Album View",p1);
        tabbedPane.addTab("Image Operations",p2);
        //tabbedPane.addTab("Zoom",p2);


        //Addint tabbedPane to this container    
        add(tabbedPane);
        //Scrolling Tabs
        //tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);


      
       ActionListener actionListener = new ActionListener(){
       public void actionPerformed(ActionEvent actionEvent){
       JButton button = (JButton) actionEvent.getSource();
       Icon icon40  = button.getIcon(); 
       JLabel label = new JLabel();
       label.setIcon(icon40); 
       //p3.add(label);
       }};

      tabbedPane.addTab("1",drawingArea);
      tabbedPane.addTab("LI",p3);
      tabbedPane.addChangeListener(this);
    }//end of AlexFrame();
//------------------------------------------------------------------------------

     public void stateChanged(ChangeEvent e) 
     {
          JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
            int index = sourceTabbedPane.getSelectedIndex();
            JFrame frame1 = new JFrame("Hello");
            frame1.setVisible(true);  
     }    
          

    public void actionPerformed(ActionEvent e) 
    {
        if ("edit".equals(e.getActionCommand())) {
//            b1.setEnabled(false);
            JFrame frame1 = new JFrame("Invert Colors");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setSize(new Dimension(500,450));
            
            //Display the window.
            //frame1.getContentPane().add(label);
            frame1.pack();
            frame1.setVisible(true);
       
        }
        if("zoom".equals(e.getActionCommand())){
            JFrame frame1 = new JFrame("Zoom");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setSize(new Dimension(500,450));
            
             //Display the window.
            frame1.pack();
            frame1.setVisible(true);
        }
        
    else {}
    }


    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = AlexFrame.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }


    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Invert Colors");
        frame.setSize(new Dimension(500,450));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.getContentPane().add(new AlexFrame());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });//end of new runnable();
    }//end of main();
}

[Edited by - Glass_Knife on October 16, 2009 7:21:32 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hmm, is the OP still looking for a solution? I'm just now cleaning it up in Netbeans.

EDIT: just started sifting through it - and probably won't go any further unless the OP returns. I can't seen any rationale behind delegating GUI creation to the event dispatch thread at all; and the createImageIcon() method has me completely perplexed. I'd like to hear some sort of explanation of the use of ClassLoader-relevant constructs in attempting to load image files.

EDIT2: oh, I see. Don't necessarily like it, though.

[Edited by - Fenrisulvur on October 17, 2009 8:16:53 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!