Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A public method setPaintFlag that sets the paintFlag variable to true and theMod

ID: 3843881 • Letter: A

Question

A public method setPaintFlag that sets the paintFlag variable to true and theMode to the given int currentMode argument. The method does not return anything.(I do not know how to do this part. Thank you for your help)

(d). Override the paintComponent method with the following.

public void paintComponent(Graphics g) {

// return if paint flag is false super.paintComponent(g); // clears screen

// get the fancier 2D graphics object Graphics2D g2 = (Graphics2D)g;

// create a random color

int red = (int)(Math.random() * 255) + 1; int green = (int)(Math.random() * 255) + 1; int blue = (int)(Math.random() * 255) + 1;

// set the color

g2.setColor(new Color(red, green, blue));

// create a random width

int w = (int)(Math.random() * (width/2));

// create a random location

int x = (int)(Math.random() * width) + 1; int y = (int)(Math.random() * height) + 1;

// draw the shape:

// if the mode is set to CIRCLE, call: g2.fillOval(x, y, w, w)

// else if mode is set to SQUARE, call: g2.fillRect(x, y, w, w)

// turn off the paint flag g.dispose();

please help!

Explanation / Answer

//Following code contains code for setPaintFlag() and modified paintComponent() method.

//Suppose the methods and variables all used in code are in sample class file

//Or you can change the name of class file as per your class name

//Let consider the theMode=1 for CIRCLE and theMode=2 for SQUARE

public class sample
{
private int theMode;
private boolean paintFlag;
public void setPaintFlag(int current)
{
   theMode=current;
   paintFlag=true;
}
public void dispose()
{
   paintFlag=false;
}
public void paintComponent(Graphics g)
{
   // return if paint flag is false super.paintComponent(g); // clears screen
   if(paintFlag==false)
       return super.paintComponent(g);
   else
   {
       // get the fancier 2D graphics object
       Graphics2D g2 = (Graphics2D)g;
       // create a random color
       int red = (int)(Math.random() * 255) + 1; int green = (int)(Math.random() * 255) + 1; int blue = (int)(Math.random()* 255) + 1;
       // set the color
       g2.setColor(new Color(red, green, blue));
       // create a random width
       int w = (int)(Math.random() * (width/2));
       // create a random location
       int x = (int)(Math.random() * width) + 1; int y = (int)(Math.random() * height) + 1;
       // draw the shape:
       // if the mode is set to CIRCLE, call: g2.fillOval(x, y, w, w)
       // else if mode is set to SQUARE, call: g2.fillRect(x, y, w, w)
       if(theMode==1) //Here theMode is 1 for Circle
           g2.fillOval(x, y, w, w);
       else if(theMode==2) //Here theMode is 2 for square
           g2.fillRect(x, y, w, w);
      
       // turn off the paint flag g.dispose();
       g.dispose();
   }
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote