Gregory Desrosiers Montreal

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 30 May 2013

JAVA Program: Random 0s and 1s Table with Refresh Button

Posted on 17:36 by Unknown
Hello, everyone. Got a new program I want to share with you guys that demonstrates the power of listening to an interactive button.

This program is a stand-alone GUI program where what you see is a 10 by 10 table of 0s and 1s, with a clickable "Refresh" button at the bottom. If you click on it, it randomizes all the values in the tables, changing them to 0s or 1s.

I'm going to share you the code that unless you read the explanations at the bottom, you can figure out for yourselves on how this actually works. There's a description at the top of the writing for your own convenience. Enjoy!


/*
 * Data Structures and Object-Oriented Programming - Assignment 7
 * Question 16.40 of Introduction to Java Programming (by Daniel Liang)
 * 
 *      Display Random 0 or 1 with Refresh Button
 *  
 *      Same mechanics as Question 12.14, except that a refresh button is included at
 *      the bottom of the window, and every time it is clicked, a new 10 by 10
 *      table of randomized 0s and 1s appear. 
 *  
 *      To accomplish this, the actionPerformed method for the refresh button executes 
 *      a loop that invokes the setText method for all the elements in the JLabel array, 
 *      and passes the string value of the value returned by a java.util.Random object's 
 *      nextInt method.
 * 
 * Programmed by Gregory Desrosiers
 * 
 * Start Date: March 23, 2013
 * End Date: March 23, 2013
 * 
 * File Name: Chapter16Exercise40.java
 * Teacher: Amin Ranj Bar
 * 
 * SPECIAL: The ActionListener for the Refresh button is an anoymonous inner class.
 */

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class Chapter16Exercise40 extends JFrame
{
private static final long serialVersionUID = -4480542370452207354L;

private JPanel mainPanel;

// Top Panel Components
private JPanel topPanel;
private JLabel[] labels;

// Bottom Panel Components
private JPanel bottomPanel;
private JButton refreshButton;

public static void main(String[] args)
{
Chapter16Exercise40 frame = new Chapter16Exercise40();

frame.setTitle("Exercise16_40");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);

frame.pack();
frame.setVisible(true);
}

public Chapter16Exercise40()
{
mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());

// Top Panel Construction

topPanel = new JPanel();
topPanel.setLayout(new GridLayout(10, 10, 16, 2));
topPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
topPanel.setBackground(Color.BLUE);

Random binaryGenerator = new Random();
labels = new JLabel[100];

for (int x = 0; x < labels.length; x++)
{
labels[x] = new JLabel(String.valueOf(binaryGenerator.nextInt(2)));
labels[x].setHorizontalTextPosition(AbstractButton.CENTER);
labels[x].setForeground(Color.WHITE);
labels[x].setFont(new Font("Times New Roman", Font.PLAIN, 12));

topPanel.add(labels[x]);
}

mainPanel.add(topPanel, BorderLayout.NORTH);

// Bottom Panel Construction

bottomPanel = new JPanel();
bottomPanel.setBackground(Color.BLACK);

refreshButton = new JButton("Refresh");
refreshButton.setBackground(Color.WHITE);
refreshButton.setForeground(Color.BLACK);
refreshButton.setFont(new Font("Calibri", Font.BOLD, 12));

refreshButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)
{
for (int y = 0; y < labels.length; y++)
labels[y].setText(String.valueOf(new         
                                                          Random().nextInt(2)));
}
});

bottomPanel.add(refreshButton);
mainPanel.add(bottomPanel, BorderLayout.CENTER);

add(mainPanel);
}
}
If you guys have any questions, comments, or suggestions, please post them right here! I am willing to accept them! Anyway, please follow me on Facebook, Twitter, and Google+! Facebook: https://www.facebook.com/pages/Gregory-Desrosiers/171954446270382 Twitter: http://www.twitter.com/GregoryDes
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • The Quebec Ministerial Exam of College English (English Exit Exam)
    Hello, everyone. How you doing? I'm good. There is some unfortunate news for me to deliver, though I am planning to try to work myself a...
  • Computer Game Collection
    Hello, people! Before I happen to leave this very foundation of Champlain College Saint-Lambert (that's where I'm learning to get wh...
  • String Letter Counter (JAVA Program)
    Hello, guys. This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string strai...
  • DOS Games to Play
    Hello, people. If there's one thing that I want to do when I have lots of money, it's to play full version DOS Games on my Windows P...
  • JAVA Program: Command-based String Remover in Text Files
    Hello, everyone. Let me share with you the code on one of the programs I did back in March 2013, while doing my Data Structures class. This ...
  • The Black Glove [Short Poem by me]
    This is a poem written by me back in April 2010. I'm not sure if this was for an assignment, but this is what I got. It may not be good ...
  • Fundraising Project (To Autism Society Canada)
    Hi, everyone! How are you people doing? I am feeling tired and stuff, but there are a couple of things I want to bring up with you. Tomorrow...
  • Best VGM Series
    Hello, guys. I want to present to you a series of YouTube imports of video game music, with some of them already recorded but in a form that...
  • Princess Peach In India [Story Idea]
    Here's a really fancy story I have made, although it is more of an initial idea and comes from a visual I had more than two years ago. E...
  • Super Mario Script 1 (v 2.3) [Super Mario Galaxy font, with a few photos]
    Hello there. Do you recognize the font of this text? I'll give you two clues. The one below is the a sample sentence of different sizes....

Blog Archive

  • ▼  2013 (38)
    • ►  August (2)
    • ►  July (9)
    • ►  June (12)
    • ▼  May (12)
      • JAVA Program: Number Loop (Experimental Program of...
      • JAVA Program: Displaying Unlimited Strings on a GU...
      • JAVA Program: Command-based String Remover in Text...
      • Arcade Games to check out
      • JAVA Program: Random 0s and 1s Table with Refresh ...
      • JAVA Program: Object-Oriented Programming Class, t...
      • Announcement on "The Asperger Computer"
      • JAVA Program : Hello World!
      • Introducing myself (original blog on Autism Suppor...
      • Demonstration of JAVA Classes
      • Non-fiction e-book, The Asperger Computer, reveals...
      • The Asperger Computer - Facebook Edition
    • ►  April (2)
    • ►  January (1)
  • ►  2012 (24)
    • ►  December (13)
    • ►  November (3)
    • ►  October (5)
    • ►  September (1)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile