Java form elements not showing up fully until activated i.e. clicked on

I’ve been trying to add Java form elements such as a textField and a passwordField to a fullscreen exclusive mode frame. The issue i’ve been having is that when I run the code, my form elements don’t show up fully or not at all until I actually click on them.

Here’s my code:

Master.java

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Master extends JFrame {

    private static final long serialVersionUID = -4927941474660261348L;
    static GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    static GraphicsDevice gd = ge.getDefaultScreenDevice();
    static int WIDTH = gd.getDisplayMode().getWidth();
    static int HEIGHT = gd.getDisplayMode().getHeight();
    static Toolkit toolkit = Toolkit.getDefaultToolkit();
    static Graphics2D g2d = null;
    static FontMetrics metrics = null;
    static URL vignetteURL = null;
    static Image vignette = null;
    static Rectangle red = new Rectangle(WIDTH - 35, 0, 35, 35);

    public Master() {
        super("Project Zenith");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        getContentPane().setLayout(null);
        setUndecorated(true);
        setResizable(false);
        gd.setFullScreenWindow(this);
        repaint();

        setIconImage(new ImageIcon(getClass().getResource("images/hex.png")).getImage());
        vignetteURL = getClass().getResource("images/vignette2.png");

        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getX() >= red.getX() && e.getY() <= red.getHeight()) {
                    closeClicked();
                }
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {

            }

            @Override
            public void mouseExited(MouseEvent arg0) {

            }

            @Override
            public void mousePressed(MouseEvent arg0) {

            }

            @Override
            public void mouseReleased(MouseEvent arg0) {

            }           
        });
    }

    public void update(Graphics g) {
        paint(g);
    }

    public void paint(Graphics g) {
        g2d = (Graphics2D) g;
        vignette = toolkit.getImage(vignetteURL);
        g2d.clearRect(0, 0, getWidth(), getHeight());
        g2d.setFont(new Font("Bebas Neue", Font.PLAIN, 25));
        metrics = g2d.getFontMetrics();
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setColor(new Color(168, 168, 168, 200));
        g2d.drawString("Project Zenith", 10, HEIGHT - 10);
        g2d.setColor(new Color(0xFFA6A6));
        g2d.fill(red);
        g2d.drawImage(vignette, 0, 0, WIDTH, HEIGHT, this); 

    }

    public void closeClicked() {
        System.exit(0);
    }
}

Login.java

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login extends Master {
    private static final long serialVersionUID = 1L;
    static Master l;
    static JTextField username;
    static JTextField password;
    static Graphics2D g2d = null;
    static FontMetrics metrics = null;
    static RoundRectangle2D loginRect = new RoundRectangle2D.Float((WIDTH / 2) - ((WIDTH / 4) / 2), (HEIGHT / 2) - ((HEIGHT / 4) / 2), (WIDTH / 4), (HEIGHT / 4), 20, 20);

    Login() {
        l = this;
        Font inputFont = new Font("Calibri", Font.PLAIN, 15);

        username = new JTextField();
        username.setBounds((WIDTH / 2) - (((int) loginRect.getY() - 10) / 2), (int) loginRect.getY() + 60, (int) loginRect.getY() - 10, 18);
        username.setFont(inputFont);
        username.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        username.setEditable(true);
        username.setBackground(new Color(0xE6E6E6));
        //username.setVisible(true);
        getContentPane().add(username);

        password = new JPasswordField();
        password.setBounds((WIDTH / 2) - (((int) loginRect.getY() - 10) / 2), (int) loginRect.getY() + 110, (int) loginRect.getY() - 10, 18);
        password.setFont(inputFont);
        password.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        password.setEditable(true);
        password.setBackground(new Color(0xE6E6E6));
        //password.setVisible(true);
        getContentPane().add(password);

        JButton loginButton = new JButton("Login");
        loginButton.setBounds((WIDTH / 2) - 40, (int) loginRect.getY() + 155, 80, 20);
        loginButton.setFont(inputFont);
        loginButton.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        loginButton.setBackground(new Color(0xE6E6E6));
        //loginButton.setVisible(true);
        loginButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(username.getText());
            }
        });
        getContentPane().add(loginButton);
        repaint();
        setVisible(true);

    }

    public void paint(Graphics g) {
        super.paint(g);
        g2d = (Graphics2D) g;
        metrics = g2d.getFontMetrics();
        g2d.setColor(Color.WHITE);
        g2d.fill(loginRect);
        g2d.setColor(new Color(0xA8A8A8));
        g2d.drawString("Access Terminal", (Master.WIDTH / 2) - (metrics.stringWidth("Access Terminal") / 2), (int) loginRect.getY() + metrics.getHeight() + 2);
    }

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


Source: forms

Leave a Reply