如何给像素着色?

发布于 2024-09-11 09:54:31 字数 220 浏览 5 评论 0原文

我必须创建一个简单的 2D 动画,而不使用各种图元来绘制线条、圆形等。它必须通过操纵像素并通过对像素着色来实现绘制线条、圆形等的算法之一来完成。

我想过使用 Turbo C 来达到目的,但我使用 ubuntu。所以我尝试使用dosbox安装并运行turbo C但无济于事。

现在我唯一的选择是Java。 Java 中可以操作像素吗?我找不到任何好的教程。如果可以给出相同的示例代码,那就太好了。

I have to create a simple 2D animation without using various primitives for drawing line, circle etc for the purpose. It has to be done by manipulating pixels and implementing one of the algorithms for drawing line, circle etc by coloring pixels.

I thought of using Turbo C for the purpose, but I use ubuntu. So I tried using dosbox to install and run turbo C but to no avail.

Now my only option is Java. Is it possible to manipulate pixels in Java? I couldn't find myself any good tutorials for the same. It would be great if a sample code for the same can be given.

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

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

发布评论

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

评论(4

挽梦忆笙歌 2024-09-18 09:54:31

java.awt.BufferedImage< /code>有一个方法 setRGB(int x, int y, int rgb),它设置单个像素的颜色。此外,您可能需要查看 java.lang. awt.Color,尤其是它的 getRGB() 方法,它可以将 Color 转换为整数,然后将其放入 int rgb 参数中代码>设置RGB。

The class java.awt.BufferedImage has a method setRGB(int x, int y, int rgb) which sets the color of an individual pixel. Additionally, you might want to look at java.awt.Color, especially its getRGB() method, which can convert Colors into integers that you can put into the int rgb parameter of setRGB.

一片旧的回忆 2024-09-18 09:54:31
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DirectDrawDemo extends JPanel {

    private BufferedImage canvas;

    public DirectDrawDemo(int width, int height) {
        canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        fillCanvas(Color.BLUE);
        drawRect(Color.RED, 0, 0, width/2, height/2);
    }

    public Dimension getPreferredSize() {
        return new Dimension(canvas.getWidth(), canvas.getHeight());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(canvas, null, null);
    }


    public void fillCanvas(Color c) {
        int color = c.getRGB();
        for (int x = 0; x < canvas.getWidth(); x++) {
            for (int y = 0; y < canvas.getHeight(); y++) {
                canvas.setRGB(x, y, color);
            }
        }
        repaint();
    }


    public void drawLine(Color c, int x1, int y1, int x2, int y2) {
        // Implement line drawing
        repaint();
    }

    public void drawRect(Color c, int x1, int y1, int width, int height) {
        int color = c.getRGB();
        // Implement rectangle drawing
        for (int x = x1; x < x1 + width; x++) {
            for (int y = y1; y < y1 + height; y++) {
                canvas.setRGB(x, y, color);
            }
        }
        repaint();
    }

    public void drawOval(Color c, int x1, int y1, int width, int height) {
        // Implement oval drawing
        repaint();
    }



    public static void main(String[] args) {
        int width = 640;
        int height = 480;
        JFrame frame = new JFrame("Direct draw demo");

        DirectDrawDemo panel = new DirectDrawDemo(width, height);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}

替代文本http://grab.by/grabs/39416148962d1da3de12bc0d95745341.png

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DirectDrawDemo extends JPanel {

    private BufferedImage canvas;

    public DirectDrawDemo(int width, int height) {
        canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        fillCanvas(Color.BLUE);
        drawRect(Color.RED, 0, 0, width/2, height/2);
    }

    public Dimension getPreferredSize() {
        return new Dimension(canvas.getWidth(), canvas.getHeight());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(canvas, null, null);
    }


    public void fillCanvas(Color c) {
        int color = c.getRGB();
        for (int x = 0; x < canvas.getWidth(); x++) {
            for (int y = 0; y < canvas.getHeight(); y++) {
                canvas.setRGB(x, y, color);
            }
        }
        repaint();
    }


    public void drawLine(Color c, int x1, int y1, int x2, int y2) {
        // Implement line drawing
        repaint();
    }

    public void drawRect(Color c, int x1, int y1, int width, int height) {
        int color = c.getRGB();
        // Implement rectangle drawing
        for (int x = x1; x < x1 + width; x++) {
            for (int y = y1; y < y1 + height; y++) {
                canvas.setRGB(x, y, color);
            }
        }
        repaint();
    }

    public void drawOval(Color c, int x1, int y1, int width, int height) {
        // Implement oval drawing
        repaint();
    }



    public static void main(String[] args) {
        int width = 640;
        int height = 480;
        JFrame frame = new JFrame("Direct draw demo");

        DirectDrawDemo panel = new DirectDrawDemo(width, height);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}

alt text http://grab.by/grabs/39416148962d1da3de12bc0d95745341.png

謸气贵蔟 2024-09-18 09:54:31

今天我得到的另一点乐趣是,我使用 #Jave Cavas、Color、Graphics 和 #Swing JFrame 创建一个简单的着色像素类,我们所做的就是创建一个正方形的 JFrame 400×400 像素(框架需要很少的额外像素) self),然后我们扩展画布并对像素进行对称着色。

package gcclinux.co.uk;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class ColouringPixels extends Canvas {

    private static final long serialVersionUID = 1L;
    private static final int WIDTH = 407; // Additional pixels needed for the frame
    private static final int HEIGHT = 427; // Additional pixels needed for the frame


    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int r = 0; r <= 2; r++) {

        for(int y = 0; y < HEIGHT; y++) { 
            for(int x = 0; x < WIDTH; x++) { 
                if (x >= 1 && x <= 100 && y >= 1 && y <=100){
                    g.setColor(Color.WHITE);
                } else if (x >= 101 && x <= 200 && y >= 101 && y <=200){
                    g.setColor(Color.RED);
                } else if (x >= 201 && x <= 300 && y >= 201 && y <=300){
                    g.setColor(Color.WHITE);
                } else if (x >= 301 && x <= 399 && y >= 301 && y <=400){
                    g.setColor(Color.RED);
                } else              
                {
                    g.setColor(Color.BLUE);
                }
                g.drawLine(x, y, x, y);
            }
        }
            for(int x = 0; x < HEIGHT; x++) {
                for(int y = 0; y < WIDTH; y++) {
                    if (x >= 1 && x <= 100 && y >= 1 && y <=100){
                        g.setColor(Color.RED);
                    } else if (x >= 101 && x <= 200 && y >= 101 && y <=200){
                        g.setColor(Color.WHITE);
                    } else if (x >= 201 && x <= 300 && y >= 201 && y <=300){
                        g.setColor(Color.RED);
                    } else if (x >= 301 && x <= 399 && y >= 301 && y <=400){
                        g.setColor(Color.WHITE);
                    } else              
                    {
                        g.setColor(Color.BLUE);
                    }
                    g.drawLine(x, y, x, y);
                }
            }
        }
        try {
            Thread.sleep(2000);             // Sleep for 2 seconds
            System.exit(0);             // Closed the program
        }catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("ColouringPixels - Lesson 9");
        frame.setSize(WIDTH, HEIGHT);
        frame.setResizable(false);
        frame.add(new ColouringPixels());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Another bit of fun I had today where I used #Jave Cavas, Color, Graphics and #Swing JFrame to create a simply colouring pixels class all we are doing is creating square a JFrame 400×400 pixels (few extra pixels required for the frame it self) and then we extend the Canvas and colour the pixels symmetrically.

package gcclinux.co.uk;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class ColouringPixels extends Canvas {

    private static final long serialVersionUID = 1L;
    private static final int WIDTH = 407; // Additional pixels needed for the frame
    private static final int HEIGHT = 427; // Additional pixels needed for the frame


    @Override
    public void paint(Graphics g) {
        super.paint(g);

        for (int r = 0; r <= 2; r++) {

        for(int y = 0; y < HEIGHT; y++) { 
            for(int x = 0; x < WIDTH; x++) { 
                if (x >= 1 && x <= 100 && y >= 1 && y <=100){
                    g.setColor(Color.WHITE);
                } else if (x >= 101 && x <= 200 && y >= 101 && y <=200){
                    g.setColor(Color.RED);
                } else if (x >= 201 && x <= 300 && y >= 201 && y <=300){
                    g.setColor(Color.WHITE);
                } else if (x >= 301 && x <= 399 && y >= 301 && y <=400){
                    g.setColor(Color.RED);
                } else              
                {
                    g.setColor(Color.BLUE);
                }
                g.drawLine(x, y, x, y);
            }
        }
            for(int x = 0; x < HEIGHT; x++) {
                for(int y = 0; y < WIDTH; y++) {
                    if (x >= 1 && x <= 100 && y >= 1 && y <=100){
                        g.setColor(Color.RED);
                    } else if (x >= 101 && x <= 200 && y >= 101 && y <=200){
                        g.setColor(Color.WHITE);
                    } else if (x >= 201 && x <= 300 && y >= 201 && y <=300){
                        g.setColor(Color.RED);
                    } else if (x >= 301 && x <= 399 && y >= 301 && y <=400){
                        g.setColor(Color.WHITE);
                    } else              
                    {
                        g.setColor(Color.BLUE);
                    }
                    g.drawLine(x, y, x, y);
                }
            }
        }
        try {
            Thread.sleep(2000);             // Sleep for 2 seconds
            System.exit(0);             // Closed the program
        }catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("ColouringPixels - Lesson 9");
        frame.setSize(WIDTH, HEIGHT);
        frame.setResizable(false);
        frame.add(new ColouringPixels());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
野稚 2024-09-18 09:54:31

您可以使用 java 的内置 2D Graphics 包来完成此操作。

You can accomplish this using java's builtin 2D Graphics package.

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