有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我如何为这个由三条线组成的三角形填充颜色?

所以,我的老师希望我们在这个周末为一个项目建造一座房子,我可以想出如何为我的三角形填充颜色,我正在用三角形做屋顶。我对java非常陌生,所以我的代码很粗糙,但如果有人能帮助我,那就太好了!(请注意,我的老师并不真正关心代码的长度,只关心我们如何解决这个问题)。 这是我的密码:

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);
   }
}

共 (1) 个答案

  1. # 1 楼答案

    好吧,也许是时候学点新东西了

    /* 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);