I have to write a code that displays a 4 x 4 grid of faces. I am stuck and this is wat i have so far:

import java.awt.Graphics;
	import java.util.Scanner;
	import javax.swing.JApplet;

	public class Program7 extends JApplet {
		
		public void init() {
			setSize(2000, 2000);
		}
		
		/** 
		 * Draw one happy face at position (xOffset,yOffset) rather
		 * than at (0,0)
		 */
		public void happyFace(int xOffset, int yOffset, Graphics canvas) {
			// Put your code here
			canvas.drawOval(100, 50, 200, 200);
			canvas.fillOval(155, 100, 20, 30);
			canvas.fillOval(230, 100, 20, 30);
			canvas.drawOval(150, 175, 100, 50);
			canvas.fillOval(150, 175, 100, 50);
			canvas.drawOval(195, 140, 10, 20);
			canvas.fillOval(195, 140, 10, 20);
			canvas.drawLine(150, 65, 80, 10);
			canvas.drawLine(150, 65, 100, 10);
			canvas.drawLine(150, 65, 90, 10);
			canvas.drawLine(150, 65, 110, 10);
			canvas.drawLine(150, 65, 120, 10);
			canvas.drawLine(150, 65, 130, 10);
			canvas.drawLine(150, 65, 140, 10);
			canvas.drawLine(150, 65, 150, 10);
			canvas.drawLine(150, 65, 160, 10);
			canvas.drawLine(150, 65, 170, 10);
			canvas.drawLine(150, 65, 180, 10);
			canvas.drawLine(150, 65, 190, 10);
			canvas.drawLine(150, 65, 200, 10);
			canvas.drawLine(150, 65, 210, 10);
			canvas.drawLine(150, 65, 220, 10);
			canvas.drawLine(150, 65, 230, 10);
			canvas.drawLine(150, 65, 240, 10);
			canvas.drawLine(150, 65, 250, 10);
		}

		/**
		 * return true if value is an odd number, otherwise return false
		 */
		public boolean isOdd(int value) {
			// Put your code here
		}
		
		public void paint(Graphics canvas) {
			// Put your code here
		}
		
		/** 
		 * Draw one sad face at position (xOffset,yOffset) rather
		 * than at (0,0)
		 */
		public void sadFace(int xOffset, int yOffset, Graphics canvas) {
			// Put your code here
			canvas.drawOval(100, 50, 200, 200);
			canvas.fillOval(155, 100, 20, 30);
			canvas.fillOval(230, 100, 20, 30);
			canvas.drawOval(150, 175, 100, 50);
			canvas.fillOval(150, 175, 100, 50);
			canvas.drawOval(195, 140, 10, 20);
			canvas.fillOval(195, 140, 10, 20);
			canvas.drawLine(150, 65, 80, 10);
			canvas.drawLine(150, 65, 100, 10);
			canvas.drawLine(150, 65, 90, 10);
			canvas.drawLine(150, 65, 110, 10);
			canvas.drawLine(150, 65, 120, 10);
			canvas.drawLine(150, 65, 130, 10);
			canvas.drawLine(150, 65, 140, 10);
			canvas.drawLine(150, 65, 150, 10);
			canvas.drawLine(150, 65, 160, 10);
			canvas.drawLine(150, 65, 170, 10);
			canvas.drawLine(150, 65, 180, 10);
			canvas.drawLine(150, 65, 190, 10);
			canvas.drawLine(150, 65, 200, 10);
			canvas.drawLine(150, 65, 210, 10);
			canvas.drawLine(150, 65, 220, 10);
			canvas.drawLine(150, 65, 230, 10);
			canvas.drawLine(150, 65, 240, 10);
			canvas.drawLine(150, 65, 250, 10);
		}

	}

When you call a method like canvas.drawOval(100, 50, 200, 200); , the x and y coordinates are offset from (0,0). In other words, this method will draw an oval with the top left corner of its bounding box 100 pixels to the right and 50 pixels down from the top left corner of the canvas.

If you are asked to draw an oval offset from position (xOffset,yOffset) rather than (0,0), then you should instead call the method like this:

canvas.drawOval(xOffset+100, yOffset+50, 200, 200);

Now if I call your happyFace method with xOffset=0 and yOffset=0, then the new code has the same effect as the original in your post. But if I call the method with xOffset=500 and yOffset=500, then the code will draw the same oval but at a new position. Using this method I can call the method many times with different x & y offset values and draw the same image in many different locations on the screen.

