1

I'm having a problem putting multiple images on a JFrame. I already added a picture on the JFrame as the primary background. But when I try to put another image for the logo of my program, it doesn't show up. Can someone please help me? thanks.

P.S. I'm using the Container class in my JFrame.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
import java.io.*;
import java.awt.Container;

public class logIn extends JFrame
{

    Random rand = new Random();
int n2 = (int) (1+Math.random()*255);
int n1 = rand.nextInt(n2);
int n3 = rand.nextInt(n2);
int n4 = rand.nextInt(n2);
Color color = new Color(n1,n3,n4);
JLabel image = new JLabel (new ImageIcon("space2.png"));
//JLabel image2 = new JLabel (new ImageIcon("login.png"));
JLabel userName = new JLabel("Username");
JLabel passWord = new JLabel("Password");
JTextField user = new JTextField(10);
JTextField pass = new JTextField(10);

JLabel myDog = new JLabel(new ImageIcon("space.jpeg"));


public static void main (String args[])
{
    new logIn();
}

public logIn()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = getContentPane();
    c.setLayout(null);
    c.add(image);
    image.setBounds(0,0,1366,768);
    JLabel image2 = new JLabel (new ImageIcon("login.png"));
    c.add(image2);
    image2.setBounds(2000,2000,2000,2000);
    //c.add(image2);
    //image2.setBounds(10,10,250,250);




    //c.add(userName);
    //userName.setLayout(null);
    //userName.setBounds(50,100,100,50);

    setVisible(true);
    setSize(1366,768);

    setLayout(new BorderLayout());
    add(myDog);
    myDog.setLayout(null);
}

public void paint (Graphics g)
{
    Image a = Toolkit.getDefaultToolkit().getImage("login.png");
    g.drawImage(a,0,0,1366,768,this);
    super.paint(g);
    setVisible(true);
}

}

user3226012
  • 1
  • 1
  • 4
  • And details of your problem. Right now you have asked your question as if we can see what your goal is, can see how it's not working, and can see code not shown, and so it's anyone's guess what the heck you're really trying to do and how it's not working. – Hovercraft Full Of Eels Jan 29 '14 at 13:20
  • Ok. I'll put it in the answers. – user3226012 Jan 29 '14 at 13:23
  • 1
    "in the answers"? No, please update your question by editing it. Only post actual *answers* in the answers section. – Hovercraft Full Of Eels Jan 29 '14 at 13:24
  • I already uploaded the code. Sorry, I'm new here. – user3226012 Jan 29 '14 at 13:29
  • 1
    You can use multiple `JPanel` objects in your layout and insert an image in each of them. – Bruno Toffolo Jan 29 '14 at 13:32
  • I don't know JPanel yet. Our professor haven't discussed it yet. Right now, we're still on JFrame and JMenuBar.. – user3226012 Jan 29 '14 at 13:33
  • You are allowed to learn things that your professor hasn't yet shown you, and in fact, you're likely to learn things better, that which you've taught yourself. Please have a look at the [JPanel Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/panel.html) and the [Layout Manager Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – Hovercraft Full Of Eels Jan 29 '14 at 15:27
  • Two related examples are seen [here](http://stackoverflow.com/q/15961412/230513). – trashgod Jan 29 '14 at 17:42
  • Thanks for the help. I already figured it out. :) – user3226012 Jan 29 '14 at 17:49

2 Answers2

0

not sure what you're trying to achieve, but if you want to have multiple images (like a key image besides the password field and an avatar besides the user name, etc) you could create multiple JLabels with the different images and add them to the panel. If this is your purpose, don't override paint.

Janos
  • 1,987
  • 3
  • 17
  • 24
0

You need to read a few tutorials on custom painting, layouts, and gui development.

If you want to paint an image you should not load the image every repaint. You need to read that image in ahead of time. You should not be calling set visible in this method either.

Any custom painting needs to be done in paintComponent instead of paint.

Read this: http://docs.oracle.com/javase/tutorial/uiswing/painting/

It sounds like what you really want to do is create a JFrame with a panel that has a background image - and then use swing and layouts to add other components or panels with images on top of that.

So you would have a JPanel as the content pane with paintComponent overridden to only paint an image. Then build new components from there and place them using a layout manager.

Dodd10x
  • 3,344
  • 1
  • 18
  • 27