SlideShare a Scribd company logo
The Noland national flag is a square showing the following
pattern. Write a method public void drawNoland(int n) that
draws the Noland flag on the screen with a height of n pixels.
Your method should create and use a SimpleCanvas (as used in
lectures and laboratories) to draw on. All of the colors needed
are pre-constructed Color objects.
Solution
import javax.swing.*;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.Observer;
import java.util.Random;
public class SimpleCanvas
{
private static int num = 0;
private static Color[] colorArray = { Color.green, Color.cyan,
new Color(204,0,204), Color.gray};
private ModelDisplay modelDisplay = null;
private Picture picture = null;
private int width = 15;
private int height = 18;
private int xPos = 0;
private int yPos = 0;
private double heading = 0; // default is facing north
private Pen pen = new Pen();
private Color bodyColor = null;
private Color shellColor = null;
private Color infoColor = Color.black;
private boolean visible = true;
private boolean showInfo = false;
private String name = "No name";
public SimpleCanvas(int x, int y)
{
xPos = x;
yPos = y;
bodyColor = colorArray[num % colorArray.length];
setPenColor(bodyColor);
num++;
}
public SimpleCanvas(int x, int y, ModelDisplay display)
{
this(x,y); // invoke constructor that takes x and y
modelDisplay = display;
display.addModel(this);
}
public SimpleCanvas(ModelDisplay display)
{
// invoke constructor that takes x and y
this((int) (display.getWidth() / 2),
(int) (display.getHeight() / 2));
modelDisplay = display;
display.addModel(this);
}
public SimpleCanvas(int x, int y, Picture picture)
{
this(x,y); // invoke constructor that takes x and y
this.picture = picture;
this.visible = false; }
public SimpleCanvas(Picture picture)
{
// invoke constructor that takes x and y
this((int) (picture.getWidth() / 2),
(int) (picture.getHeight() / 2));
this.picture = picture;
this.visible = false;
}
public double getDistance(int x, int y)
{
int xDiff = x - xPos;
int yDiff = y - yPos;
return (Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
}
public void turnToFace(SimpleCanvas turtle)
{
turnToFace(turtle.xPos,turtle.yPos);
}
public void turnToFace(int x, int y)
{
double dx = x - this.xPos;
double dy = y - this.yPos;
double arcTan = 0.0;
double angle = 0.0;
// avoid a divide by 0
if (dx == 0)
{
// if below the current turtle
if (dy > 0)
heading = 180;
// if above the current turtle
else if (dy < 0)
heading = 0;
}
// dx isn't 0 so can divide by it
else
{
arcTan = Math.toDegrees(Math.atan(dy / dx));
if (dx < 0)
heading = arcTan - 90;
else
heading = arcTan + 90;
}
// notify the display that we need to repaint
updateDisplay();
}
public Picture getPicture() { return this.picture; }
public void setPicture(Picture pict) { this.picture = pict; }
public ModelDisplay getModelDisplay() { return
this.modelDisplay; }
public void setModelDisplay(ModelDisplay theModelDisplay)
{ this.modelDisplay = theModelDisplay; }
public boolean getShowInfo() { return this.showInfo; }
public void setShowInfo(boolean value) { this.showInfo =
value; }
public Color getShellColor()
{
Color color = null;
if (this.shellColor == null && this.bodyColor != null)
color = bodyColor.darker();
else color = this.shellColor;
return color;
}
public void setShellColor(Color color) { this.shellColor = color;
}
public Color getBodyColor() { return this.bodyColor; }
public void setBodyColor(Color color) { this.bodyColor =
color;}
public void setColor(Color color) { this.setBodyColor(color); }
public Color getInfoColor() { return this.infoColor; }
public void setInfoColor(Color color) { this.infoColor = color;
}
public int getWidth() { return this.width; }
public int getHeight() { return this.height; }
public void setWidth(int theWidth) { this.width = theWidth; }
public void setHeight(int theHeight) { this.height = theHeight; }
public int getXPos() { return this.xPos; }
public int getYPos() { return this.yPos; }
public Pen getPen() { return this.pen; }
public void setPen(Pen thePen) { this.pen = thePen; }
public boolean isPenDown() { return this.pen.isPenDown(); }
public void setPenDown(boolean value) {
this.pen.setPenDown(value); }
public void penUp() { this.pen.setPenDown(false);}
public void penDown() { this.pen.setPenDown(true);}
public Color getPenColor() { return this.pen.getColor(); }
public void setPenColor(Color color) { this.pen.setColor(color);
}
public void setPenWidth(int width) { this.pen.setWidth(width);
}
public int getPenWidth() { return this.pen.getWidth(); }
public double getHeading() { return this.heading; }
public void setHeading(double heading)
{
this.heading = heading;
}
public String getName() { return this.name; }
public void setName(String theName)
{
this.name = theName;
}
public boolean isVisible() { return this.visible;}
public void hide() { this.setVisible(false); }
public void show() { this.setVisible(true); }
public void setVisible(boolean value)
{
// if the turtle wasn't visible and now is
if (visible == false && value == true)
{
// update the display
this.updateDisplay();
}
// set the visibile flag to the passed value
this.visible = value;
}
public void updateDisplay()
{
// check that x and y are at least 0
if (xPos < 0)
xPos = 0;
if (yPos < 0)
yPos = 0;
// if picture
if (picture != null)
{
if (xPos >= picture.getWidth())
xPos = picture.getWidth() - 1;
if (yPos >= picture.getHeight())
yPos = picture.getHeight() - 1;
Graphics g = picture.getGraphics();
paintComponent(g);
}
else if (modelDisplay != null)
{
if (xPos >= modelDisplay.getWidth())
xPos = modelDisplay.getWidth() - 1;
if (yPos >= modelDisplay.getHeight())
yPos = modelDisplay.getHeight() - 1;
modelDisplay.modelChanged();
}
}
public void forward(int pixels)
{
int oldX = xPos;
int oldY = yPos;
// change the current position
xPos = oldX + (int) (pixels *
Math.sin(Math.toRadians(heading)));
yPos = oldY + (int) (pixels * -
Math.cos(Math.toRadians(heading)));
// add a line from the old position to the new position to the
pen
pen.addLine(oldX,oldY,xPos,yPos);
// update the display to show the new line
updateDisplay();
}
public void backward(int pixels)
{
forward(-pixels);
}
public void moveTo(int x, int y)
{
this.pen.addLine(xPos,yPos,x,y);
this.xPos = x;
this.yPos = y;
this.updateDisplay();
}
}
} // end of class

