0

I'm working on a login screen for my game. I have a total of two images on it. One is a splash screenshot and the other is the background image. I'm using BufferedImages to render the images to the screen.

The problem I get is that when I add a standard button to the Canvas, the button takes up the whole window, and evidently, I don't want that.

I would post a picture, but alas, I do not have "enough reputation" to do that. Here's a look at my code though:

import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.swing.JFrame;
import javax.swing.UIManager;

public class Login extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

private static final int WIDTH = 495;
private static final int HEIGHT = 307;
private static final int SCALE = 2;
private final Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

private BufferedImage splash = new BufferedImage(315, 177, BufferedImage.TYPE_INT_RGB);
public int[] splashPixels = ((DataBufferInt) splash.getRaster().getDataBuffer()).getData();

private Thread thread;
public static boolean isRunning = false;

JFrame frame;
MainMenu menu;
Splash splashscreen;

Button login;
Button register;
TextField username;

private Login() {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception exc) {
        exc.printStackTrace();
    }

    frame = new JFrame("Game Login");
    menu = new MainMenu(WIDTH, HEIGHT, "/login/login_screen.png");
    splashscreen = new Splash(315, 177, "/login/splash.png");

    frame.setSize(size);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);

    setPreferredSize(size);
    frame.add(this);

    login = new Button("Login");
    login.setBounds(0, 0, getWidth(), getHeight());

    frame.add(login);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private void begin() {
    createBufferStrategy(2);
    thread = new Thread(this);
    thread.start();
    isRunning = true;
}

private void finish() throws InterruptedException {
    isRunning = false;
    thread.join();
}

private void updateLogin() {
    for (int a = 0; a < pixels.length; a++) {
        pixels[a] = menu.pixels[a];
    }
}

private void renderLogin() {
    BufferStrategy buffer = getBufferStrategy();
    Graphics gfx = buffer.getDrawGraphics();

    for (int a = 0; a < splashPixels.length; a++) {
        splashPixels[a] = splashscreen.splashPixels[a];
    }

    gfx.drawImage(image, 0, 0, getWidth(), getHeight(), null);
    gfx.drawImage(splash, 320, 37, 625, 340, null);

    gfx.setColor(Color.WHITE);
    gfx.drawString("Game Co © 2013", 3, (getHeight() - 4));
    gfx.drawString("\"Game\" is a trademark of Blah-Blah-Blah.", (getWidth() - 268), (getHeight() - 3));

    gfx.dispose();
    buffer.show();
}

public void run() {
    while (isRunning == true) {
        updateLogin();
        renderLogin();
    }

    try {
        finish();
    } catch (InterruptedException exc) {
        exc.printStackTrace();
    }
}

public static void main(String[] args) {
    Login login = new Login();
    login.begin();
}
}

Once again, my only problem is that I keep getting a enlarged button.

Thanks in advance, I know you guys are busy and whatnot and I appreciate taking the time to look over and help answer my questions.

P.S. Does anyone know how to make a password field with AWT? I'll also need that too. ;)

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Why are you mixing AWT with Swing? This makes no sense and forces you to unnecessarily limit what you can do. Why not instead just stick with all Swing components? – Hovercraft Full Of Eels Oct 13 '13 at 19:20

2 Answers2

1

You state:

The problem I get is that when I add a standard button to the Canvas, the button takes up the whole window, and evidently, I don't want that.

You're trying to add a component directly to a container that is using BorderLayout, likely the contentPane of a top-level window, and so by default it is added BorderLayout.CENTER and fills the container.

Solution: add your JButton (again use Swing components) First to a JPanel (which uses FlowLayout by default), and then add that to the top level window.

Again, there's no need to mix AWT and Swing components and there are in fact strong arguments not to do so. I suggest that you stick with all Swing components for your GUI.


Does anyone know how to make a password field with AWT? I'll also need that too. ;)

Again, don't use AWT but instead use a Swing JPasswordField.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • But how do I work with adding buffer strategies and buffered images to the JPanel? I'm not good with JPanels, I'm much better with Canvas, though. – Payneztastic Oct 13 '13 at 19:29
  • @Payneztastic: you usually don't have to worry about buffer strategies since JPanels and all Swing components are double buffered by default. But regardless, you should learn how to do Swing graphics/GUI. Move on up out of a 1995 GUI library and into 2003 graphics library. It's not cutting edge, but Swing works a lot better than AWT. – Hovercraft Full Of Eels Oct 13 '13 at 19:33
  • JPanelis double buffered by default. You can simply create two or more BufferedImages of your own, drawing to one, rendering the other and switch them as you need – MadProgrammer Oct 13 '13 at 19:34
  • And then, how would I do that? :/ – Payneztastic Oct 13 '13 at 19:41
1

Solution: add your JButton (again use Swing components) First to a JPanel (which uses FlowLayout by default), and then add that to the top level window.

You could just change the layout manager for your frame to a FlowLayout so it will behave like a JPanel.

frame.setLayout(new FlowLayout());
chasep255
  • 11,745
  • 8
  • 58
  • 115