GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Oct-2007, 21:07
tedds tedds is offline
New Member
 
Join Date: Oct 2007
Posts: 6
tedds is on a distinguished road

Image onto JFrame


Hello I am starting an app to import an image, it imports an image ok when it i hard coded. I am trying to provide a name for the image in main, using args[0], but I keep getting a "Exception in main()java.lang.NullPointerException". is this because the args parameter is not getting used by the paint method?

cheers

tedds


JAVA Code:
import java.awt.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.filechooser.*;

class TestImage extends JFrame
{

  ImagePanel theImagePanel;
  
  private String args;
  private String DEFAULT_FILE_IMAGE = args;
  private final int X_POSITION = 10;
  private final int Y_POSITION = 40;
  
  private JButton selectFileButton;
  
  private JButton upsideDownButton;
  private JButton cycleColorsButton;
  private JButton reduceRedButton;
  private JButton reduceSizeButton;
  private JButton saveToFileButton;
  
  public static File fDirectory = null;
  

  public TestImage(String theTitle)
  {
   super(theTitle);  
      
   theImagePanel = new ImagePanel();
     
   JPanel buttonPanel = new JPanel();
   buttonPanel.setLayout(new FlowLayout());
   buttonPanel.setBorder(BorderFactory.createEtchedBorder(Color.red, Color.red));
   
   selectFileButton = new JButton("Open File");
   buttonPanel.add(selectFileButton);  
    
   JPanel functionPanel = new JPanel();
   functionPanel.setLayout(new FlowLayout());
   functionPanel.setBorder(BorderFactory.createEtchedBorder(Color.red, Color.red));
   
   upsideDownButton = new JButton("Upside Down");
   cycleColorsButton = new JButton("Cycle Color");
   reduceRedButton = new JButton("Reduce Red");
   reduceSizeButton = new JButton("Reduce Size");
   saveToFileButton = new JButton("Save");
   functionPanel.add(upsideDownButton);
   functionPanel.add(cycleColorsButton);
   functionPanel.add(reduceRedButton);
   functionPanel.add(reduceSizeButton);
   functionPanel.add(saveToFileButton);
   
   //add listener to buttons
   selectFileButton.addActionListener(new SelectFileListener());
   upsideDownButton.addActionListener(new FlipImageListener());
   cycleColorsButton.addActionListener(new CycleColorListener());
   reduceRedButton.addActionListener(new ReduceRedListener());
   reduceSizeButton.addActionListener(new ReduceSizeListener());
   saveToFileButton.addActionListener(new SaveFileListener());
   
   
   //text area to add messages to
   JTextPane messageTextPanel = new JTextPane();
   
   //set up scoll affect
   JScrollPane scroll = new JScrollPane(messageTextPanel);
   //set message panel fort properties
   messageTextPanel.setFont(new Font("MS Sans Serif", 1, 14));
   //place a border around the whole panel
   scroll.setBorder(BorderFactory.createEtchedBorder(Color.red, Color.red));
   //place a border around the inner message panel and give it a title
   messageTextPanel.setBorder(new TitledBorder("Messages"));
   //position whole panel on the screen and set size
   scroll.setBounds(480,45,300,475);

   //add panels to main JFrame
   getContentPane().add(scroll);
   getContentPane().add("North",buttonPanel);
   getContentPane().add(theImagePanel);
   getContentPane().add("South",functionPanel);

  }
  
  /**
   * This class blah blah blah
   */
  class SelectFileListener implements ActionListener
  {

