0

如何使用 Java2D 绘制一个简单的 3D 房子?我想画房子的侧壁,让它看起来像一个简单的 3D 绘图。

// House.java

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GradientPaint;
import java.awt.geom.Rectangle2D;

public class House extends Canvas{

  House(){
    setSize(800,600);
  }
  public void paint(Graphics g){

    // house - foundation
    g.setColor(new Color(0xCDBE70));
    g.fillRect(148,500,304,50);

    // house - front
    g.setColor(new Color(0xFFEC8B));
    g.fillRect(150,350,300,150);

    // house - side 
    }    
  }

//ShowHouse.java

import java.awt.Frame; 
import java.awt.AWTEvent;
import java.awt.event.WindowEvent;

public class ShowHouse extends Frame{ 
  public static void main(String arg[]){
    new ShowHouse(); 
  }
  ShowHouse(){ 
    enableEvents(AWTEvent.WINDOW_EVENT_MASK); 
    setLocation(250,50);
    House canvas = new House();
    add(canvas);
    pack(); 
    setVisible(true);
  }
  public void processWindowEvent(WindowEvent event){
    if(event.getID() == WindowEvent.WINDOW_CLOSING) 
      System.exit(0);
  }
  }
4

1 回答 1

1

我重写了你的 House Class:

//房子.java

public class House extends Canvas{

  House(){
    setSize(800,600);
  }
  public void paint(Graphics g){

    Polygon p = new Polygon();
    p.addPoint(100,50);
    p.addPoint(300,50);
    p.addPoint(250,100);
    p.addPoint(50,100);

    g.fillPolygon(p);

    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(new Color(0xAEAEAE));
    g2.draw(new Line2D.Double(300, 50, 350, 100));
    g2.draw(new Line2D.Double(350, 100, 350, 180));
    g2.draw(new Line2D.Double(350, 180, 250, 180));
    g.setColor(new Color(0xFFEABB));
    p = new Polygon();
    p.addPoint(50,100);
    p.addPoint(250,100);
    p.addPoint(250,180);
    p.addPoint(50,180);

    g.fillPolygon(p);

    g.setColor(new Color(0xFFFFFF));
    p = new Polygon();
    p.addPoint(75,125);
    p.addPoint(225,125);
    p.addPoint(225,150);
    p.addPoint(75,150);

    g.fillPolygon(p);

    }
  }
于 2010-11-24T03:33:18.850 回答