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

DOCX
Applications
PDF
I wanna add the shape creator like rectangular , square , circle etc.pdf
PDF
PDF
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
PDF
can you add a delete button and a add button to the below program. j.pdf
PDF
Refactoring
DOCX
This code currently works... Run it and get a screen shot of its .docx
PPT
java graphics
Applications
I wanna add the shape creator like rectangular , square , circle etc.pdf
Based on CubeNetImpl, write CubeImpl and CubeSolver in Java language.pdf
can you add a delete button and a add button to the below program. j.pdf
Refactoring
This code currently works... Run it and get a screen shot of its .docx
java graphics

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

PDF
building_games_with_ruby_rubyconf
PDF
building_games_with_ruby_rubyconf
PPTX
java_for_future_15-Multithreaded-Graphics.pptx
DOCX
Please finish everything marked TODO BinaryNode-java public class Bi.docx
PDF
Scala vs Java 8 in a Java 8 World
PPT
ch03g-graphics.ppt
PDF
C# v8 new features - raimundas banevicius
PDF
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
PDF
Test beautycleanness
PDF
Please help!!I wanted to know how to add a high score to this prog.pdf
PDF
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
PPTX
PDF
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
KEY
The Canvas API for Rubyists
PPT
Kill the DBA
PDF
import java.util.Random;defines the Stock class public class S.pdf
PDF
662305 LAB12
KEY
Sbaw090623
PDF
はじめてのGroovy
DOCX
Computer graphics
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
java_for_future_15-Multithreaded-Graphics.pptx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Scala vs Java 8 in a Java 8 World
ch03g-graphics.ppt
C# v8 new features - raimundas banevicius
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Test beautycleanness
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
Roberto Gallea: Workshop Arduino, giorno #2 Arduino + Processing
The Canvas API for Rubyists
Kill the DBA
import java.util.Random;defines the Stock class public class S.pdf
662305 LAB12
Sbaw090623
はじめてのGroovy
Computer graphics

More from Komlin1 (20)

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

Recently uploaded (20)

PDF
IGGE1 Understanding the Self1234567891011
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
advance database management system book.pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
20th Century Theater, Methods, History.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
International_Financial_Reporting_Standa.pdf
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
My India Quiz Book_20210205121199924.pdf
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
IGGE1 Understanding the Self1234567891011
TNA_Presentation-1-Final(SAVE)) (1).pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
advance database management system book.pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Cambridge-Practice-Tests-for-IELTS-12.docx
20th Century Theater, Methods, History.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
International_Financial_Reporting_Standa.pdf
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Unit 4 Computer Architecture Multicore Processor.pptx
My India Quiz Book_20210205121199924.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
FORM 1 BIOLOGY MIND MAPS and their schemes
Practical Manual AGRO-233 Principles and Practices of Natural Farming

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