More Related Content

Similar to The Noland national flag is a square showing the following pattern. .docx

building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
tutorialsruby
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
tutorialsruby
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
JakeT2gGrayp
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
BTI360
 
ch03g-graphics.ppt
ch03g-graphics.pptch03g-graphics.ppt
ch03g-graphics.ppt
Mahyuddin8
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas banevicius
Raimundas Banevičius
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
arjuncorner565
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
bergel
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdf
JUSTSTYLISH3B2MOHALI
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
anukoolelectronics
 
Applet life cycle
Applet life cycleApplet life cycle
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Demetrio Siragusa
 
The Canvas API for Rubyists
The Canvas API for RubyistsThe Canvas API for Rubyists
The Canvas API for Rubyists
deanhudson
 
Kill the DBA
Kill the DBAKill the DBA
Kill the DBA
Knut Haugen
 
import java.util.Random;defines the Stock class public class S.pdf
import java.util.Random;defines the Stock class public class S.pdfimport java.util.Random;defines the Stock class public class S.pdf
import java.util.Random;defines the Stock class public class S.pdf
aquazac
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
Nitigan Nakjuatong
 
Sbaw090623
Sbaw090623Sbaw090623
Sbaw090623
Atsushi Tadokoro
 
はじめてのGroovy
はじめてのGroovyはじめてのGroovy
はじめてのGroovy
Tsuyoshi Yamamoto
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
snelkoli
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
Dmitry Sheiko
 

Similar to The Noland national flag is a square showing the following pattern. .docx (20)

building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
ch03g-graphics.ppt
ch03g-graphics.pptch03g-graphics.ppt
ch03g-graphics.ppt
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas banevicius
 
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdfModify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
 
Test beautycleanness
Test beautycleannessTest beautycleanness
Test beautycleanness
 
Please help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdfPlease help!!I wanted to know how to add a high score to this prog.pdf
Please help!!I wanted to know how to add a high score to this prog.pdf
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + ProcessingRoberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
 
The Canvas API for Rubyists
The Canvas API for RubyistsThe Canvas API for Rubyists
The Canvas API for Rubyists
 
Kill the DBA
Kill the DBAKill the DBA
Kill the DBA
 
import java.util.Random;defines the Stock class public class S.pdf
import java.util.Random;defines the Stock class public class S.pdfimport java.util.Random;defines the Stock class public class S.pdf
import java.util.Random;defines the Stock class public class S.pdf
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
 
Sbaw090623
Sbaw090623Sbaw090623
Sbaw090623
 
