0

I need help troubleshooting some code. I am taking my first java class, so its pretty basic. Essentially, I need to use a switch statement to draw a series of 3 houses, but with each house drawn in a different location and with a different wall color. Beneath is the code I've been endlessly playing with.

public class Neighborhood extends Applet {
public void paint (Graphics page) { 
    int xOffset = 50; 
    int yOffset = 0; 
    Color houseColor = Color.yellow; 

switch (houseColor) { 
case 0: houseColor = Color.red; 
case 1: houseColor = Color.blue; 
case 2: houseColor = Color.green; 




  Polygon poly = new Polygon();                // Roof Polygon
  poly.addPoint (50,90);
  poly.addPoint (150, 50);
  poly.addPoint (250, 90);
  page.setColor (new Color(218,165,32));      // Custom brown color
  page.fillPolygon (poly);

  page.setColor (Color.black);  
  page.drawLine (50, 90, 150, 50);     // Roof outline
  page.drawLine (150, 50, 250, 90);

  page.setColor (Color.green);            
  page.fillRect (50, 90, 200, 100);  // House base with houseColor
  page.setColor (Color.black);  
  page.drawRect (50, 90, 200, 100);  // House outline

  page.setColor (Color.black);
  page.fillRect (75, 110, 30, 25);   // Window 1
  page.fillRect (190, 110, 30, 25);  // Window 2    

  page.setColor (Color.white);
  page.drawLine (75, 123, 105, 123);   // Window Frame 1
  page.drawLine (89, 110, 89, 134);
  page.fillRect (70, 110, 5, 25);      // Shutter 1
  page.fillRect (105, 110, 5, 25);     // Shutter 2

  page.drawLine (75+115, 123, 105+115, 123);   // Window Frame 2
  page.drawLine (89+115, 110, 89+115, 134);
  page.fillRect (70+115, 110, 5, 25);     // Shutter 3
  page.fillRect (105+115, 110, 5, 25);     // Shutter 4

  page.setColor (Color.red);
  page.fillRect (130, 150, 35, 40);  // Door

  page.setColor (Color.blue);           
  page.fillOval (155, 170, 4, 4);    // Door knob
}
}}

Its fairly straight-forward, I don't need it to interact or do anything crazy. I've changed this code about a million times trying to get it to work, but I've realized its come time for better eyes to take a look. Any help is appreciated. Thanks.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mike L
  • 486
  • 5
  • 16
  • 33
  • 1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jul 09 '13 at 06:51
  • Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Jul 09 '13 at 06:51

1 Answers1

0

The first thing is that you forgot the break statements.

switch (houseColor) { 
  case 0: houseColor = Color.red; break;
  case 1: houseColor = Color.blue; break;
  case 2: houseColor = Color.green; break;

If you don't have the breaks, it will fall through to the next statement. Also, you are not using the color. Is that intentional?

If you want to draw 3 houses, do something like this:

private void drawHouses() {
    // make an array of colors
    Color[] colors = new Color[] { Color.RED, Color.BLUE, Color.GREEN };

    // loop through the colors
    for(int i = 0 ; i < colors.length; i ++ ) {
       // use the index of the loop for the position of the house and for the current color.
       drawHouse(i * 50, 100, colors[i]);
    }
}

private void drawHouse(int x, int y, Color color) {
    // draw house here

}

Also, currently you are using hardcoded values. That's fine, but you need to add the x and y parameters to x and y axis respectively. For example, if you want to draw a square at 100, 150, of 50 by 50, you'll need to increment the 100 by x (100+x) and the 150 by y (150+y)

Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
  • But isn't that only if I want it to stop after a specific case? I want it to do all 3. I need to create 3 different houses in the same applet, but they need to appear in 3 different colors and locations. – Mike L Jul 08 '13 at 19:59
  • Then you should not use a switch. Just make 3 method calls: drawHouse(0, 100, Color.RED); drawHouse(100, 100, Color.BLUE); drawHouse(200, 100, Color.GREEN); – Erik Pragt Jul 08 '13 at 20:00
  • I was specifically instructed to "use a loop that iterates 3 times" ... so I can't do them one by one, unfortunately. – Mike L Jul 08 '13 at 20:05
  • That's fine. You were not using a loop, you were using a switch. I'll update my code. – Erik Pragt Jul 08 '13 at 20:06