Question
Let's have a little more fun with the classes available in the Java API. For this activity, your program will draw a circle point that
Let's have a little more fun with the classes available in the Java API. For this activity, your program will draw a circle point that you can 'paint' with!
Follow these directions to create your simple painting program:
Create the following Draw class file. (See code below.)
Add your name and date to the top comments
Compile to make sure there are no syntax errors.
Run/execute the program to make sure you are able to draw with the up and down arrow keys.
Once 1-4 are complete, modify the code to make these changes:
the circle can move left and right as well as up and down.
the circle is smaller.
the circle moves 10 pixels instead of 5 at a time.
when the user presses F1, the drawing color changes to blue
when the user presses F2, the drawing color changes to red
when the user presses F3, the drawing color changes to green
when the user presses F4, the drawing color resets to black
Compile and run to make sure A-G works properly.
Draw a picture with your program using all colors (blue, red, green, black) with strokes up, down, left, right and diagonal.
Create a word processing document named S06Draw with your last name and first initial, e.g. S06DrawSmithD.
Take a screenshot of your drawing and your code.
Insert into your S06Draw document.
Save/download/export your S06Draw document as a pdf.
Submit your S06Draw pdf to this assignment dropbox.
Draw.java Given Code:
import java.awt.*; import java.awt.event.KeyEvent; import javax.swing.JFrame; public class Draw extends Canvas { int x, y; Color color; public static void main( String[] args ) { JFrame draw = new JFrame("Use the arrow keys to draw!"); draw.setSize(1020,764); draw.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); draw.add( new Draw() ); draw.setVisible(true); } public Draw() { enableEvents(java.awt.AWTEvent.KEY_EVENT_MASK); requestFocus(); x = 500; y = 500; color = Color.black; } public void paint( Graphics g ) { g.setColor(color); g.fillOval(x, y, 50, 50); } public void update( Graphics g ) { paint(g); } public void processKeyEvent(KeyEvent e) { // this method automatically gets called when you press a key if ( e.getID() == KeyEvent.KEY_PRESSED ) { if ( e.getKeyCode() == KeyEvent.VK_UP ) y -= 5; if ( e.getKeyCode() == KeyEvent.VK_DOWN ) y += 5; repaint(); } } public boolean isFocusable() { return true; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started