CrypticZero CrypticZero - 2 months ago 11
Java Question

How do I fill in the color for this triangle that I made from three lines?

So, my teacher wanted us to make a house for a project this weekend, and I can figure out how to fill in the color for my triangle that I am using for a roof. I am very new at java, so my code is rather ruff, but if anyone could help me, that would be great!(Note that my teacher doesn't really care about code length, just that we figure out house to do it).
Here is my code:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;

public class HouseMaker
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setSize(3000, 3000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setBackground(Color.BLUE);

HouseComponent component = new HouseComponent();
frame.add(component);

frame.setVisible(true);
}
}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;
import java.awt.Polygon;

/*
A component that draws an house with a sun and other things
*/
public class HouseComponent extends JComponent
{
public void paintComponent(Graphics g)
{
// Recover Graphics2D
Graphics2D g2 = (Graphics2D) g;

// Draw the house frame
g2.setColor(Color.GRAY);
Rectangle frame = new Rectangle(300, 400, 500, 230);
g2.fill(frame);

// Draw the roof
Line2D.Double roof = new Line2D.Double(300, 400, 800, 400);
g2.setColor(Color.RED);
g2.drawLine(300, 400, 550, 200);
g2.setColor(Color.RED);
g2.drawLine(800, 400, 550, 200);
g2.setColor(Color.RED);
g2.draw(roof);

//sun in the sky
g2.setColor(Color.YELLOW);
g.drawOval(50,50,100,100);
g2.setColor(Color.YELLOW);
g2.fillOval(50,50,100,100);
g2.drawLine(50,50,20,20);
g2.setColor(Color.YELLOW);
g2.drawLine(150,150,180,180);
g2.drawLine(150,50,180,20);
g2.drawLine(50,150,20,180);
g2.drawLine(30,100,-10,100);
g2.drawLine(100,30,100,-10);
g2.drawLine(170,100,210,100);
g2.drawLine(100,170,100,210);

//Draw the door

g2.setColor(Color.BLACK);
Rectangle door = new Rectangle(450, 510, 50, 120);
g2.fill(door);

//Draw door knob
g2.setColor(Color.WHITE);
g2.drawOval(450,560,10,10);
g2.setColor(Color.WHITE);
g2.fillOval(450,560,10,10);

//Draw three windows

g2.setColor(Color.BLACK);
Rectangle window = new Rectangle(350, 520, 50, 50);
g2.fill(window);
g2.setColor(Color.BLACK);
Rectangle window1 = new Rectangle(550, 520, 50, 50);
g2.fill(window1);
g2.setColor(Color.BLACK);
Rectangle window2 = new Rectangle(650, 520, 50, 50);
g2.fill(window2);

//Draw window panes
g2.setColor(Color.WHITE);
g2.drawLine(350,545,400,545);
g2.drawLine(375,520,375,570);
g2.drawLine(550,545,600,545);
g2.drawLine(575,520,575,570);
g2.drawLine(650,545,700,545);
g2.drawLine(675,520,675,570);

//Draw ground
g2.setColor(Color.GREEN);
Rectangle ground = new Rectangle(0, 630, 1500, 1500);
g2.fill(ground);
}
}

Answer

Ok, maybe is time to learn something new?

/* Have a look over java.awt.geom.GeneralPath*/
// Draw the roof
GeneralPath shape=new GeneralPath();
shape.moveTo(300, 400)
shape.lineTo(800, 400);
shape.lineTo(550, 200);
shape.closePath(); // easier than shape.lineTo(300, 400);
g2.setColor(Color.RED);
g2.fill(shape);

// let's get a tinge of pink for the outline;
Stroke origStroke=g2.getStroke(); // just to restore it afterwards
g2.setColor(Color.PINK);
g2.setStroke(new BasicStroke(0.5f)); // WHOA, is that something new?
g2.draw(shape); // reusing it, why bother creating other lines?

// as we don't want the next drawing to go with a thin line,
// restore the old stroke
g2.setStroke(origStroke); 
Comments