    public void actionPerformed(ActionEvent e)
    {
        
        try
        {
        Image image = new Image(DEFAULT_FILE_IMAGE);
        //image.flipX();
        //System.out.print("Select file button pressed");
        
        /*
        JFileChooser chooser = new JFileChooser();
        chooser.setBackground(Color.blue);
        if(fDirectory != null)
        chooser.setCurrentDirectoy(fDirectory);
        */
        
        ///*
        JFileChooser chooser = new JFileChooser();  
        
        FileReader in = null;
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            File selectedFile = chooser.getSelectedFile();
            String path = selectedFile.getPath();
            System.out.println(path);
            Image DEFAULT_FILE_IMAGE = new Image(path);
            
            if (!selectedFile.exists())
            {
                System.out.println(image + " does not exists");
            }
            //getContentPane().add(new image(newImage));
            //reader = new fileReader(selectedFile);
        }
        //*/
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }
              //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new SelectFileListener();
      selectFileButton.addActionListener(listener);
    }
  }
  
  /**
   * This class blah blah blah
   */
  class FlipImageListener implements ActionListener
  {

      JTextPane messageTextPanel;// = new JTextPane();
    public void actionPerformed(ActionEvent e)
    {
        try
        {
        Image image = new Image(DEFAULT_FILE_IMAGE);
        image.flipX();
        System.out.println("Flip button pressed");
        //messageTextPanel.replaceSelection("Flip button pressed");
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }
      //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new FlipImageListener();
      upsideDownButton.addActionListener(listener);
      //return upsideDownButton;
    }

  }
  
  /**
   * This class blah blah blah
   */
  class CycleColorListener implements ActionListener
  {

      JTextPane messageTextPanel;// = new JTextPane();
    public void actionPerformed(ActionEvent e)
    {
        try
        {
        Image image = new Image(DEFAULT_FILE_IMAGE);
        //image.flipX();
        System.out.println("Cycle color button pressed");
        //messageTextPanel.replaceSelection("Flip button pressed");
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }
      //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new CycleColorListener();
      cycleColorsButton.addActionListener(listener);
      //return upsideDownButton;
    }

  }
  
  /**
   * This class blah blah blah
   */
  class ReduceRedListener implements ActionListener
  {

      JTextPane messageTextPanel;// = new JTextPane();
    public void actionPerformed(ActionEvent e)
    {
        try
        {
        Image image = new Image(DEFAULT_FILE_IMAGE);
        //image.flipX();
        System.out.println("Reduce red button pressed");
        //messageTextPanel.replaceSelection("Flip button pressed");
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }
      //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new ReduceRedListener();
      reduceRedButton.addActionListener(listener);
      //return upsideDownButton;
    }

  }
  
  /**
   * This class blah blah blah
   */
  class ReduceSizeListener implements ActionListener
  {

      JTextPane messageTextPanel;// = new JTextPane();
    public void actionPerformed(ActionEvent e)
    {
        try
        {
        Image image = new Image(DEFAULT_FILE_IMAGE);
        //image.flipX();
        System.out.println("Reduce size button pressed");
        //messageTextPanel.replaceSelection("Flip button pressed");
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }
      //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new ReduceSizeListener();
      reduceSizeButton.addActionListener(listener);
      //return upsideDownButton;
    }

  }
  
  /**
   * This class blah blah blah
   */
  class SaveFileListener implements ActionListener
  {

      JTextPane messageTextPanel;// = new JTextPane();
    public void actionPerformed(ActionEvent e)
    {
        try
        {
        Image image = new Image(DEFAULT_FILE_IMAGE);
        //image.flipX();
        System.out.println("Save file button pressed");
        //messageTextPanel.replaceSelection("Flip button pressed");
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }
      //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new SaveFileListener();
      saveToFileButton.addActionListener(listener);
      //return upsideDownButton;
    }

  }
  
  /**
   * This class ImagePanel is where the image is set
   */
  
  class ImagePanel extends JPanel //or JFrame
  {
      
  public void paint(Graphics g)
  {
      try {
      // Read the image
      Image image = new Image(args);

      // Display the image
      image.draw(g, X_POSITION,Y_POSITION);
      
      // Flip the image upside down
      image.flipX();
      // Draw the flipped image
      
   // image.draw(g, X_POSITION + (image.getWidth()*2),Y_POSITION);
      image.draw(g, X_POSITION ,Y_POSITION);
      //image.write("horseNew.jpg");
        }
      catch (Exception e) 
      {
        System.out.println("Exception in main()" + e.toString());
      }

  }
}
  public static void main(String args[])
        throws java.io.IOException, java.io.FileNotFoundException
    {
               if (args.length != 1)
       {
         System.err.println(
           "Please include image filename on command line");
         System.exit(-1);
       }
       
    // Read
    Image image = new Image(args[0]);
    System.out.println(args[0]);
   
    TestImage.setDefaultLookAndFeelDecorated(true);
    TestImage testimage = new TestImage("Test Image"); 
    testimage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    testimage.setVisible(true);
    testimage.setPreferredSize(new Dimension(800, 600));
    testimage.setResizable(false);
    testimage.pack();
    
    //Center the main frame
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension size = testimage.getSize();
    screenSize.height = screenSize.height/2;
    screenSize.width = screenSize.width/2;
    size.height = size.height/2;
    size.width = size.width/2;
    int y = screenSize.height - size.height;
    int x = screenSize.width - size.width;
    
    //set location from x and y settings
    testimage.setLocation(x, y);

  }
}
Last edited by LuciWiz : 24-Oct-2007 at 11:23. Reason: Please insert your Java code between [java] & [/java]
  #2  
