Java代写:COMP1010 Labelled Dots

练习Swing的JOptionPane的用法,随机绘制图像,完成GUI作业。

Material Covered

  • String data
  • char data
  • JOptionPane input (Gold question only)

Notes

  • Make sure your TA has recorded your mark before leaving.
  • Remember that you only need to do one of the three levels (Bronze, Silver, or Gold) to obtain 2/2 on the lab, although you are encouraged to do as many as you can.
  • The exercises are independent and can be done in any order.

Labelled dots

In this exercise, you will draw small circles in the canvas, one per frame, and label them with the (x,y) coordinate of their centres, as shown on the right.

The draw() function should generate a random (x,y) coordinate, and draw a small white circle, with a diameter of 5 pixels, with its centre at that location. Use int values, not float values, for x and y (otherwise the coordinates will not look very good when you draw them), and make sure that the x coordinate is at least 80 pixels from the right edge, and the y coordinate is at least 25 pixels from the top edge, to make sure there is room to draw the coordinates. Use a 500x500 canvas, as usual. Remember that random(a,b) can be used to get a random float value r where a≤r<b. Use appropriate named constants to control all of these things as usual. Make sure this part of the program is working properly before continuing.

Then draw the (x,y) coordinates using Strings as shown in the image. You should use black text, 16 pixels in size. Draw the text starting 5 pixels above and to the right of the centres of the circles. Use more appropriate named constants, of course.

If you draw 60 small circles per second, it will not work very well. Add the new command frameRate(1); to your setup() function. This will cause draw() to be run 1 time per second, not 60 times per second, which will work a lot better for this program.

Annoying Typing

In this exercise, you will allow the user to type characters on the keyboard, and have them appear in the canvas. However, each character typed will appear at a random position. The image at right shows what might happen if you typed “Computer Science”. (This wouldn’t make a very good word processor!)

  • Use black letters, 25 pixels tall, on a white background.
  • In addition to setup() and draw(), write the keyPressed() function which will be run every time a key is pressed on the keyboard. Note that draw() will have nothing to do. All the work will be in keyPressed().
  • Whenever the user types Enter or Return (\n), erase the screen.
  • When you choose a random location for the character, don’t choose a location too close to an edge of the canvas. Make sure the whole character will fit in the canvas. Use the functions textWidth, textAscent, and textDescent to do this.

Animated typing

To see what this program is supposed to do, download the file Lab06Gold.windows64.zip, un-zip it, and run the application inside. (This is for a 64-bit Windows system only, such as the computers in most of the U of M labs.)

This program will use JOptionPane to read in a short String of text from the user(**). It will then type that text one character per frame starting at a random position within the canvas. For example, if the user enters “This is a test”, then the program will first write the ‘T’, then one frame later it will add the ‘h’, then the ‘i’, etc. After the whole String is written, it should look exactly the same as if you had drawn it all at once. The sample at right shows this text partially completed. After the entire String has been written, the program should erase the canvas and start again at another random location.

  • Use black letters, 40 pixels high, against a grey background.
  • It will be too fast if the characters are “typed” onto the canvas 60 per second. Use frameRate(10); to slow it down to 10 per second. Or try some other number.
  • Use textWidth, textAscent, and textDescent to make sure that you pick random locations that will always allow the entire String to fit in the canvas. (**) To use JOptionPane:
  1. Add import javax.swing.JOptionPane; to the very top of your program.

  2. The function JOptionPane.showInputDialog(“Type something:”) will show a dialog box, with the given prompt, and return whatever String the user types.