Pixelweises Zeichnen mit WritableRaster

jehi

Grünschnabel
Hallo,

hat Jemand einen Hinweis, ein Beispiel oder eine Infoquelle zum Thema pixelweises Zeichnen in Java. D.h. es soll ein BufferedImage verwendet werden. In einem Raster (WritableRaster ? ) dann die einzelnen Pixel definiert werden.
Ein Beispiel-Code würde mir ungemein helfen.
Ziel ist also die einzelnen Pixel eines Images zu definieren und anzuzeigen.

Danke schon mal
Jens
 
Noch nicht ganz klar

Lieber Tom,

vielen Dank für den Link. Der Code passt eigentlich ganz gut. Leider verstehe ich den Ablauf des pixelweisen Zeichnens immer noch nicht :
Ich verstehe den Zusammenhang zwischen WritableRaster, DataBuffer, DataBufferInt nicht. Wieso wird zu einem DataBufferInt gecastet ?
Im Beispielcode wird auch ein Offscreen-Buffer implementiert. Hat dies einen Grund (außer Ruckelfreie Darstellung?).

Falls Jemand Lust und Zeit hat, wäre es toll für mich, einen Code zu sehen,
der einfach mal eine Linie in ein BufferedImage zeichnet und dessen Pixel im Programm definiert werden.

Ich danke herzlich.
Jens
 
Servus!

Vielleicht hilft ja das hier: (eine alte spiellerei von mir)
Wenn nicht, schau einfach mal bei http://www.javagaming.org vorbei ...

Code:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
import java.util.Random;

/*
* Crea2.java
*
* Created on 25. Juli 2003, 20:01
*/

/**
*
* @author Administrator
*/
public class Crea2 extends javax.swing.JFrame {

	private int x = 0;
	private int y = 0;

	private int dx;
	private int dy;

	private Image img = null;
	private Graphics2D gra = null;
	private boolean running = true;
	private int w = 200;
	private int h = 200;
	private int pix[] = new int[w * h];
	private int index = 0;
	private Random r = null;
	private Thread t = null;
	int red = 0;
	int blue = 0;

	/** Creates new form Crea2 */
	public Crea2() {
		initComponents();
		prepareImg();

		r = new Random();
		start();
	}

	private void start() {
		t = new Thread(new Test());
		t.start();
	}

	private void prepareImg() {
		for (int y = 0; y < h; y++) {
			int red = (y * 255) / (h - 1);
			for (int x = 0; x < w; x++) {
				int blue = (x * 255) / (w - 1);
				pix[index++] = (255 << 24) | (red << 16) | blue;
			}
		}
		img = createImage(new MemoryImageSource(w, h, pix, 0, w));

		gra = (Graphics2D) jPanel1.getGraphics();
	}

	/** This method is called from within the constructor to
	* initialize the form.
	* WARNING: Do NOT modify this code. The content of this method is
	* always regenerated by the Form Editor.
	*/
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				exitForm(evt);
			}
		});

		jPanel1.setMinimumSize(new java.awt.Dimension(400, 300));
		jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
		jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				jPanel1MousePressed(evt);
			}
		});

		getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

		pack();
	}

	private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {
		// Add your handling code here:
	}

	/** Exit the Application */
	private void exitForm(java.awt.event.WindowEvent evt) {
		System.exit(0);
	}

	/**
	* @param args the command line arguments
	*/
	public static void main(String args[]) {
		new Crea2().show();
	}

	/*
	public void paint(Graphics g){
	super.paint(g);
	
	
	}
	**/

	class Test implements Runnable {

		public void run() {

			while (true) {

				if (!running) {
					while (!running) {
						try {
							Thread.currentThread().sleep(100);
						} catch (InterruptedException ie) {
							ie.printStackTrace();
						}
					}
				} else {

					while (running) {

						index = 0;

						dy += 2;
						dx += 2;

						for (int y = 0; y < h; y++) {

							for (int x = 0; x < w; x++) {

								red =
									(int) (180
										+ 180
											* Math.sin(
												(x + dx)
													/ (37
														+ 15
															* Math.sin(
																(y + dy)
																	/ 74)))
											* Math.sin(
												(y + dy)
													/ (31
														+ 11
															* Math.sin(
																(x + dx)
																	/ 37))));
								blue =
									(int) (180
										+ 180
											* Math.cos(
												(x + dx)
													/ (37
														+ 15
															* Math.cos(
																(y + dy)
																	/ 74)))
											* Math.cos(
												(y + dy)
													/ (31
														+ 11
															* Math.cos(
																(x + dx)
																	/ 37))));

								pix[index++] = (16 << 24) | (red << 16) | blue;

								//System.out.println("x: " + x + " y: "+y +" index: " + index);
							}
						}

						img =
							createImage(new MemoryImageSource(w, h, pix, 0, w));

						try {

							Thread.currentThread().sleep(10);

						} catch (InterruptedException ie) {
							ie.printStackTrace();
						}
						if (img != null)
							gra.drawImage(img, 50, 50, 200, 200, jPanel1);

					}
				}

			}

		}

	}

	// Variables declaration - do not modify
	private javax.swing.JPanel jPanel1;
	// End of variables declaration

}

Gruß Tom
 

Neue Beiträge

Zurück