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

Create an applet/Screen saver and in it create: Random triangles, Random locatio

ID: 3627471 • Letter: C

Question

Create an applet/Screen saver and in it create:
Random triangles,
Random locations
Random sizes/shapes
and Random colors.
After displaying 100 triangles clear and start again,after 500ms delay .
Your triangles should be colored.
Make a method fillTriangle,this method does not exit you need to create it yourself.

How to clear the Screen: use cls method
public void cls(Graphics g)
{
-----
-----
----
}

How to create 500ms delay:
public void sleep(Long ms)
{
try
{
thread.sleep(ms);
}catch(interrupted Exception e){}
}

public void repaint(Long ms)
{
sleep(ms);
cls();
}

 


Generate a source code and html sample.

Explanation / Answer

Dear User, import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import javax.swing.JFrame;

class TD extends JPanel
{
public void paint( Graphics g )
{
super.paint( g );
Graphics2D g2d = ( Graphics2D ) g;
for ( int i = 0; i < 100; i++ )
{
GeneralPath triangle = new GeneralPath();
int x = ( int ) ( Math.random() * 375 + 25 );
int y = ( int ) ( Math.random() * 375 + 25 );
triangle.moveTo( x, y );
x = ( int ) ( Math.random() * 375 + 25 );
y = ( int ) ( Math.random() * 375 + 25 );
triangle.lineTo( x, y );
x = ( int ) ( Math.random() * 375 + 25 );
y = ( int ) ( Math.random() * 375 + 25 );
triangle.lineTo( x, y );
triangle.closePath();
g2d.setColor(new Color( ( int )
( Math.random() * 256 ), ( int )
( Math.random() * 256 ), ( int )
( Math.random() * 256 ) ) );
g2d.fill( triangle );
}
}
}
class Triangles extends JFrame
{
public static void main( String args[] )
{
JFrame frame = new JFrame("Triangles");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
TrianglesDemo app = new TrianglesDemo();
frame.add(app);
frame.setSize(450,450);
frame.setVisible(true);
}
}
I hope this will helps to You !
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