Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program Write a program which creates a JFrame with two JLabels in

ID: 640293 • Letter: W

Question

Write a Java program

Write a program which creates a JFrame with two JLabels in it. The JFrame should be colored green (using getContentPane(). setBackground()) of size 500 by 400. One label should be blue and the other red. You need to also call setOpaque(true); on each label to make the color visible. By default, JLabels have transparent backgrounds. Now using this program to experiment, draw a sketch of the window in each of the following situations: FlowLayout GridLayout(3,1) GridLayout(3,2) BorderLayout with labels at NORTH and SOUTH BorderLayout with labels at NORTH and CENTER BorderLayout with labels at WEST and SOUTH

Explanation / Answer

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

import javax.swing.JPanel;

public class Panel extends JPanel implements Runnable {

     

    private Image dbImage;

    private Graphics dbGraphics;

    int GWidth = 500;

    int GHeight = 400;

    Dimension dim = new Dimension(GWidth, GHeight);

    private Thread game;

    private volatile boolean running = false;

    Tile tile;

    public Panel(){

         

        setPreferredSize(dim);

        setBackground(Color.black);

        setFocusable(true); // Focus on JPanel to receive key events

        requestFocus();

        tile = new Tile();

    }

    public void draw(Graphics g) {

        tile.draw(g);

    }

    public void run() {

        while (running) {

            update();

            render();

            paintScreen();

        }

    }

    private void startGame() {

        if (game == null || !running) {

            game = new Thread(this);

            game.start();

            running = true;

        }

    }

    private void update() {

        if (game != null && running == true) {

        }

    }

    private void render() {

        if (dbImage == null) {

            dbImage = createImage(GWidth, GHeight);

        if (dbImage == null) {

            System.err.println("error");

            return;

            } else {

            dbGraphics = dbImage.getGraphics();

            }

        }

         

        dbGraphics.setColor(Color.WHITE);

        dbGraphics.fillRect(0, 0, GWidth, GHeight);

        draw(dbGraphics);

    }

    private void paintScreen() {

        Graphics g;

        try {

            g = this.getGraphics();

            if (dbImage != null && g != null) {

                g.drawImage(dbImage, 0, 0, null);

            }

        } catch (Exception e) {

            System.err.println(e);

        }

    }

    public void stopGame() {

        if (running) {

            running = false;

        }

    }

    public void addNotify() {

        super.addNotify();

        startGame();

    }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote