【问题标题】:Coloring a Triangle with 3 colors用 3 种颜色为三角形着色
【发布时间】:2020-07-17 11:57:13
【问题描述】:

请帮助我如何用三种颜色为三角形着色。我把它当作学校的问题, 使用 Java 编程语言

你好,这是代码: 我正在尝试用三种颜色填充三角形,但很难混合颜色来获得它。如果您要解决它(近似值),请尽可能将其发送给我,我将其作为作业和 5-6 天的截止日期

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
public class NotFullVersion2 extends JApplet {

public static void main(String s[]) {

  JFrame frame = new JFrame();
  frame.setTitle("Colors");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  JApplet applet = new NotFullVersion2();
  applet.init();
  applet.addMouseListener(
     new MouseAdapter(){  
        public void mousePressed(MouseEvent e) { 
           System.out.println(e.getX() + " " + e.getY());
        }});

  frame.getContentPane().add(applet);
  frame.pack();
  frame.setVisible(true);
}

ColorPanel panel;

public void init() {

  panel = new ColorPanel();
  Container cp = getContentPane();
  cp.setLayout(new BorderLayout());
  cp.add(panel, BorderLayout.CENTER);
  JPanel p = new JPanel();
  cp.add(p,BorderLayout.EAST);  
}


}

class ColorPanel extends JPanel  {

//int red = 100,green = 100, blue = 100;

public ColorPanel() {

  setPreferredSize(new Dimension(500, 500));
  setBackground(Color.black);
}

public void paintComponent(Graphics g) {

  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D)g;


  for(int i = 0; i < 256; i++) {

     int start = 399; 

     g2.setColor(new Color(0,i,255-i));   

     for(int j = 0; j < 200; j ++) {

        Rectangle rec = new Rectangle(150+j,start - i,1,1);
        g2.fill(rec);

     }

  }


  for(int j = 0; j < 100; j++) {

     int start = 100;

     for(int i = 0; i < 300; i++) {

        if(i < 22) {

           g2.setColor(new Color(255,0,0));
           Rectangle rec = new Rectangle(100 + i,start + j,1,1);
           g2.fill(rec);

        } else if(i > 21 && i < 278) {

           g2.setColor(new Color(255-(i-22),(i-22),0));
           Rectangle rec = new Rectangle(100 + i,start + j,1,1);
           g2.fill(rec);

        } else if(i < 300) {

           g2.setColor(new Color(0,255,0));
           Rectangle rec = new Rectangle(100 + i,start + j,1,1);
           g2.fill(rec);

        }
     }

  }

  GeneralPath closePath1a = new GeneralPath();

  g2.setColor(new Color(0,0,0));

  closePath1a.moveTo(100,100);
  closePath1a.lineTo(100,400);
  closePath1a.lineTo(250,400);
  closePath1a.closePath();

  g2.fill(closePath1a);


  GeneralPath closePath2a = new GeneralPath();

  g2.setColor(new Color(0,0,0));

  closePath2a.moveTo(400,100);
  closePath2a.lineTo(400,400);
  closePath2a.lineTo(250,400);
  closePath2a.closePath();

  g2.fill(closePath2a);


  GeneralPath closePath3a = new GeneralPath();

  g2.setColor(new Color(0,0,0));

  closePath3a.moveTo(100,100);
  closePath3a.lineTo(100,50);
  closePath3a.lineTo(400,50);
  closePath3a.lineTo(400,100);
  closePath3a.closePath();

  g2.fill(closePath3a);



 }  


 }

【问题讨论】:

  • 你能展示你用来实现左边的代码吗?
  • 1 分钟让我知道如何将代码发送给您
  • 你可以edit提问。
  • 对不起,我不知道如何发布代码,我是 Stackoverflow 的新手,我会尽快发布。
  • 您好,我编辑了问题并输入了代码,抱歉迟到了,我是这个网站的新手

标签: java graphics colors


【解决方案1】:

像这样为三角形着色相当于计算三角形内每个像素的重心坐标。以下未经测试代码计算三角形 ABC 内每个像素的重心坐标,然后使用它为该像素着色:

private float area(float Ax, float Ay, float Bx, float By, float Cx, float Cy) {
    return  0.5*((Ax - Cx)*(By - Ay) - (Ax - Bx)*(Cy - Ay));
}

private void paintTriangle(Graphics g, float Ax, float Ay, float Bx, float By, float Cx, float Cy) {
    // calculate the bounding box of the triangle:
    int minX = Math.round(Math.min(Ax, Math.min(Bx, Cx)));
    int minY = Math.round(Math.min(Ay, Math.min(By, Cy)));
    int maxX = Math.round(Math.max(Ax, Math.max(Bx, Cx)));
    int maxY = Math.round(Math.max(Ay, Math.max(By, Cy)));

    // loop for each pixel in the bounding box of the triangle
    for(int y = minY; y < maxY; ++y) {
        for(int x = minX; x < maxX; ++x) {
            // center of the pixel (x,y)
            float Px = x + 0.5, Py = y + 0.5;

            // barycentric coordinates of P
            float denom = area(Ax, Ay, Bx, By, Cx, Cy);
            float b0 = area(Px, Py, Bx, By, Cx, Cy)/denom;
            float b1 = area(Ax, Ay, Px, Py, Cx, Cy)/denom;
            float b2 = area(Ax, Ay, Bx, By, Px, Py)/denom;

            // discard pixels outside the triangle
            if(b0 < 0 || b1 < 0 || b2 < 0)
                continue;

            // paint a pixel of color (b0,b1,b2) at (x,y)
            g.setColor(new Color(b0,b1,b2));
            g.fillRect(x,y,1,1));
        }
    }
}

我会留给你测试和集成这段代码。

您可以在wikipedia 上阅读更多关于重心坐标的信息;

【讨论】:

  • 非常感谢
  • 您好,打扰了,您有使用 Bresenham 线条绘制算法的代码
  • @MrJab 不,我不知道,它不适用于问题。
猜你喜欢
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 2015-07-30
相关资源
最近更新 更多