1 / 12

Controlled Assessment Practice

Learn how to create a Java applet in NetBeans, import libraries, generate random numbers, enable user interaction, and more. Follow step-by-step instructions to build a functional applet.

larrywilson
Download Presentation

Controlled Assessment Practice

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Controlled Assessment Practice Programming Task: Task 1

  2. Creating An Applet • In NetBeans, create a new project by going to "File > New Project." In the "Choose Project" window, click "JavaApplication" and click "Next." Name the project “ControlledAssessment" and click "Finish.“ • Under "Source Packages" double-click the “controlledassessment" package (with the brown box icon) • Click "New > Java Class." Name the class “FirstApplet" and click "Finish”

  3. Creating An Applet • Under the line "package controlledassessment;" type the following two lines of Java code: import java.applet.*;import java.awt.*; • This imports the Applet and Graphics Java libraries, which you will use in your code. • 4 Between "public class FirstApplet" and the "{" type "extends Applet" to tell Java that the FirstAppletclass is going to be an Applet. * Le me wondering why my applet needs to go to the library * Le me finding out that library files are pre-written files of code that I can call inside my code!

  4. Creating An Applet • Create the following methods to have your Java code initialize the Applet "paint" a line of text on the screen:public void paint (Graphics g) {g.drawstring (“Hello World”, 25, 50);} • Click "Run" from the top menu of NetBeans, then click "Run File." • Your Java Applet's window will pop up and you will see your text on the screen.

  5. Generating Random Numbers • We know that we need a number between 0 and 255 for our binary game, but it needs to be random….. • Under the line "package controlledassessment;" type another line to import the Random library: • import java.util.Random; • Inside the Paint() class, we create a new instance of a Random: • Random myRand= new Random();

  6. Generating Random Numbers • Next, we create an integer variable and give use myRand to generate a random number up to 255: • intrandomInt = myRand.nextInt(255); • Finally, we print this out to the applet screen: g.drawString ("Random Number was:" + randomInt, 25, 60); * Le me realising that to draw a new line, I need to add 10 to the y axis of the draw() co-ordinates!

  7. Entering Data We are going to create a new function within the applet where we will begin to create text boxes to allow the user to type in data. • Firstly, we create two TextField variables • private TextField input; • private TextField output; • Then, we create a new method inside the applet: • public void init() • { • } • Then we create an instance of a TextField on the applet screen: • this.input= new TextField(10); • this.output= new TextField(50);

  8. Entering Data • Finally, we want to add these textboxes to the screen! • this.add(input); • this.add(output); • Click "Run" from the top menu of NetBeans, then click "Run File." • Your Java Applet's window will pop up and you will see your text and the new text boxes on the screen.

  9. User Interaction Before your user can interact with the screen at all, the form will need a button to call the function. This is called an ‘on-click event’ • We first import the extra library classes needed for ‘listening’ for events. • import java.awt.event.ActionEvent; • import java.awt.event.ActionListener; • Then we implement the ActionListener in our main class so that we can run our button. And create an instance of a button.

  10. User Interaction • Next we want to add the button to the applet in the same way that we did for the text boxes • Then link the button to the ActionListener. • Finally, we need to create a method called ‘actionPerformed’ which hears the button click and performs an action (like putting some text into a text box). • Now, run your file!

  11. Your finished code should look like this: • Import library classes • Instantiate (create) variables • Initial function • actionPerformed function listening for button click • Function creating random numbers

  12. Extra Tasks • Can you put the text from one box into another? • Can you put the random number into a text box? • Can you save a random number and add it to the next one?

More Related