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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.