Load an image problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Load an image problem

  1. #1
    Join Date
    Aug 2006
    Posts
    3

    Load an image problem

    Hi i have been given a task at school, this is the task:

    Design and implement a GUI program that will allow a user to load and display one of 2 images. The images should be of your own choosing.

    You should provide a text area to display the images.

    You should provide three buttoms, two of the buttons will load the images and the third will clear the text area.

    So far I have been able to do the GUI with the textarea and the three buttons, but i have no idea how to load the image after the button is pressed
    If any one knows please help me, I hane only few more days to complete the task thanx in advance

  2. #2
    Join Date
    Dec 2005
    Posts
    97
    do you mean specifically a JTextArea or do you mean something that can display text?

    all i did was create a JTextPane and then when the button was clicked load the specified image:

    Code:
    textPane.insertIcon(new ImageIcon("somePicture.jpg"));
    is this all you needed?
    Last edited by anubis; 08-13-2006 at 07:28 PM.

  3. #3
    Join Date
    Aug 2006
    Posts
    3

    Source code

    Hmm the best way you can try to help i think is when you look at my source code and try to re do it in the proper way, but many thanx for the reply


    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public final class AreaTest extends JFrame
    {
    private JButton b1, b2, b3; //Registering number buttons and operational buttons
    private JPanel buttonLayout; //creating Panel for button layout
    private int a=0;

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.createImage("img.jpg");


    public AreaTest()
    {
    Container container = getContentPane();
    container.setLayout(new BorderLayout());


    JPanel buttonLayout = new JPanel();
    buttonLayout.setLayout(new GridLayout(3, 1));

    JTextArea textArea = new JTextArea()
    {

    {setOpaque(false);}
    public void paintComponent (Graphics g)
    {


    g.drawImage(image, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), this);
    super.paintComponent(g);
    }

    };

    b1 = new JButton("one"); //creation of a button using command JButton; doing the same with the rest of the buttons
    b1.setBackground(Color.orange); //setting button color to orange
    b2 = new JButton("two");
    b2.setBackground(Color.orange);
    b3 = new JButton("three");
    b3.setBackground(Color.orange);

    b1.addActionListener(new ActionListener() //adding function to a particular button
    { public void actionPerformed(ActionEvent e)
    {

    }
    });


    container.add(textArea);
    container.add(buttonLayout, "South");
    buttonLayout.add(b1); //adding registered buttons in order to make them appear inside the calculator
    buttonLayout.add(b2);
    buttonLayout.add(b3);


    }


    public static void main(String[] args)
    {

    AreaTest areaTest = new AreaTest();
    areaTest.setSize(300, 300);
    areaTest.setLocation(350,230);
    //areaTest.setResizable(false);
    areaTest.setTitle("Area test");
    areaTest.setVisible(true);
    }
    }

  4. #4
    Join Date
    Dec 2005
    Posts
    97
    ok, well i don't know if this is the proper way to do this as i dont work with images, but this does seem to work.

    in the actionPerformed method i just added this:
    Code:
    Graphics g = textArea.getGraphics();
    image = (new ImageIcon("someImage.jpg")).getImage();
    g.drawImage(image, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), textArea);
    also make sure you declare textArea final. this is just a guess but hopefully it does what you need it to do.

  5. #5
    Join Date
    Aug 2006
    Posts
    3

    many thanx anubis

    Its working really thank you, but :-) have no clue how to do the 3 button to clear the screen, aint working as it should any, help on this would be really appreciated.

  6. #6
    Join Date
    Dec 2005
    Posts
    97
    hmm....i dont have the time right now to fully experiment with this as i have to finish a bunch of things for the start of school, but if you set the image to null, then it kinda clears the picture you just loaded, but not really. hopefully if you try it you'll understand. once im finished with my school work then i'll spend a little more time on this problem.

    Code:
    Graphics g = textArea.getGraphics();
    image = null; //kinda clears it...image is still there, but if you click on the textArea and then press enter, the image disappears...
    g.drawImage(image, 0, 0, (int)getSize().getWidth(), (int)getSize().getHeight(), textArea);

Similar Threads

  1. Type Load Exception Problem
    By Govil Manish in forum .NET
    Replies: 1
    Last Post: 09-25-2006, 07:33 AM
  2. Replies: 0
    Last Post: 03-22-2006, 02:17 AM
  3. load image from XML with XSL
    By paetje in forum XML
    Replies: 1
    Last Post: 03-06-2006, 04:10 AM
  4. Image won't display in Applet
    By syntax_error0 in forum Java
    Replies: 1
    Last Post: 02-06-2006, 01:33 AM
  5. Image problem
    By Jack in forum Web
    Replies: 0
    Last Post: 09-13-2001, 04:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center