How to paint the screen -controlling and using the


What will you learn

-How to paint the screen -Controlling and using the paintComponent ( ) method

Deliverables

app.java, myJFrame.java, myJPanel.java,  and other necessary Java files

Recommended Java examples

The mouse motionexample The paintComponent example (image on a panel example) The how to use an array example (button array example) Drawingusing paintComponent

Contents

I am giving you a guide (try to start the work on the lab before consulting the guide). You have to create an application draws on the screen (Like MS-Paint) for instance.   The application has to: -draw lines as you drag the mouse -redraw what was on the screen when the screen is minimized and then maximized (or just resized)   When this is working then implement at least oneof the options below

  1. -let the user change the color of the lines
  2. -let the user choose a line thickness
  3. -an eraser

  Remember that the redrawing capability (in paintComponent) has to work with all the features. I mean, when the screen is resized, maximized, minimized, what was on the screen has to be redrawn.  

Lab 11 Guide

Drawing on the screen

There are many ways of drawing on the screen. In this lab the best solution is to fill small rectangles on the back of a Panel. You have to choose where to do this. The right place is the methodmouseDraggedthat is called every time the mouse is dragged.  

public void mouseDragged(MouseEvent evt)
{
   Point pt = evt.getPoint();
   int x = pt.x;
   int y = pt.y;
   Graphics gg = getGraphics();

   gg.setColor(Color.RED);
   gg.fillRect(x,y,5,5);

A reminder from the MouseListener lesson (Week 07)

Repainting the screen

Now that you are able to draw on the screen, try to resize it. You will note that the drawing disappeared. This is because what Java does is to give you a fresh back of the panel. It is your job to redraw whatever was there. To do this you need to follow a series of steps. 1.Save the original drawing. Since the drawing is a series of Xs and Ys (points), you need to save them. You can create an array of the type Point (Google "Java Point class"). 2.Every time the screen is resized, Java automatically calls the method paintComponent( ); You need to override (rewrite the original method) the original paintComponent method in the original JPanel class (remember, you are using a subclass of JPanel, one in which you can add (or override) methods and variables. If you haven't read the recommended reading on overriding, you should do so now. 3.In the new paintComponent ( ) method, you will read the points you saved and ask them to be redrawn.   

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: How to paint the screen -controlling and using the
Reference No:- TGS01150061

Expected delivery within 24 Hours