Results 1 to 3 of 3
  1. #1
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Image Randomizer Project

    It seems like a completely different process, maybe I am wrong. My final project is when the user presses a button, a random picture from that folder appears I thought of approaching it two ways, but unsure how to really apply it.

    • Assign all images to an INT value 1,2,3, etc.

    OR
    • Have the program scan the folder and randomly assign INT and then display the image


    Even if I assigned each image, do I still have to scan the folder with the File methods? Do I use an array? I really wish I thought in much simpler terms. Please guide me to a resource or something that would guide me. Oh wise people of the forums!

  2. #2
    itkremit is offline Member
    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    Default Re: Image Randomizer Project

    I didn't do any of this it seems. Here is a rough draft of an unfinished program. It runs and works, just looks ugly :)

    I really think I need to see a psychologist.

    Java Code:
    //needed packages
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
    
    
    //class name
    public class RandomizeMe extends JFrame{
    //field names
    private JPanel characterBoxPanel; //hold selection
    private JPanel characterImagePanel; //hold selection
    private JTextField characterBox; //Combobox
    private JButton randomButton; //Select a random image
    private JLabel characterLabel; //holding picture selection based on combobox
    private JLabel selectedCharacter; //holding selection by combobox
    private JLabel label; // Display a message
    //private String dir = "/images";
    
    
    //GUI setup
    public RandomizeMe(){
    setTitle("Image Viewer");
    setLayout(new BorderLayout());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buildCharacterImagePanel();
    buildCharacterBoxPanel();
    add(characterLabel, BorderLayout.NORTH);
    add(label, BorderLayout.CENTER);
    add(randomButton, BorderLayout.SOUTH);
    pack();
    setVisible(true);
    
    /*
    //Set Folder check as a throw 
    File file = new File(dir);
    if(!file.exists()){
    JOptionPane.showMessageDialog(null,"The folder images is not found.");
    System.exit(0);}
    */
    
    }
    
    
    //Image Display
    private void buildCharacterImagePanel(){
    characterImagePanel = new JPanel();
    characterLabel = new JLabel();
    characterImagePanel.add(characterLabel);}
    
    //Random Button and Selected text
    private void buildCharacterBoxPanel(){
    characterBoxPanel = new JPanel();
    label = new JLabel("Your Character: ");
    characterBox = new JTextField(10);
    characterBox.setEditable(false);
    randomButton = new JButton("Randomize Me!");
    randomButton.addActionListener(new ButtonListener());
    characterBoxPanel.add(label);
    characterBoxPanel.add(randomButton);
    }
    
    //event when button is clicked
    private class ButtonListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
    
    //Read directory and pull image from another class
    Random generate = new Random();
    int number = generate.nextInt(5);
    
    for(int i = 0; i < 5; i++){
    if(number == 1){
    ImageIcon elmoImage = new ImageIcon("Elmo.jpg");
    characterLabel.setIcon(elmoImage);}
    else if(number == 2){
    ImageIcon kermitImage = new ImageIcon("Kermit.jpg");
    characterLabel.setIcon(kermitImage);}
    else if(number == 3){
    ImageIcon groverImage = new ImageIcon("Grover.jpg");
    characterLabel.setIcon(groverImage);}
    else if(number == 4){
    ImageIcon bertImage = new ImageIcon("Bert.jpg");
    characterLabel.setIcon(bertImage);}
    else if(number ==5){
    ImageIcon ernieImage = new ImageIcon("Ernie.jpg");
    characterLabel.setIcon(ernieImage);}
    else
    break;
    
    }}}
    
    
    //main method
    public static void main(String[] args){
    new RandomizeMe();
    }}

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Image Randomizer Project

    Quote Originally Posted by itkremit View Post
    It runs and works, just looks ugly :)
    The first two are important. The third one isn't.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Randomizer
    By bakriawad in forum New To Java
    Replies: 6
    Last Post: 06-01-2013, 11:42 PM
  2. Need help adding a "randomizer" to this code.
    By bzbzb1 in forum New To Java
    Replies: 1
    Last Post: 05-15-2012, 05:49 AM
  3. Sudoku randomizer help
    By mochibon in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 03:05 AM
  4. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •