用三种颜色给三角形上色

huangapple go评论92阅读模式
英文:

Coloring a Triangle with 3 colors

问题

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 {

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

    }
}
英文:

Please help me how to color the triangle with the three colors. I have it as a problem for school,
Use Java programming language

Hello this is the code:
I am trying to fill the triangle with the three colors, but is hard to mix colors to get it. Please if you are to solve it(approximate it) as much as you can send it to me, i have it as a homework and 5-6 days deadline

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(&quot;Colors&quot;);
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() + &quot; &quot; + 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 &lt; 256; i++) {
int start = 399; 
g2.setColor(new Color(0,i,255-i));   
for(int j = 0; j &lt; 200; j ++) {
Rectangle rec = new Rectangle(150+j,start - i,1,1);
g2.fill(rec);
}
}
for(int j = 0; j &lt; 100; j++) {
int start = 100;
for(int i = 0; i &lt; 300; i++) {
if(i &lt; 22) {
g2.setColor(new Color(255,0,0));
Rectangle rec = new Rectangle(100 + i,start + j,1,1);
g2.fill(rec);
} else if(i &gt; 21 &amp;&amp; i &lt; 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 &lt; 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

得分: 1

以下是翻译好的代码部分:

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) {
    // 计算三角形的边界框:
    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)));

    // 遍历边界框内的每个像素
    for (int y = minY; y < maxY; ++y) {
        for (int x = minX; x < maxX; ++x) {
            // 像素 (x,y) 的中心
            float Px = x + 0.5, Py = y + 0.5;

            // 点 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;

            // 剔除位于三角形外部的像素
            if (b0 < 0 || b1 < 0 || b2 < 0)
                continue;

            // 在 (x,y) 处绘制颜色为 (b0, b1, b2) 的像素
            g.setColor(new Color(b0, b1, b2));
            g.fillRect(x, y, 1, 1);
        }
    }
}

您可以自行进行测试和集成此代码。

您可以在维基百科上阅读更多关于重心坐标的信息。

英文:

Coloring a triangle like this is equivalent to calculating the barycentric coordinates of each pixel within the triangle. The following untested code calculates the barycentric coordinates for each pixel within the triangle ABC, and then uses it to color that pixel:

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 &lt; maxY; ++y) {
for(int x = minX; x &lt; 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 &lt; 0 || b1 &lt; 0 || b2 &lt; 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));
}
}
}

I'll leave it to you to test and integrate this code.

You can read more about barycentric coordinates on wikipedia;

huangapple
  • 本文由 发表于 2020年4月5日 21:50:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61043652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定