Skip to content

Instantly share code, notes, and snippets.

@Jordan-Cottle
Last active October 8, 2019 00:05
Show Gist options
  • Save Jordan-Cottle/73d503b46592834d694d61698238e4e0 to your computer and use it in GitHub Desktop.
Save Jordan-Cottle/73d503b46592834d694d61698238e4e0 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
int width = 1024;
int height = 800;
World earth = new World(width, height);
Turtle t0 = new Turtle(earth);
Random r = new Random();
// Draws a random pixel for every pixel on th
for(int i = 0; i < width*height; i++){
int x = r.nextInt(width);
int y = r.nextInt(height);
Color color = new Color((float)Math.random(), (float) Math.random(), (float) Math.random());
t0.drawPixel(x, y, color);
}
}
/**
* Draws a single pixel at the point (x,y) in the world
*
* @param x The x coordinate of the point to draw
* @param y The y coordinate of the point to draw
*/
public void drawPixel(int x, int y){
this.penUp();
this.moveTo(x, y);
this.penDown();
this.moveTo(x, y);
}
/**
* Draws a single pixel at the point (x,y) in the world with a specified color. Does not permanently change the turtle's pen color
*
* @param x The x coordinate of the point to draw
* @param y The y coordinate of the point to draw
* @param c The color of the pixel to draw
*/
public void drawPixel(int x, int y, Color c){
Color previousColor = c;
this.setPenColor(c);
drawPixel(x, y);
this.setPenColor(previousColor);
}
import java.awt.Color;
import java.util.Random;
public class DrawTurtle{
public static void main(String[] args) {
Picture p = new Picture(new FileChooser().pickAFile());
int width = p.getWidth() / 4;
int height = p.getHeight() / 4;
World earth = new World(width, height);
Turtle t0 = new Turtle(earth);
Random r = new Random();
// Draws a random pixel for every pixel on th
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
Color color = p.getPixel(x*4,y*4).getColor();
drawPixel(t0, x, y, color);
}
}
}
/**
* Draws a single pixel at the point (x,y) in the world
*
* @param x The x coordinate of the point to draw
* @param y The y coordinate of the point to draw
*/
public static void drawPixel(Turtle t, int x, int y){
t.penUp();
t.moveTo(x, y);
t.penDown();
t.moveTo(x, y);
}
/**
* Draws a single pixel at the point (x,y) in the world with a specified color. Does not permanently change the turtle's pen color
*
* @param x The x coordinate of the point to draw
* @param y The y coordinate of the point to draw
* @param c The color of the pixel to draw
*/
public static void drawPixel(Turtle t, int x, int y, Color c){
Color previousColor = c;
t.setPenColor(c);
drawPixel(t, x, y);
t.setPenColor(previousColor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment