Execute the following Java 2D Scene and explain what it does. Also, generate the image it produces:  import java.awt.*; import java.awt.font.TextLayout; import javax.swing.*; import javafx.scene.effect.Shadow; import java.awt.geom.*; public class Scene2D extends JPanel { private GeneralPath hills; private GeneralPath wind_fan; private float pixelSize = (float) .01;; private AffineTransform transformer; public Scene2D() { // hills hills = new GeneralPath(); hills.moveTo(0,-.8); hills.lineTo(0,1); hills.curveTo(5,1.8F,4,2.2F,5,2.2F); hills.curveTo(6,2.2F,6,2,7,2); hills.lineTo(7,-.8); hills.closePath(); wind_fan = new GeneralPath(); wind_fan.moveTo(0,0); wind_fan.lineTo(1F,0.1F); wind_fan.lineTo(1F,-0.1F); wind_fan.closePath(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setColor(Colors.BLACK.getValue()); Font f1 = new Font("Lucida Bright",Font.BOLD,10); g2.setFont(f1); TextLayout layout = new TextLayout("WIND ENERGY", f1, g2.getFontRenderContext()); g2.setPaint(new Color(150, 150, 150)); layout.draw(g2, 300 + 3, 455 + 3); g2.setPaint(Color.BLACK); layout.draw(g2, 300, 455); Image theImage; theImage = new javax.swing.ImageIcon("logo.png").getImage(); g2.drawImage(theImage,270,440,null); g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); setCoordinates(g2, getWidth(), getHeight()); // plot sky g2.setColor(Colors.SKY.getValue()); g2.fillRect(0,0,7,4); // hills g2.setColor(Colors.HILLS.getValue()); g2.fill(hills); // road g2.setColor(Colors.ROAD.getValue()); g2.fill(new Rectangle2D.Double(0,-0.4,7,0.8)); // dashed lines on road g2.setStroke( new BasicStroke(0.1F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,new float[] { 0.3F, 0.1F }, 1) ); g2.setColor(Color.WHITE); g2.drawLine(0,0,7,0); g2.setStroke( new BasicStroke(pixelSize) ); g2.setStroke( new BasicStroke(0.1F)); g2.setColor(Colors.ROADSIDE.getValue()); g2.draw(new Line2D.Double(0,.35,7,.35)); g2.draw(new Line2D.Double(0,-.35,7,-.35)); g2.setStroke( new BasicStroke(pixelSize) ); transformer = g2.getTransform(); // sun g2.translate(5,3.3); drawSun(g2); g2.setTransform(transformer); // wind energy g2.translate(0.75,1); g2.scale(0.6,0.6); drawWind(g2); g2.setTransform(transformer); g2.translate(2.2,1.2); g2.scale(0.4,0.4); drawWind(g2); g2.setTransform(transformer); g2.translate(3.7,0.8); g2.scale(1.0,1.0); drawWind(g2); g2.setTransform(transformer); } private void setCoordinates(Graphics2D g2, int width, int height) { double[] scaleRange = new double[] {0,7,4,-1}; g2.scale( width / (scaleRange[1]-scaleRange[0]), height / (scaleRange[3]-scaleRange[2]) ); g2.translate( -scaleRange[0], -scaleRange[2] ); } private void drawSun(Graphics2D g2) { g2.setColor(Colors.YELLOW.getValue()); // plot rays with sun for (int i = 0; i < 10; i++) { g2.rotate(degtorad(18)*2); g2.drawLine(0,0,0,1); } float x = 0f; float y = 0f; float r = 1.35f; x = (float) x-(r/2); y = (float) y-(r/2); g2.fill( new Ellipse2D.Float(x,y,r,r)); } private void drawWind(Graphics2D g2) { // plot pole g2.setColor(Colors.POLE1.getValue()); g2.fill(new Rectangle2D.Double(-0.05,0,0.1,1)); g2.setColor(Colors.POLE2.getValue()); g2.fill(new Rectangle2D.Double(-0.05,1,0.1,2)); g2.translate(0,3); g2.setColor(Colors.WHITE.getValue()); float x = 0f; float y = 0f; float r = .25f; x = (float) x-(r/2); y = (float) y-(r/2); g2.fill( new Ellipse2D.Float(x,y,r,r)); // plot winds for (int i = 0; i < 6; i++) { g2.rotate(degtorad(60)); g2.fill(wind_fan); } } private double degtorad(int angle) { return angle*Math.PI/180; } }   Colors.java   import java.awt.Color; public enum Colors { LIGHT_GRAY(Color.GRAY),DARK_GRAY(Color.DARK_GRAY),WHITE(Color.WHITE),GREEN(Color.GREEN),YELLOW(Color.YELLOW),SKY(new Color(150,150,255)),ROAD(new Color(100,100,150)), WIND(new Color(100,100,200)),POLE1(new Color(255, 255, 102)),POLE2(new Color(200,200,225)),ROADSIDE(new Color(255, 255, 51)), BLACK(Color.BLACK),HILLS(new Color(0,150,30)); private final Color value; private Colors(Color value) { this.value = value; } public Color getValue() { return value; } }   Driver.java   import java.awt.Dimension; import javax.swing.JFrame; public class Driver { public static void main(String[] args) { Scene2D scene = new Scene2D(); JFrame frame = new JFrame("Wind Energy"); frame.setPreferredSize( new Dimension(700,500)); frame.setContentPane( scene ); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Execute the following Java 2D Scene and explain what it does. Also, generate the image it produces: 

import java.awt.*;
import java.awt.font.TextLayout;

import javax.swing.*;

import javafx.scene.effect.Shadow;

import java.awt.geom.*;

public class Scene2D extends JPanel
{
private GeneralPath hills;
private GeneralPath wind_fan;
private float pixelSize = (float) .01;;
private AffineTransform transformer;

public Scene2D() {


// hills
hills = new GeneralPath();
hills.moveTo(0,-.8);
hills.lineTo(0,1);
hills.curveTo(5,1.8F,4,2.2F,5,2.2F);
hills.curveTo(6,2.2F,6,2,7,2);
hills.lineTo(7,-.8);
hills.closePath();


wind_fan = new GeneralPath();
wind_fan.moveTo(0,0);
wind_fan.lineTo(1F,0.1F);
wind_fan.lineTo(1F,-0.1F);
wind_fan.closePath();

}

protected void paintComponent(Graphics g) {
super.paintComponent(g);


Graphics2D g2 = (Graphics2D)g;

g2.setColor(Colors.BLACK.getValue());
Font f1 = new Font("Lucida Bright",Font.BOLD,10);
g2.setFont(f1);

TextLayout layout = new TextLayout("WIND ENERGY", f1, g2.getFontRenderContext());
g2.setPaint(new Color(150, 150, 150));
layout.draw(g2, 300 + 3, 455 + 3);

g2.setPaint(Color.BLACK);
layout.draw(g2, 300, 455);

Image theImage;

theImage = new javax.swing.ImageIcon("logo.png").getImage();
g2.drawImage(theImage,270,440,null);

g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);



setCoordinates(g2, getWidth(), getHeight());


// plot sky

g2.setColor(Colors.SKY.getValue());
g2.fillRect(0,0,7,4);

// hills

g2.setColor(Colors.HILLS.getValue());
g2.fill(hills);

// road

g2.setColor(Colors.ROAD.getValue());
g2.fill(new Rectangle2D.Double(0,-0.4,7,0.8));

// dashed lines on road

g2.setStroke( new BasicStroke(0.1F, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,new float[] { 0.3F, 0.1F }, 1) );
g2.setColor(Color.WHITE);
g2.drawLine(0,0,7,0);
g2.setStroke( new BasicStroke(pixelSize) );

g2.setStroke( new BasicStroke(0.1F));
g2.setColor(Colors.ROADSIDE.getValue());
g2.draw(new Line2D.Double(0,.35,7,.35));
g2.draw(new Line2D.Double(0,-.35,7,-.35));
g2.setStroke( new BasicStroke(pixelSize) );


transformer = g2.getTransform();

// sun
g2.translate(5,3.3);
drawSun(g2);
g2.setTransform(transformer);

// wind energy

g2.translate(0.75,1);
g2.scale(0.6,0.6);
drawWind(g2);
g2.setTransform(transformer);

g2.translate(2.2,1.2);
g2.scale(0.4,0.4);
drawWind(g2);
g2.setTransform(transformer);

g2.translate(3.7,0.8);
g2.scale(1.0,1.0);
drawWind(g2);
g2.setTransform(transformer);

}

private void setCoordinates(Graphics2D g2, int width, int height)
{

double[] scaleRange = new double[] {0,7,4,-1};
g2.scale( width / (scaleRange[1]-scaleRange[0]), height / (scaleRange[3]-scaleRange[2]) );
g2.translate( -scaleRange[0], -scaleRange[2] );
}


private void drawSun(Graphics2D g2)
{
g2.setColor(Colors.YELLOW.getValue());

// plot rays with sun
for (int i = 0; i < 10; i++)
{
g2.rotate(degtorad(18)*2);
g2.drawLine(0,0,0,1);
}

float x = 0f;
float y = 0f;
float r = 1.35f;
x = (float) x-(r/2);
y = (float) y-(r/2);

g2.fill( new Ellipse2D.Float(x,y,r,r));
}

private void drawWind(Graphics2D g2)
{
// plot pole
g2.setColor(Colors.POLE1.getValue());
g2.fill(new Rectangle2D.Double(-0.05,0,0.1,1));
g2.setColor(Colors.POLE2.getValue());
g2.fill(new Rectangle2D.Double(-0.05,1,0.1,2));

g2.translate(0,3);
g2.setColor(Colors.WHITE.getValue());

float x = 0f;
float y = 0f;
float r = .25f;
x = (float) x-(r/2);
y = (float) y-(r/2);

g2.fill( new Ellipse2D.Float(x,y,r,r));


// plot winds

for (int i = 0; i < 6; i++)
{
g2.rotate(degtorad(60));
g2.fill(wind_fan);
}
}

private double degtorad(int angle)
{
return angle*Math.PI/180;
}

}

 

Colors.java

 

import java.awt.Color;

public enum Colors {

LIGHT_GRAY(Color.GRAY),DARK_GRAY(Color.DARK_GRAY),WHITE(Color.WHITE),GREEN(Color.GREEN),YELLOW(Color.YELLOW),SKY(new Color(150,150,255)),ROAD(new Color(100,100,150)),
WIND(new Color(100,100,200)),POLE1(new Color(255, 255, 102)),POLE2(new Color(200,200,225)),ROADSIDE(new Color(255, 255, 51)), BLACK(Color.BLACK),HILLS(new Color(0,150,30));

private final Color value;
private Colors(Color value) {
this.value = value;
}
public Color getValue() {
return value;
}
}

 

Driver.java

 

import java.awt.Dimension;

import javax.swing.JFrame;

public class Driver {

public static void main(String[] args) {

Scene2D scene = new Scene2D();

JFrame frame = new JFrame("Wind Energy");
frame.setPreferredSize( new Dimension(700,500));
frame.setContentPane( scene );
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education