Java Importing Images to a Java frame

  • Thread starter Thread starter Quantum_Prodegy
  • Start date Start date
  • Tags Tags
    Frame Images Java
Click For Summary
To import an image as a background in a Java frame, it is recommended to create a custom panel or canvas that overrides the paint method. This allows for the use of the drawImage method within the Graphics object to display the image. Alternatively, using an ImageIcon can also achieve the desired effect. This approach enhances the visual appeal of the frame while maintaining the simplicity of the program.
Quantum_Prodegy
[SOLVED] Importing Images to a Java frame

Hey guys,

I have a very simple java program. I opens a frame, and I want to be able to put a picture in the frame. How do I import a picture and set it as the background to the frame?

Here is my simple program:

import java.io.*;
import java.awt.*;
import java.awt.event.*;

class demo extends Frame
{
void setup()
{
setLayout(new FlowLayout());

Button ok = new Button(" OK ");

add(ok);
}
}

public class window
{
public static void main (String[] args)
{
int width = 700, height = 134;

demo frm = new demo();

frm.setup();
frm.setBackground(Color.gray);
frm.setTitle("Logon Message");
frm.setSize(width, height);
frm.setVisible(true);
}
}

so as you can see it is an extremely basic window, and I would like to import a graphic as the background to the window within the setup() method.

Any help much appreciated, thanks!
-Jon
 
Technology news on Phys.org
somebody must know this
 
Create a Panel and call the drawImage method in its Graphics object.
 
Use a Canvas with an overridden paint() method, or an ImageIcon.

- Warren
 
:thumbsup: thanks guys
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 0 ·
Replies
0
Views
627
  • · Replies 0 ·
Replies
0
Views
657
Replies
1
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 14 ·
Replies
14
Views
6K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
3
Views
3K