はじめてのGroovy
はじめてのGroovyはじめてのGroovy
はじめてのGroovy
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
TypeScript Introduction
TypeScript IntroductionTypeScript Introduction
TypeScript Introduction
 

More from Komlin1

Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docxTheodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Komlin1
 
Theory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docxTheory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docx
Komlin1
 
Theory and the White-Collar OffenderOur previous week’s discussion.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docxTheory and the White-Collar OffenderOur previous week’s discussion.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docx
Komlin1
 
There are 2 questions part A and B. All questions and relevant att.docx
There are 2 questions part A and B. All questions and relevant att.docxThere are 2 questions part A and B. All questions and relevant att.docx
There are 2 questions part A and B. All questions and relevant att.docx
Komlin1
 
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docxThere are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
Komlin1
 
Theoretical PerspectiveIdentify at least one human developme.docx
Theoretical PerspectiveIdentify at least one human developme.docxTheoretical PerspectiveIdentify at least one human developme.docx
Theoretical PerspectiveIdentify at least one human developme.docx
Komlin1
 
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docxTHEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
Komlin1
 
Theories of Behavior TimelineComplete the following tabl.docx
Theories of Behavior TimelineComplete the following tabl.docxTheories of Behavior TimelineComplete the following tabl.docx
Theories of Behavior TimelineComplete the following tabl.docx
Komlin1
 
Thematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docxThematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docx
Komlin1
 
The written portion of the research paper should be 9-11 pages in le.docx
The written portion of the research paper should be 9-11 pages in le.docxThe written portion of the research paper should be 9-11 pages in le.docx
The written portion of the research paper should be 9-11 pages in le.docx
Komlin1
 
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docxThe World since 1945Country Report- SAUDI ARABIA     Histo.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
Komlin1
 
The world runs on Big Data.  Traditionally, Data has been expressed .docx
The world runs on Big Data.  Traditionally, Data has been expressed .docxThe world runs on Big Data.  Traditionally, Data has been expressed .docx
The world runs on Big Data.  Traditionally, Data has been expressed .docx
Komlin1
 
the    1.The collaborative planning Methodology is the f.docx
the    1.The collaborative planning Methodology is the f.docxthe    1.The collaborative planning Methodology is the f.docx
the    1.The collaborative planning Methodology is the f.docx
Komlin1
 
The word stereotype originally referred to a method used by printers.docx
The word stereotype originally referred to a method used by printers.docxThe word stereotype originally referred to a method used by printers.docx
The word stereotype originally referred to a method used by printers.docx
Komlin1
 
The Value of Critical Thinking  Please respond to the followin.docx
The Value of Critical Thinking  Please respond to the followin.docxThe Value of Critical Thinking  Please respond to the followin.docx
The Value of Critical Thinking  Please respond to the followin.docx
Komlin1
 
The Value Chain Concept Please respond to the following·.docx
The Value Chain Concept Please respond to the following·.docxThe Value Chain Concept Please respond to the following·.docx
The Value Chain Concept Please respond to the following·.docx
Komlin1
 
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docxThe wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
Komlin1
 
The Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docxThe Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docx
Komlin1
 
The United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docxThe United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docx
Komlin1
 
The Value Chain Concept Please respond to the followingDescribe.docx
The Value Chain Concept Please respond to the followingDescribe.docxThe Value Chain Concept Please respond to the followingDescribe.docx
The Value Chain Concept Please respond to the followingDescribe.docx
Komlin1
 

