This is my first time using a website like this, and I typically wouldn’t, however, I just cannot seem to figure out two parts of my code that are going wrong. I do not want someone to just give me the answer because I do need to learn, but I don’t know where to look after searching all day.
I am having two problems, one within the checkerboard method, occasionally my board will print diagonally and I have tried switching up many things to stop it, and also, my spiral method is always starting its x and y axis at the same place just different multiples of it (hopefully that makes sense & this is when I put the code on a random loop). There is a Graphics file my school has given to me to input the Graphics, hopefully that is not needed. If you can help in any way it would be greatly appreciated. I do not want anyone to do my homework for me, and I think I have done enough to where no one believes otherwise, however if I should be phrasing this a separate way, please let me know.
Thank you, Chase
Here is my Code:
import java.awt.Graphics ; import java.awt.Color ; import java.awt.Polygon; import java.util.Random; public class abstractArt { public static void main (String[] args) { DrawingCanvas canvas = new DrawingCanvas( ); Graphics g = canvas.getGraphics( ); Random rand = new Random(); // loop to draw items // for (int h = 0; h < 10; h++) // { //Generate random Color int red = rand.nextInt(256); int green = rand.nextInt(256); int blue = rand.nextInt(256); int black = rand.nextInt(256); Color randomColor = new Color(red,green,blue,black); red = rand.nextInt(256); green = rand.nextInt(256); blue = rand.nextInt(256); black = rand.nextInt(256); Color randomColor2 = new Color(red,green,blue,black); red = rand.nextInt(256); green = rand.nextInt(256); blue = rand.nextInt(256); black = rand.nextInt(256); Color randomColor3 = new Color(red,green,blue,black); red = rand.nextInt(256); green = rand.nextInt(256); blue = rand.nextInt(256); black = rand.nextInt(256); Color randomColor4 = new Color(red,green,blue,black); int w = rand.nextInt(101) + 50; int v = rand.nextInt(101) + 50; int checkerwidth = rand.nextInt(15) * 10 + 20; int squares = rand.nextInt(8) + 4; int spiralWidth = rand.nextInt (10) + 5; int spirals = rand.nextInt (11) + 4; int stars = rand.nextInt (50) + 5; //Generate random x/y, p/q, a/b quardinate int x = rand.nextInt(canvas.getWidth() - 220); int y = rand.nextInt(canvas.getHeight() - 220); int p = rand.nextInt(canvas.getWidth() - w); int q = rand.nextInt(canvas.getHeight() - w); int a = rand.nextInt(canvas.getWidth() - w); int b = rand.nextInt(canvas.getHeight() - w); Color c1 = randomColor; Color c2 = randomColor2; int width, height, numbCircles, numSquares,numSpokes; // -- or ++ for int's drawCheckerboard(g,200,200,checkerwidth,squares,randomColor,randomColor2); drawStar(g,a,b,w,stars,randomColor3); drawSpiral(g,x + spiralWidth,y + spiralWidth ,spiralWidth,spirals,randomColor4); // } } public static void drawCheckerboard(Graphics g, int p, int q, int checkerwidth, int squares, Color randomColor, Color randomColor2) { int widthPerSqr = checkerwidth/squares; int n=1; // n = square color for(int i = 0; i < squares; i++) //i = row { for(int j = 0; j < squares; j++) //j = col { if ( (i % 2) == (j % 2) ) g.setColor(randomColor); else g.setColor(randomColor2); g.fillRect(p,q,widthPerSqr,widthPerSqr); g.fillRect(p,q,widthPerSqr,widthPerSqr); n++; p += widthPerSqr; } q-=widthPerSqr; p -= checkerwidth; } } public static void drawStar(Graphics g, int a, int b, int width,int numSpokes, Color c1) { int centerX = a + width/2; int centerY = b + width/2; int radius = width/2; double angle = 2*Math.PI/numSpokes; g.setColor(c1); for(int i =0; i<numSpokes; i++){ int spokeX = (int)(Math.cos(i*angle)*radius + centerX); int spokeY = (int)(Math.sin(i*angle)*radius + centerY); g.drawLine(centerX,centerY,spokeX,spokeY); } } public static void drawSpiral(Graphics g, int x, int y, int width, int numSpirals, Color c1) { int x1,x2,x3,x4; int y1,y2,y3,y4; int lineLength = (width/numSpirals)*4; int lineInc = lineLength; int centerX = x + width/2, centerY = x + width/2; for(int i=1; i<numSpirals;i++) { //Calculate starting points of each line of spiral x1= centerX+ lineLength; y1= centerY; x2=x1; y2=y1-(lineLength+lineInc); x3= x2 - (lineLength + 2*lineInc); y3=y2; x4=x3; y4=y3 + (lineLength + 3*lineInc); //draw line segments of spiral g.setColor(c1); g.drawLine(centerX,centerY,x1,y1); g.drawLine(x1,y1,x2,y2); g.drawLine(x2,y2,x3,y3); g.drawLine(x3,y3,x4,y4); //update starting point and line length centerX=x4; centerY=y4; lineLength+=(4*lineInc); } } }