我必须创建一个简单的二维动画,而不使用绘制线,圆等各种原始目的。它必须通过操纵像素,并通过着色像素来实现绘制线,圆等的算法之一。如何为像素着色?
我以为使用Turbo C的目的,但我使用Ubuntu的。所以我尝试使用dosbox安装并运行turbo C,但无济于事。
现在我唯一的选择是Java。有没有可能在Java中操纵像素?我找不到任何相同的好教程。如果可以给出相同的示例代码,那将是非常好的。
我必须创建一个简单的二维动画,而不使用绘制线,圆等各种原始目的。它必须通过操纵像素,并通过着色像素来实现绘制线,圆等的算法之一。如何为像素着色?
我以为使用Turbo C的目的,但我使用Ubuntu的。所以我尝试使用dosbox安装并运行turbo C,但无济于事。
现在我唯一的选择是Java。有没有可能在Java中操纵像素?我找不到任何相同的好教程。如果可以给出相同的示例代码,那将是非常好的。
该类java.awt.BufferedImage
有一个方法setRGB(int x, int y, int rgb)
它设置一个单独的像素的颜色。此外,您可能需要查看java.awt.Color
,尤其是其getRGB()
方法,该方法可以将颜色转换为整数,您可以将其放入参数setRGB
的int rgb
。
断开的链接。请参阅https://docs.oracle.com/javase/8/docs/api/java/awt/image/BufferedImage.html和https://docs.oracle.com/javase/8/docs/api/java/awt /Color.html – 2016-08-09 23:31:41
您可以使用java的内置2D Graphics包完成此操作。
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
的乐趣另一个位我今天在这里我用#Jave卡瓦酒,颜色,图形和#Swing的JFrame创建一个简单的像素着色类所有我们正在做的是创造广场一个JFrame 400有×400像素(它自身需要少量额外的像素),然后我们扩展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);
}
}
如果你运行ubuntu并且想要一个编译器,'apt-get install build-essential'会为你带来GCC,这是标准的C和C++编译器。 – 2010-07-25 02:46:02
请注意,如果您在执行后(例如编写游戏),您不想使用* setRgb *单独操纵每个像素,这太慢了。您通常希望直接操作基础原始数据。例如,您可能想要查看BufferedImage的* getRaster()。getDataBuffer())。getData()*,它返回像素的int []表示形式。 – NoozNooz42 2010-07-25 10:45:07