Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

java language Shooter Frame For this inlab you are going to build a JFrame, with a JPanel, and the JPanel is going to have a

java language image text in transcribed
image text in transcribed
image text in transcribed
Shooter Frame For this inlab you are going to build a JFrame, with a JPanel, and the JPanel is going to have a JButton on the panel. When the button is pressed you will draw a line from a random Y value across the JPanel to another random Y value. Here are what my frames looked like from start up, pushing the button once, a few more times, then a bunch. I did it in two classes: 1. The class that held my main was called GUI_Inlab, the main only had one line: a. new GUI_Inlab(); b. The GUI_Inlab extended JFrame c. In the constructor I set the default close operation d. Set the size YOU SHOULD USE (500, 500) e. Then created the PaintPanel and did an add to the CENTER f. Set the frame to visible & And that's it. 5 lines of code in the constructor and one line in the main. h. i did also make this a singleton class 1. getinstance() that was static and returned a GUI_Inlab instance ii. and one instance field that his static to hold the one instance 2. The Paint Panel class: a. This Class extended JPanel b. Implemented ActionListener c. Had five methods 1. Get instance that is static to get the instance of this class ii. Constructor that is private iii. paintComponent (this should be a copy and paste) iv. a draw method v. and the actionPerformed to handle the button click d. There were three instance fields i. The ImageBuffer ii. The Graphics2D for the ImageBuffer iii. And the PaintPanel singleton instance e. The Constructor: i. Created the button ii. Added an actionListener to the button iii. Added the button to the PaintPanel (just an add to the auto FlowLayout) f. The paintComponent Method is just a cut and paste from below, make sure you change the imageBuffer and Graphics names to your variable names. g. The draw method: i. Creates an instance of Random number generator ii. Takes a random number from 0 to panel height for the start Y iii. The takes another random number from 0 to panel height for the end Y iv. The draws a line from 0, starty to 500, ende v. Then remember to call repaint(0) vi. Five lines, but I could have done it in three. h. The action Performed method will not need to find out which button is pressed, just that the only button was pressed and then it should call draw, only one line

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions