Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6976211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:28:03+00:00 2026-05-27T17:28:03+00:00

I am trying to create a game where I have a background image and

  • 0

I am trying to create a game where I have a background image and text on top of that in the form of JLabels. How would I go about doing that?

The main reason I’m doing this is so that I can have 2 different text areas with different font sizes. Using g.drawString() will only let you use 1 text size for the whole thing.

Here is my code so far:

package com.cgp.buildtown;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Intro extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private BufferedImage bg;
    private Font font;

    public Intro() {
        super();
        loadImages();
        setFont(loadFont(50f));
    }

    private Font loadFont(Float f) {
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, new File("res/komikatext.ttf"));
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            ge.registerFont(font);
        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }
        return font.deriveFont(f);
    }

    private void loadImages() {
        try {
            bg = ImageIO.read(new File("res/introbg.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while(true) {
            repaint();
        }
    }

    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.drawImage(bg, 0, 0, null);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:28:04+00:00Added an answer on May 27, 2026 at 5:28 pm

    Here is one way.

    Intro. screen

    But of course that is merely a version hacked out of your own code/ specs. For a much better implementation of the same idea, see this Background Panel.

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    import java.net.URL;
    import javax.imageio.ImageIO;
    
    public class Intro extends JPanel implements Runnable {
        private static final long serialVersionUID = 1L;
        private Thread thread;
        private BufferedImage bg;
        private String html =
            "<html><body style='color: yellow;'>" +
            "<h1>Game</h1>" +
            "<p>Welcome to the Game!";
    
        public Intro() {
            super();
            loadImages();
            setLayout(new BorderLayout());
            setBorder(new EmptyBorder(40,40,40,40));
            add(new JLabel(html), BorderLayout.NORTH);
            add(new JTextField("..enter name"), BorderLayout.SOUTH);
        }
    
        private void loadImages() {
            try {
                URL url = new URL("http://pscode.org/media/stromlo2.jpg");
                bg = ImageIO.read(url);
                setPreferredSize(new Dimension(bg.getWidth(), bg.getHeight()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void addNotify() {
            super.addNotify();
            thread = new Thread(this);
            thread.start();
        }
    
        public void run() {
            while(true) {
                repaint();
            }
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
            g.drawImage(bg, 0, 0, this);
        }
    
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null, new Intro());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a pacman-like game. I have an array that looks
I'm trying to create a 'game paused' screen that overlays on top of my
I am trying to create a game and have run into what is probably
I'm trying to create a PHP based game where you have to do all
I'm trying to create a sudoku game, for those that do not know what
I have been trying to create a 2D array of vector for a game.
I have been experimenting with PulpCore, trying to create my own tower defence game
I am trying to create my first game for android. I currently have a
I am trying to create a game, where the user will see an image,
I am trying to create a Game so that I can change its data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.