I need help on an assignment I have. The first part was to write a program that would declare an array with 150 elements, assign the "#" symbol to each element, then print the elements in such a way that it would spell a letter (in this case, "A") within a 10x15 grid pattern. The elements used to write the letter would be filled with a blank. That was easy. However, the second part requires me to create an applet in which I draw a rectangular grid with 15x10 grid (each cell being 10x10 pixels). Each cell is supposed to correspond to one element in the array. If there is a pound sign in the array element, the cell should be blue and filled. If there is a space in the array element, the cell should be blue and unfilled (the cell should appear white, the default applet backgroud color). I'm also supposed to use the code I used in the first part of the assignment. I have no trouble creating a grid, but corresponding a blue square with the "#" is something I'm not able to figure out... an example of what my instructor wants is here:http://i907.photobucket.com/albums/ac280/bluesilver89/csproject.jpg

Here's the code I have so far for the second part of my assignment:

import javax.swing.JApplet;
import java.awt.*;

public class Lab8b extends JApplet
{
	public void paint(Graphics g)
	{
	
		char letterShape[] = new char[150];
		
		for (int i = 0; i < letterShape.length; i++)
		{
			letterShape[i] = '#';
		}
		
		letterShape[25] = ' ';
		letterShape[26] = ' ';
		letterShape[32] = ' ';
		letterShape[35] = ' ';
		letterShape[36] = ' ';
		letterShape[43] = ' ';
		letterShape[44] = ' ';
		letterShape[47] = ' ';
		letterShape[54] = ' ';
		letterShape[57] = ' ';
		letterShape[63] = ' ';
		letterShape[68] = ' ';
		letterShape[73] = ' ';
		letterShape[78] = ' ';
		letterShape[79] = ' ';
		letterShape[83] = ' ';
		letterShape[84] = ' ';
		letterShape[85] = ' ';
		letterShape[86] = ' ';
		letterShape[87] = ' ';
		letterShape[88] = ' ';
		letterShape[93] = ' ';
		letterShape[98] = ' ';
		letterShape[100] = ' ';
		letterShape[102] = ' ';
		letterShape[109] = ' ';
		letterShape[110] = ' ';
		letterShape[112] = ' ';
		letterShape[119] = ' ';
		letterShape[120] = ' ';
		letterShape[121] = ' ';
		letterShape[122] = ' ';
		letterShape[128] = ' ';
		letterShape[129] = ' ';


			for (int x = 0; x <= 90; x = x + 10)
			{
				for (int y = 0; y <= 140; y = y + 10)
  				{
					g.drawRect(x, y, 10, 10);
				} // end for
			} // end for
			


	
	} // end main
} // end class

I'd greatly appreciate any help - I suspect it involves the use of a loop, or possibly modifying the array, but I'm not sure.

I haven't worked with Applets, but I think they work the same way as Swing basically? (Edit: confirmed - one whole point of JApplet is that it works with Swing). Use a JPanel or whatever the standard applet window is called, make the layout a GridLayout(x, y) where x and y are the 10x15 (the number of cells you need). Then add one JButton for each cell. Set the text of each JButton to "#" and color it blue or white depending on what it should be.

I realize that is somewhat undetailed, but I think you are going wrong by trying to draw all these rectangles yourself. If you use a strategy similar to what I mentioned above, it will be a lot easier.

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.