DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006

    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
    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:

    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

    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()

    public void paintComponent (Graphics g)

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


    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");
    b3 = new JButton("three");

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


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


    public static void main(String[] args)

    AreaTest areaTest = new AreaTest();
    areaTest.setSize(300, 300);
    areaTest.setTitle("Area test");

  4. #4
    Join Date
    Dec 2005
    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:
    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

    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
    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.

    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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.