r/javahelp • • Apr 02 '22

Homework I desperately need help on this Java GUI code of displaying random images with a button, assistance please!

I need to help figuring out this code, I want to make an array of images appear randomly by a button click, and another image appear with another button click.

But there is an error, I can't figure it out:

 package m03Test;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Font;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class M03 extends JFrame {
	
	private ImageIcon Light1 = new ImageIcon(M03.class.getResource("/resources/Planet1.png"));
	private ImageIcon Light2 = new ImageIcon(M03.class.getResource("/resources/Planet2.png"));
	private ImageIcon Light3 = new ImageIcon(M03.class.getResource("/resources/Planet3.png"));
	private ImageIcon Light4 = new ImageIcon(M03.class.getResource("/resources/Planet4.png"));
	private ImageIcon Light5 = new ImageIcon(M03.class.getResource("/resources/Planet5.png"));
	private ImageIcon Light6 = new ImageIcon(M03.class.getResource("/resources/Planet6.png"));
	private ImageIcon Light7 = new ImageIcon(M03.class.getResource("/resources/Planet7.png"));
	private ImageIcon Light8 = new ImageIcon(M03.class.getResource("/resources/Planet8.png"));
	private ImageIcon Light9 = new ImageIcon(M03.class.getResource("/resources/Planet9.png"));
	
	private JLabel lblDisplay;
	
	
	private final Random rand = new Random();
	int spread = rand.nextInt(9);
	int vertify = 0;
			

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					M03 frame = new M03();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public M03() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel ImagePanel = new JPanel();
		ImagePanel.setBackground(new Color(0, 0, 139));
		contentPane.add(ImagePanel, BorderLayout.SOUTH);
		
		JButton ResetButton = createResetButton();
		ImagePanel.add(ResetButton);
		
		JButton RandomButton = createRandomButton();
		ImagePanel.add(RandomButton);
		
		JLabel lblTitle = createlblTitle();
		contentPane.add(lblTitle, BorderLayout.NORTH);
		
		lblDisplay = satellite();
		
		JLabel lblDisplay = satellite();
		contentPane.add(lblDisplay, BorderLayout.CENTER);
	}

	private JButton createRandomButton() {
		JButton RandomButton = new JButton("Random");
		RandomButton.setBackground(new Color(0, 191, 255));
		ImageIcon[] Satellite = { Light1, Light2, Light3, Light4, Light5, Light6, Light7, Light8, Light9};
		
		RandomButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do
				{
					spread = rand.nextInt(9);
					
				} while (spread == vertify);
				
				vertify = spread;
				
				lblDisplay.setIcon(Satellite[vertify]);
			}
			
		});
		
		return RandomButton;
	}

	private JLabel satellite() {
		JLabel lblDisplay = new JLabel("");
		lblDisplay.setIcon(Satellite [spread]);
		lblDisplay.setHorizontalAlignment(SwingConstants.CENTER);
		return lblDisplay;
	}

	private JButton createResetButton() {
		ImageIcon Starlight = new ImageIcon(M03.class.getResource("/resources/Solar.jpeg"));
		
		JButton ResetButton = new JButton("Solar Reset");
		ResetButton.setBackground(new Color(0, 191, 255));
		
		ResetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ResetButton.setIcon(Starlight);
			}
		});
		return ResetButton;
	}

	private JLabel createlblTitle() {
		JLabel lblTitle = new JLabel("New label");
		lblTitle.setForeground(new Color(255, 255, 0));
		lblTitle.setBackground(new Color(0, 0, 0));
		lblTitle.setFont(new Font("Verdana", Font.PLAIN, 12));
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setOpaque(true);
		return lblTitle;
	}

}
0 Upvotes

3 comments sorted by

•

u/AutoModerator Apr 02 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/wildjokers Apr 02 '22

Are we supposed to guess what the error is?

1

u/FavorableTrashpanda Apr 02 '22

So... what is the error? This is crucial.