hi, I have one HW that the teacher gave me 18/20. This is teacher comments: Very
ID: 3592253 • Letter: H
Question
hi,
I have one HW that the teacher gave me 18/20. This is teacher comments: Very good program! But the loops for row, column, and diagonal should be in their own methods. Also comments for some methods are too short. 3/3 - part 1 works 1/2 - decomposition into methods is correct 2/2 - handling of arguments is correct 2/2 - drawOneCar is correct 2/2 - column is correct 2/2 - row is correct 2/2 - diagonal is correct 4/5 - comments, style
this is my codes:
/**
* Write a description of class HW2 here.
* 2 parts drawing panel, first one draw alternating grid of circles and squares
second one draw various arrangements of train cars.
* @author (yu kai)
* @version (10/11/2017)
*/
import java.awt.*;
public class HW2{
/**
Method main
param args A parameter
*/
public static void main(String[] args){
// part 1 code using class Drawingpanel
// Draw the panel and call methodes set Colors
int dim;
Color[] colors = {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.BLUE,Color.MAGENTA};
DrawingPanel panel1 = new DrawingPanel(400,400);
Graphics g1 = panel1.getGraphics();
g1.setColor(Color.BLACK);
g1.fillRect(0,0,400,400);
int[] coordinates = {0,100,250,350};
g1.setColor(Color.YELLOW);
int i = 0;
dim = 50;
DrawingPanel panel2 = new DrawingPanel(800,600);
Graphics g2 = panel2.getGraphics();
//part 2 code using for loop to draw multiples cars using dim to change the position for cars row, column, and diagonal
drawOnecar(g2,Color.RED,20,30,40);
for (int j=0;j<4;j++){
drawOnecar(g2,Color.RED, 200 + j * 4*30,100,50);
drawOnecar(g2,Color.RED,400 + j * 15*3, 250 + j *3*22,15);}
for (int k=0;k<5;k++){
drawOnecar(g2,Color.RED,100,200+k*3*20,20);
panel2.setBackground(Color.LIGHT_GRAY);}
// Draw alternating grid of circles and squares using for loop
for (int x = 0; x < 400; x += dim) {
for (int y = 0; y < 400; y += dim) {
Color color = colors[i % colors.length];
if (i % 2 == 0) {
drawCircle(g1, color, x, y, dim);
} else {
drawSquare(g1, color, x, y, dim);
}
i += 1;
}
i += 1;
}
drawString(g1);
}
// Part 1 helper method to draw circle
public static void drawCircle(Graphics g, Color color, int x, int y, int dim) {
g.setColor(color);
g.fillOval(x,y,dim,dim);
}
// Part 1 helper method to draw square
public static void drawSquare(Graphics g, Color color, int x, int y, int dim) {
g.setColor(color);
g.fillRect(x,y,dim,dim);
}
// helper mothod to draw my name using drawString
public static void drawString(Graphics g){
g.drawString("Yu Kai",50,50);
}
public static void drawOnecar(Graphics g, Color color, int x, int y, int dim){
//helper mothod drawOneCar using dim and x, y
dim=dim/2;
g.setColor(Color.BLACK);
g.drawRect(x+dim,y,2*dim,dim);
g.setColor(Color.RED);
g.fillRect(x,y+dim,4*dim,2*dim);
g.setColor(Color.CYAN);
g.fillOval(x+dim/2,y+3*dim,dim,dim);
g.fillOval(x+5*dim/2,y+3*dim,dim,dim);
}
}
How I can change?
Explanation / Answer
import java.awt.*;
public class HW2{
/**
Method main
param args A parameter
*/
public static void main(String[] args){
// part 1 code using class Drawingpanel
// Draw the panel and call methodes set Colors
int dim;
Color[] colors = {Color.RED,Color.ORANGE,Color.YELLOW,Color.GREEN,Color.BLUE,Color.MAGENTA};
DrawingPanel panel1 = new DrawingPanel(400,400);
Graphics g1 = panel1.getGraphics();
g1.setColor(Color.BLACK);
g1.fillRect(0,0,400,400);
int[] coordinates = {0,100,250,350};
g1.setColor(Color.YELLOW);
int i = 0;
dim = 50;
DrawingPanel panel2 = new DrawingPanel(800,600);
Graphics g2 = panel2.getGraphics();
//part 2 code using for loop to draw multiples cars using dim to change the position for cars row, column, and diagonal
drawOnecar(g2,Color.RED,20,30,40);
for (int j=0;j<4;j++){
drawOnecar(g2,Color.RED, 200 + j * 4*30,100,50);
drawOnecar(g2,Color.RED,400 + j * 15*3, 250 + j *3*22,15);}
for (int k=0;k<5;k++){
drawOnecar(g2,Color.RED,100,200+k*3*20,20);
panel2.setBackground(Color.LIGHT_GRAY);}
// Draw alternating grid of circles and squares using for loop
for (int x = 0; x < 400; x += dim) {
for (int y = 0; y < 400; y += dim) {
Color color = colors[i % colors.length];
if (i % 2 == 0) {
drawCircle(g1, color, x, y, dim);
} else {
drawSquare(g1, color, x, y, dim);
}
i += 1;
}
i += 1;
}
drawString(g1);
}
// Part 1 helper method to draw circle
public static void drawCircle(Graphics g, Color color, int x, int y, int dim) {
g.setColor(color);
g.fillOval(x,y,dim,dim);
}
// Part 1 helper method to draw square
public static void drawSquare(Graphics g, Color color, int x, int y, int dim) {
g.setColor(color);
g.fillRect(x,y,dim,dim);
}
// helper mothod to draw my name using drawString
public static void drawString(Graphics g){
g.drawString("Yu Kai",50,50);
}
public static void drawOnecar(Graphics g, Color color, int x, int y, int dim){
//helper mothod drawOneCar using dim and x, y
dim=dim/2;
g.setColor(Color.BLACK);
g.drawRect(x+dim,y,2*dim,dim);
g.setColor(Color.RED);
g.fillRect(x,y+dim,4*dim,2*dim);
g.setColor(Color.CYAN);
g.fillOval(x+dim/2,y+3*dim,dim,dim);
g.fillOval(x+5*dim/2,y+3*dim,dim,dim);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.