如何为像素着色?

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

我考虑过使用Turbo C,但我使用的是ubuntu。 所以我尝试使用dosbox来安装和运行turbo C,但无济于事。

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

java.awt.BufferedImage有一个方法setRGB(int x, int y, int rgb) ,它设置单个像素的颜色。 另外,您可能希望查看java.awt.Color ,尤其是它的getRGB()方法,它可以将Colors转换为可以放入setRGBint rgb参数的整数。

 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://sofzh.miximages.com/java/www.grab.by

我今天使用#Jave Cavas,Color,Graphics和#Swing JFrame来创建一个简单的着色像素类,我们正在做的另一点乐趣就是创建一个400×400像素的JFrame(框架需要几个额外的像素)然后我们扩展Canvas并对称地着色像素。

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

您可以使用java的内置2D图形包来完成此任务。