Old 24-Oct-2007, 09:26
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 926
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Image onto JFrame


Yes, 'args' IS null at the paint method, for in the TestImage class, 'args' is declared, but never assigned anything -- so it is null.

How does that code work for you? The Image class is abstract, and compiling gives this:
Code:
C:\TestImage.java:103: java.awt.Image is abstract; cannot be instantiated Image image = new Image(DEFAULT_FILE_IMAGE); ^ C:\TestImage.java:123: java.awt.Image is abstract; cannot be instantiated Image DEFAULT_FILE_IMAGE = new Image(path); ^ C:\TestImage.java:155: java.awt.Image is abstract; cannot be instantiated Image image = new Image(DEFAULT_FILE_IMAGE); ^ C:\TestImage.java:156: cannot find symbol symbol : method flipX() location: class java.awt.Image image.flipX(); ^ C:\TestImage.java:183: java.awt.Image is abstract; cannot be instantiated Image image = new Image(DEFAULT_FILE_IMAGE); ^ C:\TestImage.java:211: java.awt.Image is abstract; cannot be instantiated Image image = new Image(DEFAULT_FILE_IMAGE); ^ C:\TestImage.java:239: java.awt.Image is abstract; cannot be instantiated Image image = new Image(DEFAULT_FILE_IMAGE); ^ C:\TestImage.java:267: java.awt.Image is abstract; cannot be instantiated Image image = new Image(DEFAULT_FILE_IMAGE); ^ C:\TestImage.java:295: java.awt.Image is abstract; cannot be instantiated Image image = new Image(args); ^ C:\TestImage.java:298: cannot find symbol symbol : method draw(java.awt.Graphics,int,int) location: class java.awt.Image image.draw(g, X_POSITION,Y_POSITION); ^ C:\TestImage.java:301: cannot find symbol symbol : method flipX() location: class java.awt.Image image.flipX(); ^ C:\TestImage.java:305: cannot find symbol symbol : method draw(java.awt.Graphics,int,int) location: class java.awt.Image image.draw(g, X_POSITION ,Y_POSITION); ^ C:\TestImage.java:326: java.awt.Image is abstract; cannot be instantiated Image image = new Image(args[0]); ^ 13 errors
Anyway, since 'args' is NOT global from main(), you can pass the desired 'args' value from main through a TestImage constructor, maybe something like this:
JAVA Code:
public TestImage(String theTitle, String theImagePath) // 2-arg constructor.
{
  args = theImagePath; // assign the 'args' value in the TestImage class.
  TestImage(theTitle); // keep the original constructor for initialization.
}
Note that this is just one possible solution. There are many ways to solve the problem.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 24-Oct-2007, 09:36
tedds tedds is offline
New Member
 
Join Date: Oct 2007
Posts: 6
tedds is on a distinguished road

Re: Image onto JFrame


Thats not good, I should have said that I have a class "Image". Thank you for your response, i changed the constuctor and it worked, so i have quickly gone onto creating a JFileChooser.
I just added a JFileChooser to my code, so that when the app is running you can click the button to choose a different file. I can see how I can allocate the new pic to a brand new frame by duplicating the main method and creating a new JFrame. Is there a way that I can use the selected file from JFileChooser and replace the original file from args[0]?

cheers

