Dice
. Run the program and note that it pops up 2 windows: one with an over and the other one with a text box that asks you to enter a number. When you enter a number the horizontal diameter of the oval changes accordingly. Also note that this program is a simple extension of the graphics supplement sections from your textbook./** Jose M VidalThe program is also here (Dice.java). You will be making many changes to it.* TA Please NOTE: If that's not me on the line above then I deserve 0 points on this homework. */ import java.awt.Color; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class Dice extends JPanel { /** The horizontal diameter of the oval we will draw. */ public int diameter; public void paintComponent(Graphics g) { int height = getHeight(); g.setColor(Color.black); g.drawOval(0, 0, diameter, height); } public static void main(String args[]) { JFrame frame = new JFrame("An Oval"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Dice panel = new Dice(); panel.diameter = 100; //set the diameter of the oval frame.add(panel); frame.setSize(300, 200); //create a window that is 300px horizontally by 200px vertically. frame.setVisible(true); String diameterPane = JOptionPane.showInputDialog("Enter new diameter for the oval:"); panel.diameter = Integer.parseInt(diameterPane); //set the new diameter if (panel.diameter == 0) { System.exit(0); } frame.repaint(); //don't forget to re-paint it! } }
The program you will write will ask the user for the number to display and then display that number in the face of the dice, as shown by the screenshots below.
Note that
- your program will continue asking the user for a new number right after displaying the one he just typed,
- when the user enters 0 the program should exit.
This program is due Tuesday, September 14 at 9am. Please deposit it in our dropbox or email it to us.
1 comment:
A goal of this homework is to get you to learn to read an API (the graphics API in this case) and figure out how to invoke methods you never seen before, like drawing a square. Developers spend a lot of their time reading and understanding new APIs.
In other words, I want you to figure out by yourself how to draw a rectangle. I already showed you how to draw a circle (well, an oval, but if the x-axis equals...do I even need to finish that?)
API: http://en.wikipedia.org/wiki/Application_programming_interface
Post a Comment