2010-07-24 114 views
13

我必須創建一個簡單的二維動畫,而不使用繪製線,圓等各種原始目的。它必須通過操縱像素,並通過着色像素來實現繪製線,圓等的算法之一。如何爲像素着色?

我以爲使用Turbo C的目的,但我使用Ubuntu的。所以我嘗試使用dosbox安裝並運行turbo C,但無濟於事。

現在我唯一的選擇是Java。有沒有可能在Java中操縱像素?我找不到任何相同的好教程。如果可以給出相同的示例代碼,那將是非常好的。

+1

如果你運行ubuntu並且想要一個編譯器,'apt-get install build-essential'會爲你帶來GCC,這是標準的C和C++編譯器。 – 2010-07-25 02:46:02

+5

請注意,如果您在執行後(例如編寫遊戲),您不想使用* setRgb *單獨操縱每個像素,這太慢了。您通常希望直接操作基礎原始數據。例如,您可能想要查看BufferedImage的* getRaster()。getDataBuffer())。getData()*,它返回像素的int []表示形式。 – NoozNooz42 2010-07-25 10:45:07

回答

28

該類java.awt.BufferedImage有一個方法setRGB(int x, int y, int rgb)它設置一個單獨的像素的顏色。此外,您可能需要查看java.awt.Color,尤其是其getRGB()方法,該方法可以將顏色轉換爲整數,您可以將其放入參數setRGBint rgb

+1

斷開的鏈接。請參閱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

23
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

1

的樂趣另一個位我今天在這裏我用#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); 
    } 
}