JAVA Code:
public void actionPerformed(ActionEvent e)
    {
        
        JFrame f = new JFrame();
        JFileChooser jFileChooser1 = new javax.swing.JFileChooser();
        f.addWindowListener(new java.awt.event.WindowAdapter()
        {
            public void windowClosing(java.awt.event.WindowEvent evt)
            {
                System.exit(0);
            }
        });
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        f.setLocation((screenSize.width-500)/2,(screenSize.height-300)/2);  
        int returnVal = jFileChooser1.showOpenDialog(f);
        if (returnVal == jFileChooser1.APPROVE_OPTION)
        {
            File fi = new File(jFileChooser1.getSelectedFile().getPath());
            String path = fi.getPath();
            System.out.println(path);
            ImageIcon icon = new ImageIcon(path);
 
            TestImage testimage = new TestImage("fileload", fi.getParent()+"/"+fi.getName());
 
//this puts an image on a new JFrame
     /*       
    testimage.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    testimage.setVisible(true);
    testimage.setPreferredSize(new Dimension(800, 600));
    testimage.setResizable(false);
    testimage.pack();
    */
 
       } 
  #4  
Old 24-Oct-2007, 09:49
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 926
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Image onto JFrame


If there's a way to get the file string from the JFileChooser, I would think it would be a simple of matter of reassigning the 'args' variable with the file string -- if that it what you mean.

Note: I made a minor edit in post #2.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #5  
Old 24-Oct-2007, 09:54
tedds tedds is offline
New Member
 
Join Date: Oct 2007
Posts: 6
tedds is on a distinguished road

Re: Image onto JFrame


yep, thats what I had in mind. Im going to give that a go now. I just have to make sure the args[0] in the main method still don't override the new file string.

cheers

yep it worked a charm, excatly what you mentioned

thank you for your time, its good to learn from not just books.

cheers
  #6  
Old 24-Oct-2007, 10:46
tedds tedds is offline
New Member
 
Join Date: Oct 2007
Posts: 6
tedds is on a distinguished road

Re: Image onto JFrame


hello again, im going pretty quick through what to do with this swing stuff. the Image class has a method of flipX(), which turns the image upside down. I am trying to use this method and see if I can in fact change the image. I think that this is similar to the last issue that I had, but I'm not to sure. Do you how I can do this?

cheers

JAVA Code:
  class FlipImageListener implements ActionListener
  {

    public void actionPerformed(ActionEvent e)
    {
        try
        {
        Image image = new Image(theImage);
        
        image.flipX();

        TestImage testimage = new TestImage("Flipped Image", theImage);
  
    }
        catch (java.io.IOException exception)
        {
            exception.printStackTrace();
        }

      //add lisnter to flip to allow for turning image upside down
      ActionListener listener = new FlipImageListener();
      upsideDownButton.addActionListener(listener);
    }

  }
  #7  
Old 25-Oct-2007, 18:04
tedds tedds is offline
New Member
 
Join Date: Oct 2007
Posts: 6
tedds is on a distinguished road

Re: Image onto JFrame


Hello TurboPT, thanks for your advice on the previous. I have come back to this and am checking out how to cycle through the rbg colors of a loaded image. for example, the red takes the blue value, blue takes the green value and green takes the red value. I ave stated the method for this, but am stuck on how to get the rbg values from the array. I am assuming that after the second for loop, I find the rbg values then assign them, i.e. r(value) = b(value), b(value) = g(value), g(value) = r(value). Am I making a correct assumption here?

Cheers

Code:
public void cycleColor() { Color newRgbColor; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { newRgbColor = picture[row][col]; picture[row][col] = newRgbColor; } } }
  #8  
Old 25-Oct-2007, 19:48
TurboPT's Avatar
TurboPT TurboPT is offline
Regular Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 926
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Image onto JFrame


There is a getRGB() function (as well as other 'get' functions for specific, individual [Red,Green,Blue] colors) is this what you need?

Unfortunately, I haven't done much with color-space stuff, so my help might be limited here.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #9  
Old 25-Oct-2007, 20:04
tedds tedds is offline
New Member
 
Join Date: Oct 2007
Posts: 6
tedds is on a distinguished road

Re: Image onto JFrame


Yeah thats what I need, I've just been reading up about it and it looks like I can figured it out.
Thanks for the response.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing images arety33 Java Forum 1 12-Jul-2007 13:28
VC++: Image doubt gal4MS MS Visual C++ / MFC Forum 0 04-Oct-2006 15:56
How to create or display an image Unit1 C Programming Language 1 27-Sep-2006 02:18
GIM gidedit - a fltk fluid resize project cable_guy_67 FLTK Forum 2 01-Jun-2005 15:00
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 21:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.