使用 Java2D 绘制 3D 房屋?

发布于 2024-10-04 06:34:15 字数 1163 浏览 6 评论 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);
  }
  }

How do I draw a simple 3D house using Java2D? I want to draw the side wall of the house to make it look like a simple 3D drawing.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2024-10-11 06:34:15

我重写了你的 House 类:

//House.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);

    }
  }

I have rewritten your House Class:

//House.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);

    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文