This is my new code. I was able to have one row of four faces, but I still need to make it a 4x4 grid of the alternating faces.

import java.awt.Graphics;
	import java.util.Scanner;
	import javax.swing.JApplet;

	public class Program7 extends JApplet {
		
		public void init() {
			setSize(2000, 2000);
		}
		
		/** 
		 * Draw one happy face at position (xOffset,yOffset) rather
		 * than at (0,0)
		 */
		
		int xOffset = 0;
		int yOffset = 0;
		
		public void happyFace(int xOffset, int yOffset, Graphics canvas) {
			canvas.drawOval(xOffset+100, yOffset+50, 200, 200);
			canvas.fillOval(xOffset+155, yOffset+100, 20, 30);
			canvas.fillOval(xOffset+230, yOffset+100, 20, 30);
			canvas.drawOval(xOffset+150, yOffset+175, 100, 50);
			
		}

		/**
		 * return true if value is an odd number, otherwise return false
		 */
		public boolean isOdd(int value) {
			// Put your code here
		}
		
		
		
		public void paint(Graphics canvas) {
			// Put your code here
			for(int row = 0; row<4; row++){
				happyFace (xOffset+50, 50, canvas);
				xOffset=xOffset+200;
		}
		}
		/** 
		 * Draw one sad face at position (xOffset,yOffset) rather
		 * than at (0,0)
		 */
		public void sadFace(int xOffset, int yOffset, Graphics canvas) {
			// Put your code here
			canvas.drawOval(xOffset+100, yOffset+50, 200, 200);
			canvas.fillOval(xOffset+155, yOffset+100, 20, 30);
			canvas.fillOval(xOffset+230, yOffset+100, 20, 30);
			canvas.drawOval(xOffset+150, yOffset+175, 100, 50);
		}

	}

1. You are only using happyFace. I'm guessing you should use sadFace too.
2. You are only varying xOffset, so you are getting the faces in one row. If you vary yOffset as well, you can get the grid you want.

I am still not able to get the faces to repeat but thisis what i have:

import java.awt.Graphics;
import java.util.Scanner;
import javax.swing.JApplet;

public class Program7 extends JApplet {
	public void init() {
		setSize(2000, 2000);
	}

	/**
	 * Draw one happy face at position (xOffset,yOffset) rather than at (0,0)
	 */

	public void happyFace(int xOffset, int yOffset, Graphics canvas) {
		canvas.drawOval(xOffset + 100, yOffset + 50, 200, 200);
		canvas.fillOval(xOffset + 155, yOffset + 100, 20, 30);
		canvas.fillOval(xOffset + 230, yOffset + 100, 20, 30);
		canvas.drawArc(xOffset + 150, yOffset + 160, 100, 50, 180, 180);
	}

	/**
	 * return true if value is an odd number, otherwise return false
	 */
	public boolean isOdd(int value) {
		if (value % 2 == 1) {
			return true;
		} else {
			return false;
		}
	}

	public void paint(Graphics canvas) {
		int xOffset = 0;
		int yOffset = 0;

		for (int row = 0; row < 4; row++) {

			for (int column = 0; column < 4; column++) {
				if (isOdd(row + column)) {
					sadFace(yOffset + 100, 250, canvas);
					yOffset = yOffset + 200;
				} else {
					happyFace(xOffset + 50, 50, canvas);
					xOffset = xOffset + 200;
				}

			}
		}
	}

	/**
	 * Draw one sad face at position (xOffset,yOffset) rather than at (0,0)
	 */
	public void sadFace(int xOffset, int yOffset, Graphics canvas) {
		canvas.drawOval(xOffset + 100, yOffset + 50, 200, 200);
		canvas.fillOval(xOffset + 155, yOffset + 100, 20, 30);
		canvas.fillOval(xOffset + 230, yOffset + 100, 20, 30);
		canvas.drawOval(xOffset + 150, yOffset + 175, 100, 50);
	}
}

You have 2 nested loops in your paint method - this is good! But you are incrementing both xOffset and yOffset in the inner loop. Try only incrementing yOffset in the inner loop (regardless of which face you draw). Then in the outer loop, reset yOffset back to 0 and increment xOffset.

Please post your code inside code tags in the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.