0

So I am currently making a login screen that has a cool looking background effect made using the Graphics object and a 'game loop'. When I add in a JTextField though, it is seen underneath everything and not above. Is there anyway to to make the graphics draw underneath all components inside of the JFrame?

Here is an image of the graphics:

enter image description here

The text field is there, just underneath everything being drawn to the surface of the frame. I want to somehow reorder this so it draws underneath components.

Here is my current frame code:

    frame = new JFrame("Login");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.getContentPane().setPreferredSize(new Dimension(450, 200));
    frame.getContentPane().setBackground(Color.black);
    frame.setBackground(Color.black);
    frame.setLayout(new FlowLayout());

    JTextField user = new JTextField(20);
    user.setLocation(100, 200);
    user.setVisible(true);
    frame.add(user);

    frame.pack();
    frame.setVisible(true);

    frame.createBufferStrategy(2);
    buff = frame.getBufferStrategy();

    Painter painter = new Painter();
    frame.add(painter);

Any help please?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91
  • 1
    `user.setLocation(100, 200);` Don't do that, use layouts (+ size hints in constructors, layout padding & borders) for sizing and positioning. – Andrew Thompson Jun 20 '12 at 07:17

1 Answers1

5

AnimationTest shows one approach. It overrides paintComponent() and invokes super.paintComponent() to ensure that components are rendered atop the background. Click anywhere to position a text field; resize to see how the default FlowLayout works. JPanel is double buffered by default using the existing buffer strategy.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    See also [this example](http://stackoverflow.com/a/10836833/418556) for a panel with an animated BG (GIF), as well as the accepted answer on the same question - for the best way to load it. ;) – Andrew Thompson Jun 20 '12 at 07:15