music_est_vie 0 Newbie Poster

Hello. I am a extreme n00b at programming in Java and was wonder if I could get some advice on how to execute some of basic commands. I am working on a group project in school (Computer Science 30) and my group is creating our own extreme basic version of Sims (all commands are buttons, besides adding cheats later - no user input). I have looked online and could only find complicated solutions to some basic features I need to implement. I Would like to know how to:

* Set a image as the background image
* Be able to centre text and buttons
* Have the JButtons, JLabels and shapes match the screen size if the user manually changes the screen after the program has been opened
* Transition from file to file without having the program make a new window

pls help me.

This is one file:

package realLifeGame;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.event.*;
import javax.imageio.*;

public class StartScreeen extends JFrame implements ActionListener{

    // Container
    Container cont = getContentPane();
    
    // Buttons
    JButton start = new JButton("Start"); // start Game
    JButton quit = new JButton("Quit"); // Quit game
    JButton option = new JButton("Options"); // opens options menu
    JButton change = new JButton("Change Title"); // Change title screen
    JButton back = new JButton("Back"); // return to previous screen
    JButton game = new JButton("Start Game"); // run another file

    // Label
    String titleText = "Real Life Game!";
    JLabel title = new JLabel(titleText); // Title text


    // Constructor
    public StartScreeen()
    {

        super("Real Life Game"); // Title in the tittle bar on top of screen
        setSize (900,800); // Screen size
        setVisible(true); // Show the screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close window when user clicks on tiny 'x'

       

        // Default container details
       cont.setLayout(null);
       setContentPane(cont);
       cont.setBackground(Color.CYAN);

       // Add the buttons
       cont.add(start);
       cont.add(quit);
       cont.add(title);
       cont.add(option);
       cont.add(change);
       cont.add(back);
       cont.add(game);

       // title
       title.setFont(new Font("Purisa", Font.PLAIN, 50));
       title.setBounds(cont.getWidth()/4, cont.getY(), cont.getWidth(), cont.getHeight()/4);
       // Start button details
       start.setBounds(cont.getWidth()/3, (title.getY() + title.getHeight() + 10), 100, 50);
       start.addActionListener(this);
       start.setVisible(true);
       // Running other files
       game.setBounds(cont.getWidth()/3, (start.getY() + start.getHeight() + 10), 100, 50);
       game.setVisible(true);
       game.addActionListener(this);
       // Options button
       option.setBounds(cont.getWidth()/3, (game.getY() + game.getHeight() + 10), 100, 50);
       option.addActionListener(this);
       option.setVisible(true);
       // quit button details
       quit.setBounds(cont.getWidth()/3, (option.getY() + option.getHeight() + 10), 100, 50);
       quit.addActionListener(this);
       quit.setVisible(true);

       // Change title
       change.setVisible(false); // don't show button till option is clicked
       change.setBounds(cont.getWidth()/3, (title.getY() + title.getHeight() + 10), 125, 50);
       change.addActionListener(this);
       // Return screen
       back.setVisible(false);
       back.setBounds(cont.getWidth()/3, (change.getY() + change.getHeight() + 10), 100, 50);
       back.addActionListener(this);
}


   /*
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.LIGHT_GRAY);
        URL url = this.getClass().getResource("city.jpg");
        Image img = Toolkit.getDefaultToolkit().getImage(url);
        g.drawImage(img, 900, 800, this);
    }
    */

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == start)
        {
            start.setBackground(Color.RED);
        }
        else if (e.getSource() == quit)
        {
            System.exit(0); // Close window
        }
        else if (e.getSource() == option)
        {
            back.setVisible(true);
            change.setVisible(true);
            start.setVisible(false);
            quit.setVisible(false);
            option.setVisible(false);
            game.setVisible(false);
        } else if (e.getSource() == change)
        {
            title.setText(titleText = JOptionPane.showInputDialog("What is the new title of the game:"));
            title.setBounds(cont.getWidth()/7, cont.getY(), cont.getWidth(), cont.getHeight()/4);
        } else if (e.getSource() == back)
        {
            back.setVisible(false);
            change.setVisible(false);
            start.setVisible(true);
            quit.setVisible(true);
            game.setVisible(true);
            option.setVisible(true);
        } else if (e.getSource() == game)
        {
            new GamePlay();
        }
    }

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

}

and here is the other file:

package realLifeGame;

import javax.swing.*; // JFrames
import javax.swing.event.*; // extra content
import java.awt.*; // Graphics
import java.awt.event.*; // Extra graphics details
import java.util.*; // User input
import java.io.*; // Using additional files (wav, jpg, bmp)
import java.net.*; // Needed for URL's

public class GamePlay extends JFrame implements ActionListener{

    Container cont = getContentPane();

    // Constructor
    public GamePlay()
    {
        super("Real Life Game");
        setSize(900, 800);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        cont.setBackground(Color.CYAN);
    }

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

        g.setColor(Color.pink);
        Rectangle rect = new Rectangle(cont.getX(), cont.getY(), cont.getWidth()/5, cont.getHeight());
        g.fillRect(rect.x, rect.y, rect.width, rect.height);

    }

    public void actionPerformed(ActionEvent e)
    {
        
    }

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


   
}

All I would need is a solution and probably just an example code to understand the concept. THX

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.