-8

I have the following code set to display an image on my hard drive. however, whenever I run the code I get the following error

import java.awt.*;  
import java.awt.Graphics.*;  
import java.awt.event.*;  
import java.awt.image.*;  
import java.io.*;  
import javax.imageio.*;  
import javax.swing.*;  
import javax.swing.JFrame;  
  
  
public class ImagePanel extends JPanel{  
  
    public ImagePanel() {  
      setTitle("ImagePanel");  
      setSize(500,250);  
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      setVisible(true);  
    public void (Graphics g){  
      Image image = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")).getImage();  
      g.drawImage(image,3,4, this);  
    }  
   
public static void main(String[] args) {  
  ImagePanel img = new ImagePanel();  
}  
} 

and the error I get:

<No main classes found>
peterh
  • 11,875
  • 18
  • 85
  • 108
Shawn
  • 933
  • 4
  • 18
  • 29
  • 1
    How do you run the code? – Henry Feb 09 '14 at 08:28
  • 1
    Will that even compile??? `public void (Graphics g)`??? – Hovercraft Full Of Eels Feb 09 '14 at 08:28
  • The `public static void main (String[] args)` you show in the snippet is exactly correct. So that part of the code is correct. Either you're not running the right class, or you're not invoking the class correctly. Q: Did you actually *COMPILE* this class??? – FoggyDay Feb 09 '14 at 08:28
  • 2
    you don't have a closing brace for "ImagePanel() " – TheLostMind Feb 09 '14 at 08:28
  • 1
    ... or a declared method name for your painting method. And you're trying to read in a file inside of a paint method -something you should never do. Please if you have code that won't compile, do post your compilation errors. Don't make us guess. – Hovercraft Full Of Eels Feb 09 '14 at 08:29
  • And don't even try to run code that doesn't compile. It makes no sense. Read the error messages you get from the compiler, fix all the errors, and then only you can run the application. That's somthing you should know much before doing GUI programming with Swing which is quite complex. – JB Nizet Feb 09 '14 at 08:30
  • Thanks for not replying to any of our comments or questions, for not clarifying your problem for us. – Hovercraft Full Of Eels Feb 09 '14 at 15:48

2 Answers2

1

The code you posted has syntax errors, thus cannot be successfully compiled.

For example:

public void (Graphics g){  
  Image image = new ImageIcon("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg")).getImage();  
  g.drawImage(image,3,4, this);  
} 

Is it a method? If yes, what is its method name?

I think you'd better refer to this page: How to add an image to a JPanel?

Community
  • 1
  • 1
windflyer
  • 305
  • 2
  • 8
1

Want to display Image, try something like this:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImagePanel extends JPanel{
    private BufferedImage bi;

    public ImagePanel() {  

      try {
            bi = ImageIO.read(new File("Your Image Path"));
        } catch (IOException ex) {
            Logger.getLogger(ImagePanel.class.getName()).log(Level.SEVERE, null, ex);
        }


        final JPanel panel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g){
                Graphics g2 = g.create();
                g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
                g2.dispose();
            }

            @Override
            public Dimension getPreferredSize(){
                return new Dimension(bi.getWidth()/2, bi.getHeight()/2);
                //return new Dimension(200, 200);
            }
        };

        add(panel);
    }  

        public static void main(String args[]){
            SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ImagePanel imgPanel=new ImagePanel();
                JOptionPane.showMessageDialog(
                        null, imgPanel, "Image Panel", JOptionPane.PLAIN_MESSAGE);
            }
        });
        }
}

Output

Panel

ravibagul91
  • 20,072
  • 5
  • 36
  • 59