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

Fix the errors . Draw and tell what the animation results would be. Show the col

ID: 3544113 • Letter: F

Question

Fix the errors . Draw and tell what the animation results would be. Show the color and placements of the drawings. Please , try to explain to me every line of this program even though you think It is easy.


import java.awt.*;

import javax.swing.*;

import java.awt.event.*;


public class midani extends JApplet

{

int step = 3;

int x = 10, y = 50, oldx = 10;

Font gfont = new Font ("Impact", Font.BOLD, 36);

Container c;


public void init()

{

c = getContentPane();

c.setBackground(Color.cyan);

c.setForeground(Color.blue);

}


public void update (Graphics g)

{

update (g);

}


public void paint (Graphics g)

{


super.paint(g);

g.setFont(gfont);

g.drawString("NY", oldx, y);

g.drawString("NY", x, y);

g.setColor (c.setForeground());

g.setColor(c.getBackground());;

oldx = x;

x += step;

if (x < getSize() . height - 30 || x > getSize() .width - 30)

{

step = -step;

}

try

{

sleep(20);

}

catch (InterruptedException e)

{}

paint();

}

}


Explanation / Answer

I did all the things I can to make it error free and wrote

comments on the side for u to understand...



package midani;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;



public class midani extends JApplet { //Extending JApplet so we can get an applet which can be used as a screen

int step = 3;

int x = 10, y = 50, oldx = 10; // x, y are coordinates where the string should appear

Font gfont = new Font ("Impact", Font.BOLD, 36); // giving the String "impact font" which is bold with size 36

Container c; // This is nothing but the whole inside part of applet excluding status bar


public void init()

{

c = getContentPane(); // assigning c to the whole middle plane of the applet

c.setBackground(Color.cyan); //assigning background to cyan color.

//almost like assigning background color of ur screen

//c.setForeground(Color.blue);

}


public void update (Graphics g)

{

update (g); // This whole is not needed. You have to use this if u write midani

// implements JApplet. since u used extends... U don't need it...jus ignore for now.

}


public void paint(Graphics g)

{


super.paint(g); //Since we inherited JApplet using extends JApplet we are using one the

//pre-defined methods of JApplet to paint.

g.setFont(gfont); //Assigning the above mentioned font to the word NY in the next line.

g.drawString("NY", oldx, y); // u r displaying the word NY at oldx coordinates and y coordinates

g.drawString("NY", x, y); // u r displaying the word NY at x coordinates and y coordinates

// The above two lines are used to make the word NY blink. This blink can be seen if u can adjust the

//sleep time

// The whole below block is not needed till try block

g.setColor(c.getBackground()); // This one is not needed

oldx = x;

x += step;

if (x<getSize().height-30 || x > getSize() .width - 30)

{

step = -step;

}

try

{

Thread.sleep(20); //making the sys fall asleep for 20 microsecs.

}

catch(Exception e)

{

e.printStackTrace();

}

}

}