Welcome to Dream.In.Code
Become a Java Expert!

Join 150,362 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,803 people online right now. Registration is fast and FREE... Join Now!




picture

 
Reply to this topicStart new topic

picture

dato.java
15 Feb, 2008 - 09:13 AM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 30

can anyone tell me or write a little programme which will open a picture from a file cool.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Picture
15 Feb, 2008 - 09:43 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Typically it is not the policy of this site to provide code without first seeing a try on your part. However, I had this lying around and it would show you how to setup a picture from a file and put it on an application. So in the future, please provide code that you are working on and showing an attempt to do it and we will do our best to help you from there. Thanks!

CODE

import javax.swing.*;
import java.awt.*;

public class picpanel extends JPanel {
    // This is a custom image we are loading. You would need to change this or
    // come up with a dynamic location... most likely from a filechooser.

    Image img = getToolkit().getImage("c:\\mypic.jpg");

    public picpanel() { }

    // Fires whenever this panel is resized
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(),this);
    }
}


The above class loads in a picture from the C:\ drive. In this case "mypic.jpg". It loads it using the java.awt toolkit using getToolkit's getImage method. It then takes over the panels painting to draw the image.

CODE

import javax.swing.*;
import java.awt.event.*;

public class mypic extends JFrame {
    // Get an instance of our class

    picpanel p = new picpanel();

    public mypic() {
        super("Picture Example");

        
        setLayout(null);

        // Set our panel's inital size (thus changing our picture's size)
        p.setBounds(10, 10, 100, 150);
        
        add(p);

        JButton changeSize = new JButton("Change");

        changeSize.setBounds(10, 170, 100, 30);


        // Add a listener to our change size button
        changeSize.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                            // Change the size of the panel by adding 20 px to its width each time
                            p.setSize(p.getWidth() + 20, 150);
                        }
                        });

        add(changeSize);
    }

    public static void main(String args[]) {
        mypic frame = new mypic();

        frame.setSize(300,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}


This driver program sets up our JFrame, creates our picpanel class instance and puts it on the frame along with a button. The button then has an action listener attached to it to respond to clicking. It adds 20 pixels to the width of the picture causing the repaint to fire. This will allow you to change the size of the picture dynamically.

Enjoy!

"At DIC we be java picture stretching code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month