Results 1 to 2 of 2

Thread: Help with image display on JPanel

  1. #1
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Help with image display on JPanel

    Hello guys! This is my first thread and my first time taking part in the world of forums. Anyway I started my intro to Java class at a local CC here and I would be lying if I said it has been on of the easiest computer science classes I have taken. Again anyway, I have a final project due where I need to create a game that contains an animation with sound. So I have a game in the taste of Price is Right where the user needs to guess the price of 1 of 3 prizes that are generated using the random method in java.util.Random. I have the logic down. It's where the animations comes in that I'm stuck. The user has 60 second to get the "price right". So in a JPanel I'm trying to cycle thru images of numbers (60 - 0) but I'm having trouble just trying to display one! I know that I need to create an array of String type for the file names and need a ActionListener with a Timer type to calculate when a second goes by and then use the repaint method to repaint it but I'm not sure what I am doing wrong. I tried using Javadocs but not much help there. Here's a copy of the code: (Any help would be grealty appriciated!!)

    Main---------------------------------------------

    package assignment_07;
    import java.applet.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import java.io.*;
    import java.text.DecimalFormat;
    import javax.swing.*;
    public class Main
    {

    public Main()
    {

    }

    public static void main(String[] args)
    {
    JFrame frame = new JFrame ("Guess The Price & Take the Prize");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.getContentPane().add(new GuessingPanel());
    frame.pack();
    frame.setVisible(true);

    /*
    DecimalFormat fmt = new DecimalFormat("##.00");
    Random rdmNum = new Random();
    String Num,Guess, Temp, message,tab=" ";
    int num, price, guess, temp;

    message =tab+tab+"Guess The Price\n"+tab+tab+" &\n"+tab+tab+"Take The Prize\n\n"+
    "Rules:\n"+
    "1. Enter the number of prize you would like to win.\n"+
    "2. You have 60 seconds to guess the dollar amount\n"+
    "of that prize. (No cents included)\n"+
    "3. You will get a hint to guess either higher or lower\n"+
    "after your first guess\n\n"+
    "(Hint: The bigger the prize the harder to guess its price.)\n\n\n" +
    tab+tab+"GOOD LUCK!!";

    JOptionPane.showMessageDialog (null,message);

    Num = JOptionPane.showInputDialog ("Please enter the number for" +
    " the prize you would would want to win: " +
    "\n1. Color Television \n2. Refrigerator \n3. Car");
    num = Integer.parseInt (Num);

    if(num == 1)
    {
    price = rdmNum.nextInt(300) + 200;

    System.out.println("Color Television Price:$ " + fmt.format(price));
    Guess = JOptionPane.showInputDialog("Enter your first guess for the Color TV:");
    guess = Integer.parseInt(Guess);

    do
    {
    if(guess>price)
    {
    Guess = JOptionPane.showInputDialog("Guess lower?");
    guess = Integer.parseInt(Guess);
    }
    if(guess<price)
    {
    Guess = JOptionPane.showInputDialog("Guess higher?");
    guess = Integer.parseInt(Guess);
    }
    }
    while(guess!=price);
    JOptionPane.showMessageDialog(null, "Congrats! You've won a COLOR TV!!");

    }
    else
    if(num == 2)
    {
    price = rdmNum.nextInt(300) + 600;

    System.out.println("Refrigerator Price:$ " + fmt.format(price));
    Guess = JOptionPane.showInputDialog("Enter your first guess for the refrigerator:");
    guess = Integer.parseInt(Guess);

    do
    {
    if(guess>price)
    {
    Guess = JOptionPane.showInputDialog("Guess lower?");
    guess = Integer.parseInt(Guess);
    }
    if(guess<price)
    {
    Guess = JOptionPane.showInputDialog("Guess higher?");
    guess = Integer.parseInt(Guess);
    }
    }
    while(guess!=price);
    JOptionPane.showMessageDialog(null, "Congrats! You've won a REFRIGERATOR!!");
    }
    else
    if(num == 3)
    {
    price = rdmNum.nextInt(10000) + 5000;

    System.out.println("Car Price:$ " + fmt.format(price));
    Guess = JOptionPane.showInputDialog("Enter your first guess for the car:");
    guess = Integer.parseInt(Guess);

    do
    {
    if(guess>price)
    {
    Guess = JOptionPane.showInputDialog("Guess lower?");
    guess = Integer.parseInt(Guess);
    }
    if(guess<price)
    {
    Guess = JOptionPane.showInputDialog("Guess higher?");
    guess = Integer.parseInt(Guess);
    }
    }
    while(guess!=price);
    JOptionPane.showMessageDialog(null, "Congrats! You've won a CAR!!");
    }
    else
    {
    JOptionPane.showMessageDialog(null,"Incorrect prize number entered. Please try again.");
    }*/

    }

    }


    GuessingPanel------------------------------------
    package assignment_07;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class GuessingPanel extends JPanel
    {
    private final int WIDTH = 288, HEIGHT = 214;
    private final int DELAY = 0, IMAGE_Size = 35;

    private static String[] imageList =
    {
    "60.jpg","59.jpg","58.jpg","57.jpg","56.jpg","55.jpg","54.jpg","53.jpg","52.jpg","51.jpg",
    "50.jpg","49.jpg","48.jpg","47.jpg","46.jpg","45.jpg","44.jpg","43.jpg","42.jpg","41.jpg",
    "40.jpg","39.jpg","38.jpg","37.jpg","36.jpg","35.jpg","34.jpg","33.jpg","32.jpg","31.jpg",
    "30.jpg","29.jpg","28.jpg","27.jpg","26.jpg","25.jpg","24.jpg","23.jpg","22.jpg","21.jpg",
    "20.jpg","19.jpg","19.jpg","17.jpg","16.jpg","15.jpg","14.jpg","13.jpg","12.jpg","11.jpg",
    "10.jpg","09.jpg","08.jpg","07.jpg","06.jpg","05.jpg","04.jpg","03.jpg","02.jpg","01.jpg",
    "00.jpg"
    };
    private static ImageIcon image;
    private static int imageNum = -1;
    private static boolean soundFlag = true;
    private Timer timer;
    private int x,y;

    public GuessingPanel()
    {
    timer = new Timer(DELAY, new GuessingListener());
    image = new ImageIcon(imageList[0]);
    x=0;
    y=0;
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setBackground(Color.BLACK);
    timer.start();

    }

    private class GuessingListener implements ActionListener
    {
    public void actionPerformed (ActionEvent event)
    {
    imageNum++;
    image = new ImageIcon (imageList[imageNum]);
    repaint();
    }

    }

    public void paintComponent (Graphics page)
    {
    super.paintComponent (page);
    if(image != null)
    image.paintIcon(this, page, x, y);
    }
    }
    Last edited by FRin323; 12-07-2007 at 04:29 PM.

  2. #2
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    nice....keep it up!!!

Bookmarks

Posting Permissions

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