More from Komlin1 (20)

Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docxTheodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
Theodore Robert (Ted) BundyReview the case of Theodore Robert (Ted.docx
 
Theory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docxTheory and Research Related to Social Issue By now, you have had t.docx
Theory and Research Related to Social Issue By now, you have had t.docx
 
Theory and the White-Collar OffenderOur previous week’s discussion.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docxTheory and the White-Collar OffenderOur previous week’s discussion.docx
Theory and the White-Collar OffenderOur previous week’s discussion.docx
 
There are 2 questions part A and B. All questions and relevant att.docx
There are 2 questions part A and B. All questions and relevant att.docxThere are 2 questions part A and B. All questions and relevant att.docx
There are 2 questions part A and B. All questions and relevant att.docx
 
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docxThere are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
There are 2 discussions Topic 1 & Topic 2 (They both require refere.docx
 
Theoretical PerspectiveIdentify at least one human developme.docx
Theoretical PerspectiveIdentify at least one human developme.docxTheoretical PerspectiveIdentify at least one human developme.docx
Theoretical PerspectiveIdentify at least one human developme.docx
 
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docxTHEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
THEIEPGOALSSHOULD BE WRITTEN INAWORDDO.docx
 
Theories of Behavior TimelineComplete the following tabl.docx
Theories of Behavior TimelineComplete the following tabl.docxTheories of Behavior TimelineComplete the following tabl.docx
Theories of Behavior TimelineComplete the following tabl.docx
 
Thematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docxThematic Issues Globalization; Islam & the West.docx
Thematic Issues Globalization; Islam & the West.docx
 
The written portion of the research paper should be 9-11 pages in le.docx
The written portion of the research paper should be 9-11 pages in le.docxThe written portion of the research paper should be 9-11 pages in le.docx
The written portion of the research paper should be 9-11 pages in le.docx
 
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docxThe World since 1945Country Report- SAUDI ARABIA     Histo.docx
The World since 1945Country Report- SAUDI ARABIA     Histo.docx
 
The world runs on Big Data.  Traditionally, Data has been expressed .docx
The world runs on Big Data.  Traditionally, Data has been expressed .docxThe world runs on Big Data.  Traditionally, Data has been expressed .docx
The world runs on Big Data.  Traditionally, Data has been expressed .docx
 
the    1.The collaborative planning Methodology is the f.docx
the    1.The collaborative planning Methodology is the f.docxthe    1.The collaborative planning Methodology is the f.docx
the    1.The collaborative planning Methodology is the f.docx
 
The word stereotype originally referred to a method used by printers.docx
The word stereotype originally referred to a method used by printers.docxThe word stereotype originally referred to a method used by printers.docx
The word stereotype originally referred to a method used by printers.docx
 
The Value of Critical Thinking  Please respond to the followin.docx
The Value of Critical Thinking  Please respond to the followin.docxThe Value of Critical Thinking  Please respond to the followin.docx
The Value of Critical Thinking  Please respond to the followin.docx
 
The Value Chain Concept Please respond to the following·.docx
The Value Chain Concept Please respond to the following·.docxThe Value Chain Concept Please respond to the following·.docx
The Value Chain Concept Please respond to the following·.docx
 
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docxThe wealth and energy between 1880 and 1910 was a unique and dynamic.docx
The wealth and energy between 1880 and 1910 was a unique and dynamic.docx
 
The Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docxThe Value of Research in Social PolicyWhile research can be intere.docx
The Value of Research in Social PolicyWhile research can be intere.docx
 
The United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docxThe United States’ foreign policy until the end of the nineteenth ce.docx
The United States’ foreign policy until the end of the nineteenth ce.docx
 
The Value Chain Concept Please respond to the followingDescribe.docx
The Value Chain Concept Please respond to the followingDescribe.docxThe Value Chain Concept Please respond to the followingDescribe.docx
The Value Chain Concept Please respond to the followingDescribe.docx
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 

The Noland national flag is a square showing the following pattern. .docx

  • 1. The Noland national flag is a square showing the following pattern. Write a method public void drawNoland(int n) that draws the Noland flag on the screen with a height of n pixels. Your method should create and use a SimpleCanvas (as used in lectures and laboratories) to draw on. All of the colors needed are pre-constructed Color objects. Solution import javax.swing.*; import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import java.util.Observer; import java.util.Random; public class SimpleCanvas { private static int num = 0; private static Color[] colorArray = { Color.green, Color.cyan, new Color(204,0,204), Color.gray}; private ModelDisplay modelDisplay = null; private Picture picture = null; private int width = 15; private int height = 18;
  • 2. private int xPos = 0; private int yPos = 0; private double heading = 0; // default is facing north private Pen pen = new Pen(); private Color bodyColor = null; private Color shellColor = null; private Color infoColor = Color.black; private boolean visible = true; private boolean showInfo = false; private String name = "No name"; public SimpleCanvas(int x, int y) { xPos = x; yPos = y; bodyColor = colorArray[num % colorArray.length]; setPenColor(bodyColor); num++; } public SimpleCanvas(int x, int y, ModelDisplay display) { this(x,y); // invoke constructor that takes x and y modelDisplay = display; display.addModel(this); } public SimpleCanvas(ModelDisplay display)
  • 3. { // invoke constructor that takes x and y this((int) (display.getWidth() / 2), (int) (display.getHeight() / 2)); modelDisplay = display; display.addModel(this); } public SimpleCanvas(int x, int y, Picture picture) { this(x,y); // invoke constructor that takes x and y this.picture = picture; this.visible = false; } public SimpleCanvas(Picture picture) { // invoke constructor that takes x and y this((int) (picture.getWidth() / 2), (int) (picture.getHeight() / 2)); this.picture = picture; this.visible = false; } public double getDistance(int x, int y) { int xDiff = x - xPos; int yDiff = y - yPos; return (Math.sqrt((xDiff * xDiff) + (yDiff * yDiff)));
  • 4. } public void turnToFace(SimpleCanvas turtle) { turnToFace(turtle.xPos,turtle.yPos); } public void turnToFace(int x, int y) { double dx = x - this.xPos; double dy = y - this.yPos; double arcTan = 0.0; double angle = 0.0; // avoid a divide by 0 if (dx == 0) { // if below the current turtle if (dy > 0) heading = 180; // if above the current turtle else if (dy < 0) heading = 0; } // dx isn't 0 so can divide by it else {
  • 5. arcTan = Math.toDegrees(Math.atan(dy / dx)); if (dx < 0) heading = arcTan - 90; else heading = arcTan + 90; } // notify the display that we need to repaint updateDisplay(); } public Picture getPicture() { return this.picture; } public void setPicture(Picture pict) { this.picture = pict; } public ModelDisplay getModelDisplay() { return this.modelDisplay; } public void setModelDisplay(ModelDisplay theModelDisplay) { this.modelDisplay = theModelDisplay; } public boolean getShowInfo() { return this.showInfo; } public void setShowInfo(boolean value) { this.showInfo = value; } public Color getShellColor() { Color color = null; if (this.shellColor == null && this.bodyColor != null) color = bodyColor.darker(); else color = this.shellColor;
  • 6. return color; } public void setShellColor(Color color) { this.shellColor = color; } public Color getBodyColor() { return this.bodyColor; } public void setBodyColor(Color color) { this.bodyColor = color;} public void setColor(Color color) { this.setBodyColor(color); } public Color getInfoColor() { return this.infoColor; } public void setInfoColor(Color color) { this.infoColor = color; } public int getWidth() { return this.width; } public int getHeight() { return this.height; } public void setWidth(int theWidth) { this.width = theWidth; } public void setHeight(int theHeight) { this.height = theHeight; } public int getXPos() { return this.xPos; } public int getYPos() { return this.yPos; } public Pen getPen() { return this.pen; } public void setPen(Pen thePen) { this.pen = thePen; } public boolean isPenDown() { return this.pen.isPenDown(); } public void setPenDown(boolean value) { this.pen.setPenDown(value); } public void penUp() { this.pen.setPenDown(false);} public void penDown() { this.pen.setPenDown(true);} public Color getPenColor() { return this.pen.getColor(); }
  • 7. public void setPenColor(Color color) { this.pen.setColor(color); } public void setPenWidth(int width) { this.pen.setWidth(width); } public int getPenWidth() { return this.pen.getWidth(); } public double getHeading() { return this.heading; } public void setHeading(double heading) { this.heading = heading; } public String getName() { return this.name; } public void setName(String theName) { this.name = theName; } public boolean isVisible() { return this.visible;} public void hide() { this.setVisible(false); } public void show() { this.setVisible(true); } public void setVisible(boolean value) { // if the turtle wasn't visible and now is if (visible == false && value == true) { // update the display this.updateDisplay();
  • 8. } // set the visibile flag to the passed value this.visible = value; } public void updateDisplay() { // check that x and y are at least 0 if (xPos < 0) xPos = 0; if (yPos < 0) yPos = 0; // if picture if (picture != null) { if (xPos >= picture.getWidth()) xPos = picture.getWidth() - 1; if (yPos >= picture.getHeight()) yPos = picture.getHeight() - 1; Graphics g = picture.getGraphics(); paintComponent(g); } else if (modelDisplay != null) {
  • 9. if (xPos >= modelDisplay.getWidth()) xPos = modelDisplay.getWidth() - 1; if (yPos >= modelDisplay.getHeight()) yPos = modelDisplay.getHeight() - 1; modelDisplay.modelChanged(); } } public void forward(int pixels) { int oldX = xPos; int oldY = yPos; // change the current position xPos = oldX + (int) (pixels * Math.sin(Math.toRadians(heading))); yPos = oldY + (int) (pixels * - Math.cos(Math.toRadians(heading))); // add a line from the old position to the new position to the pen pen.addLine(oldX,oldY,xPos,yPos); // update the display to show the new line updateDisplay(); }
  • 10. public void backward(int pixels) { forward(-pixels); } public void moveTo(int x, int y) { this.pen.addLine(xPos,yPos,x,y); this.xPos = x; this.yPos = y; this.updateDisplay(); } } } // end of class