Results 1 to 5 of 5
  1. #1
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default Coin application

    Hi, I am just starting to teach myself Java. Needless, to say I'm a beginner. This is pretty much my first major programming attempt and I'm needing some help on it...

    I want to create an application that shows a picture of a coin in the middle of a window that 'flips' the coin every 2 seconds. I've made 2 classes: Coin and CoinTest.

    Java Code:
    package prob2;
    
    import java.awt.*;
    
    public class Coin {
    	private Image head;
    	private Image tail;
    	private int side;
    	
    	//Constructor
    	public Coin(Image heads, Image tails) {
    		head = heads;
    		tail = tails;
    		side = 0;
    	}
    	
    	//Flips the coin
    	public void flip() {
    		if (side == 0) {
    			side = 1;
    		}
    		else {
    			side = 0;
    		}
    	}
    	
    	//draws the appropriate side of the coin
    	//centered at (x,y)
    	public void draw(Graphics g, int x, int y) {
    		if (side == 0) {
    			g.drawImage(head, x, y, null);
    		}
    		else {
    			g.drawImage(tail, x, y, null);
    		}
    	}
    }
    I think my Coin class is right, but then I'm having trouble writing my CoinTest class. This assignment is from a book, so it make suggestions as to what you should include. This is what I have so far:

    Java Code:
    package prob2;
    
    import javax.swing.*;
    import java.awt.event.ActionListener;
    
    public abstract class CoinTest implements ActionListener
    {
    	private Timer timer;
    	private Coin coin;
    	
    	//Constructor
    	public CoinTest()
    	{
    		timer = new Timer(2000, this);
    		timer.start();
    		coin = new Coin(//??);
    	}
    	
    	public void paint()
    	{
    		//??
    	}
    	
    	public void actionPerformed()
    	{
    		coin.flip();
    		//??
    	}
    }
    The paint method should paint the coin and the actionperformed method should flip the coin and repaint the window. I am unsure of how to do this. Also, I know I have to create fields of type Image to give to the Coin constructor in my CoinTest constructor. I have 2 images form online that I saved onto my computer, but what's the code that will allow me to access that?

    Thanks! Any help appreciated :)
    Last edited by kkGG; 11-07-2010 at 04:27 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    This assignment is from a book, so it make suggestions as to what you should include.

    Since you are writing a graphical program you will need a JFrame instance somewere. And a component of some sort to display the images. But it's very hard to be precise without knowing what your book is suggesting. If it's not a lot of text perhaps you could post it.

    I know I have to create fields of type Image to give to the Coin constructor in my CoinTest constructor. I have 2 images form online that I saved onto my computer, but what's the code that will allow me to access that?

    This one can be answered regardless of the overall setup of your program. Have a look at the ImageIO class. You will see that it has a number of read() methods that return a BufferedImage. BufferedImage IS-An Image so you can pass these on to the Coin constructor.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your painting needs to be done in a Swing component, probably a JPanel, and so perhaps CoinTest should extend JPanel. Next you should probably draw the coin in the JPanel's paintComponent method override, not the paint override.

    To get images, you should probably use the ImageIO.read(...) method. The API will help you in this. It will return a BufferedImage.

    Another thought: another way to swap images is to create ImageIcons and then display one icon or the other in a single JLabel.

    Finally my main recommendation: read the graphics tutorials at the Swing tutorial site.

    Also, I'm curious why CoinTest is abstract?

    edit: I second all that pbrockway said! :)

  4. #4
    kkGG is offline Member
    Join Date
    Nov 2010
    Posts
    28
    Rep Power
    0

    Default

    But it's very hard to be precise without knowing what your book is suggesting. If it's not a lot of text perhaps you could post it.
    This is the exercise exactly as described in the book:

    "Create an application that shows a picture of a coin in the middle of a window and "flips" the coin every two seconds. Your application should consist of two classes: Coin and CoinTest. The Coin Class should have one constructor that takes two parameters of the type Image: the heads and tails pictures of the coin. The constructor saves these images in the coin's fields. the Coin class should have two methods:
    //Flips the coin
    public void flip() {
    ...
    }
    and
    //draws the appropriate side of the coin
    //centered at (x,y)
    public void draw(Graphics g, int x, int y) {
    ...
    }
    The CoinTest class's constructor should create a Timer and a Coin. It also should have a paint method that paints the coin and an actionperformed method that flips the coin and repaints the window."

    Hope this helps you help me :]

    And question: if I were to use the ImageIO.read() method and my image was saved in a file called 'CoinImg.jpeg' how would I call the method to have it read this file?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    if I were to use the ImageIO.read() method and my image was saved in a file called 'CoinImg.jpeg' how would I call the method to have it read this file?

    * Create a File instance using the file name
    * Call the static read() method with this file. If you haven't used static methods much the syntax is

    Java Code:
    Image im = ImageIO.read(file);

    (To begin with you might like to use System.out.println() to print the values both of file and im or whatever you are going to call them. I mention this because tis process often experiences difficulty in finding the file you mean.)

    -------------

    As for the rest I would repeat Fubarable's suggestion of reading the relevant bits of the Tutorial. Your draw() method looks OK (I think). The rest should be as was suggested: make CoinTest extend JPanel and override paintComponent() not paint(). This paintComponent() won't have much to do: just tell the coin to draw() itself.

    The other hole in the code you posted was in the actionPerformed() method. You tell the coin to flip() but you should also have the panel repaint itself. Again the Tutorial has examples.

    [Edit]

    The code isn't very long, so if you get stuck post what you've got to illustrate the problem you're having. Remember to work one small step at a time and make sure what you have compiles.

    I also forgot to say that if you make CoinTest a JPanel then you will still need a frame somewhere. The Tutorial follows an idiom of having a main class that calls another method to create and show the gui: again, it is well illustrated.
    Last edited by pbrockway2; 11-08-2010 at 01:28 AM.

Similar Threads

  1. Java Coin Acceptor?
    By starzsimon in forum Advanced Java
    Replies: 5
    Last Post: 08-06-2011, 08:50 AM
  2. Coin Change
    By Growler in forum New To Java
    Replies: 7
    Last Post: 09-23-2010, 06:33 PM
  3. coin toss program(Eclipse)
    By ccie007 in forum New To Java
    Replies: 5
    Last Post: 08-06-2010, 08:03 PM
  4. Random coin flip application
    By Boomer1 in forum New To Java
    Replies: 8
    Last Post: 12-18-2009, 03:57 AM
  5. Replies: 2
    Last Post: 11-11-2007, 09:07 AM

Posting Permissions

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