How to draw an image on screen in Java?

Please help draw my image on screen.  Below is the code I have been struggling with only one module.  I cannot get the image draw to screen.  Can you please help me.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawImageToScreen {
   
    public static void main(String[] args) {
   
        // TODO code application logic here
        JFrame frame= new JFrame("JavaTutorial.net");      
      //frame.getContentPane().add(new AsycudaWorld());
        Image image = Toolkit.getDefaultToolkit().getImage("TestingInternetConnection.jpg");
        Graphics g = null;
        g.drawString("Hello to JavaTutorial.net", 10, 10);
        //g.drawImage(image, 0, 0, frame);

      frame.setSize(800, 600);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setResizable(false);
   
    }
}
Vanavah EdwardsAsked:
Who is Participating?
 
Abdellatif ELFAKERCommented:
Here a more structured code :
public class DrawImageToScreen {
	private JPanel contentPane;
	private JFrame frame;
	private static final String IMGPATH = "src/images/TestingInternetConnection.jpg";


	public DrawImageToScreen() {
		super();
		contentPane = new JPanel();
		frame= new JFrame("JavaTutorial.net");
		frame.setContentPane(contentPane);

		frame.setSize(800, 600);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);


		BufferedImage image=null;
		try {
			image = ImageIO.read(new File(IMGPATH));
		} catch (IOException e) {
			e.printStackTrace();
		}

		Graphics g = contentPane.getGraphics();

		g.drawString("Hello to JavaTutorial.net", 10, 10);

		g.drawImage(image, 0, 20, frame);
	}


	public static void main(String[] args) {

		new DrawImageToScreen();

	}
}

Open in new window

1
 
CEHJCommented:
You need to take a basic "Java custom painting" tutorial - your approach is wrong.
0
 
Vanavah EdwardsAuthor Commented:
Can you please recommend one that is very basic that I can build on?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Vanavah EdwardsAuthor Commented:
I tried the following one.  IBut it would not print the image in the JFrame.  Can you please help me?

public class DrawImage extends JPanel {

    public void paint(Graphics g){
            
      Image image = Toolkit.getDefaultToolkit().getImage("TestingInternetConnection.jpg");
        g.drawImage(image, 10, 10, this);
    }
   
    public static void main(String[] args) {
   
        // TODO code application logic here
        JFrame frame= new JFrame("JavaTutorial.net");      
      frame.getContentPane().add(new AsycudaWorld());
            frame.setSize(600, 400);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
               
   }            
}
0
 
Vanavah EdwardsAuthor Commented:
I tried all types of examples and nothing works.  This is the best solution.  This expert is very good.
0
 
Vanavah EdwardsAuthor Commented:
Thank you so much for that solution.  I don't even mind paying something for your time.

I want to put about 3 gif images in sequence on top of the image so as to display some helpful information to the user.  Using the solution with the same image you provided above, can you please tell me how to draw another image (gif image) on top of this one and how to remove it.
0
 
Vanavah EdwardsAuthor Commented:
Hello I got it done using your same code with a few modifications.  However, the gif file on top to the image is flat.  It is not animated.  How can I fix this problem?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.