Complete a program by writing a method called drawGrid that draws a table / grid on the
window from (0,0) to (x,y) where x and y are integers passed to the method drawGrid as
parameters. A method call to drawGrid is given inside mousePressed so you can test your
program by pressing the mouse at different location in the display window. A sample output
is given along side.
A table consists of -
1. a red bordered rectangle with opposite
corners at (0,0) and (x,y)
2. black vertical and horizontal lines from (0,0)
to (x,0) and (0,0) to (0,y) respectively, with a
gap of GAP between them. GAP is a final int
defined at the top of the program.
1
Expert's answer
2012-08-10T09:34:07-0400
public static void drawGrid(int x, int y, Graphics g){ g.setColor(Color.RED); g.drawRect(0,0,x,y); g.setColor(Color.BLACK); int x1=0; int maxY=y/GAP; int maxX=x/GAP; for (int i=0; i<maxX; i++){ x1=GAP*i; g.drawLine(x1, 0, x1, y); } int y1=0; for (int i=0; i<maxY; i++){ y1=GAP*i; g.drawLine(0, y1, x, y1); } }
Comments
Leave a comment