drawPixel()?

ive searched through the forums and all i could seem to find reguarding draw pixel is that you have to drawline(x,y,x,y)... now for what im doing it seems to be to slow and i was wondering if what im doing is even possible... basicly i wanna make a drawing utility that has access to the array of the the pixels and sets each one on repaint(); so heres my idea:import java.awt.Color;

import javax.swing.*;

import java.awt.*;

publicclass NewThreeDToolextends JApplet

{

private Color[][] screen;

public NewThreeDTool(int rows,int columns)

{

screen =new Color[rows][columns];

for(int i = 0; i < screen.length; i++)

for(int k = 0; k < screen[i].length;k++)

screen[i][k] = Color.black;

}

publicvoid init()

{

setBackground(Color.black);

setForeground(Color.white);

}

publicvoid paint(Graphics g)

{

for(int row = 0; row < screen.length;row++)

for(int col = 0; col < screen[row].length;col++)

{

g.setColor(screen[row][col]);

g.drawLine(col,row,col,row);

}

}

publicstaticvoid main(String args[])

{

int rows = 700;

int columns = 1000;

JFrame f =new JFrame("New 3D Tool");

JApplet applet =new NewThreeDTool(rows,columns);

f.getContentPane().add("Center", applet);

f.pack();

f.setSize(new Dimension(columns,rows));

f.show();

}

}

basicly i have a parallel array to what is in the applet that i can edit each time right before the call to repaint(), but with this the drawing takes forever and i was wondering if there is possibly any faster way to do this or if i should probably just give up...

[3132 byte] By [sosleepya] at [2007-10-2 21:59:49]
# 1

> ive searched through the forums and all i could seem

> to find reguarding draw pixel is that you have to

> drawline(x,y,x,y)... now for what im doing it seems

> to be to slow and i was wondering if what im doing is

> even possible... basicly i wanna make a drawing

> utility that has access to the array of the the

> pixels and sets each one on repaint(); so heres my

> idea:import java.awt.Color;

> import javax.swing.*;

> import java.awt.*;

> public class NewThreeDTool extends JApplet

> {

> private Color[][] screen;

>

> public NewThreeDTool(int rows,int columns)

> {

> screen = new Color[rows][columns];

> for(int i = 0; i < screen.length;

> i++)

> for(int k = 0; k <

> screen[i].length;k++)

>screen[i][k] = Color.black;

> d init()

> {

> setBackground(Color.black);

> setForeground(Color.white);

> }

> void paint(Graphics g)

> {

> for(int row = 0; row < screen.length;row++)

> for(int col = 0; col < screen[row].length;col++)

>{

>g.setColor(screen[row][col]);

>g.drawLine(col,row,col,row);

>}

> static void main(String args[])

> {

> int rows = 700;

> int columns = 1000;

> JFrame f = new JFrame("New 3D Tool");

> JApplet applet = new NewThreeDTool(rows,columns);

> f.getContentPane().add("Center", applet);

> f.pack();

> f.setSize(new Dimension(columns,rows));

> f.show();

> }

> }

> basicly i have a parallel array to what is in the

> applet that i can edit each time right before the

> call to repaint(), but with this the drawing takes

> forever and i was wondering if there is possibly any

> faster way to do this or if i should probably just

> give up...

700,000 pixels. The faster way is to manipulate a BufferedImage directly and paint it using drawImage. Here's an example that creates a 700x1000 BufferedImage and randomizes every other pixel to one of seven values, repaints, and then pauses for 5 milliseconds before randomizing again. On my 1.7 GHz machine, the refresh rate is about 10.5 repaints per second. How much this approach helps you will depend on how many pixels you would be changing per repaint. For instance, changing 10% of the pixels give a rate of about 100 repaints per second on my machine.

This is by no means the fastest way to do things, but you didn't say what sort of target you were shooting for.

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class RandomPixels {

private RandomPixelPanel rpp;

private long start;

public RandomPixels() {

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

displayRepaintRate();

}

});

rpp = new RandomPixelPanel();

f.add(rpp, BorderLayout.CENTER);

f.pack();

f.setVisible(true);

start = System.currentTimeMillis();

rpp.start();

}

public static void main(String[] args) {

new RandomPixels();

}

private void displayRepaintRate() {

rpp.stop();

long end = System.currentTimeMillis();

int paintCount = rpp.getRepaintCount();

System.out.println("Repainted: " + paintCount + " times in " + (end - start) + "ms");

System.out.println("Repaint Rate: " + (paintCount / ((end - start)/1000.0)) + " repaints per second");

}

private static class RandomPixelPanel extends JPanel {

private Dimension PREF_SIZE = new Dimension(700,1000);

private Random random = new Random();

private BufferedImage img = new BufferedImage(700,1000,BufferedImage.TYPE_INT_RGB);

private Thread runner;

private volatile boolean shouldRun;

private int repaintCount = 0;

private boolean firstPaint = true;

private static final int[] colorInts = {

Color.RED.getRGB(),

Color.ORANGE.getRGB(),

Color.YELLOW.getRGB(),

Color.GREEN.getRGB(),

Color.CYAN.getRGB(),

Color.BLUE.getRGB(),

Color.MAGENTA.getRGB(),

};

public synchronized void start() {

if (runner == null) {

runner = new Thread(new Runnable() {

public void run() {

while (shouldRun) {

randomize();

repaint();

repaintCount++;

try {

Thread.sleep(5);

} catch (InterruptedException ie) {

ie.printStackTrace();

}

}

}

});

shouldRun = true;

runner.start();

}

}

public synchronized void stop() {

shouldRun = false;

}

public int getRepaintCount() {

return repaintCount;

}

public Dimension getPreferredSize() {

return PREF_SIZE;

}

protected void paintComponent(Graphics g) {

g.drawImage(img,0,0,this);

}

private void randomize() {

int w = img.getWidth();

int h = img.getHeight();

int nextPixelColor = 0;

int stepCount = 2;

if (firstPaint) {

stepCount = 1;

firstPaint = false;

}

for (int i = 0; i < w; i += stepCount) {

for (int j = 0; j < h; j += stepCount) {

nextPixelColor = colorInts[random.nextInt(7)];

img.setRGB(i,j,nextPixelColor);

}

}

}

}

}

Niceguy1a at 2007-7-14 1:16:01 > top of Java-index,Security,Cryptography...
# 2

so basicly instead of an applet.... you can use a panel object reconstructed as a buffered image? anyways... what your doing seems to work, but im not so sure exactly how the code works(mainly because i dont have much experiance with threads) so your start and stop methods are a lil confusing to me... im assuming sun probably has documentation on threads but i have no clue where to look.

sosleepya at 2007-7-14 1:16:01 > top of Java-index,Security,Cryptography...
# 3
i got it to work... but the image has streaks in it...like some of my BG color is mixin in with the pixel. is this normal?
sosleepya at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 4

> so basicly instead of an applet.... you can use a

> panel object reconstructed as a buffered image?

You can do the same thing in an Applet. Just override paint(). I used a JPanel in a JFrame because it was the easiest way to create a SSCCE(http://mindprod.com/jgloss/sscce.html)

> anyways... what your doing seems to work, but im not

> so sure exactly how the code works(mainly because i

> dont have much experiance with threads) so your start

> and stop methods are a lil confusing to me... im

> assuming sun probably has documentation on threads

> but i have no clue where to look.

The thread is necessary any time you want to do animation. See:

http://java.sun.com/docs/books/tutorial/essential/threads/index.html

An applet version would look like this:

import java.awt.Color;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.util.Random;

import javax.swing.JApplet;

/** <HTML><BODY><APPLET code="RandomPixelsApplet" width="700" height = "1000"></APPLET></BODY></HTML>*/

public class RandomPixelsApplet extends JApplet {

private Random random = new Random();

private BufferedImage img = new BufferedImage(700,1000,BufferedImage.TYPE_INT_RGB);

private Thread runner;

private volatile boolean shouldRun;

private static final int[] colorInts = {

Color.RED.getRGB(),

Color.ORANGE.getRGB(),

Color.YELLOW.getRGB(),

Color.GREEN.getRGB(),

Color.CYAN.getRGB(),

Color.BLUE.getRGB(),

Color.MAGENTA.getRGB(),

};

public synchronized void start() {

if (runner == null) {

runner = new Thread(new Runnable() {

public void run() {

while (shouldRun) {

randomize();

repaint();

try {

Thread.sleep(5);

} catch (InterruptedException ie) {

ie.printStackTrace();

}

}

}

});

shouldRun = true;

runner.start();

}

}

public synchronized void stop() {

shouldRun = false;

}

public void paint(Graphics g) {

g.drawImage(img,0,0,this);

}

private void randomize() {

int w = img.getWidth();

int h = img.getHeight();

int nextPixelColor = 0;

int stepCount = 2;

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

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

nextPixelColor = colorInts[random.nextInt(7)];

img.setRGB(i,j,nextPixelColor);

}

}

}

}

Niceguy1a at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 5

> i got it to work... but the image has streaks in

> it...like some of my BG color is mixin in with the

> pixel. is this normal?

No. If the streaks are vertical, I suspect you are painting only every other pixel. My example did that. If you want to change every pixel, every time, the two loops in the randomize() code should increment by 1 instead of 2. The point of the code was to show that speed can be greatly improved if you change only the pixels that require changing. Looping through every pixel of a 700,000 pixel image just to change a few pixels is wasteful.

Niceguy1a at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 6
yes i noticed that and i took it out a while ago.... what im talking about is some larger streaks and they are horizontal... and about 20 pixels wide...
sosleepya at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 7

> yes i noticed that and i took it out a while ago....

> what im talking about is some larger streaks and they

> are horizontal... and about 20 pixels wide...

Do they stay in the same place or do they move when the repainting is occurring? Is it a flicker or something more permanent?

Niceguy1a at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 8
eh i figured it out... aparrently fillshape is a lot harder to write when your filling a shape in a 2d array rather than an applet... cant figure out how to draw a shape(givin an array with points in order) that will take the points and fill in the shape between then
sosleepya at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 9

> eh i figured it out... aparrently fillshape is a lot

> harder to write when your filling a shape in a 2d

> array rather than an applet... cant figure out how to

> draw a shape(givin an array with points in order)

> that will take the points and fill in the shape

> between then

Have a look at GeneralPath.

Niceguy1a at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 10
well... if its open source... then thats new to me... the documentation doesnt help much... ive already looked at it
sosleepya at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 11
it seems that what im really looking for is the code for fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
sosleepya at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 12
GeneralPath is a Java class in the standard libraries. http://java.sun.com/j2se/1.5.0/docs/api/java/awt/geom/GeneralPath.html
Niceguy1a at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 13

GeneralPath just constructs a polygon for the fill(Shape) method.... there is a fillPolygon(int[] ypoints, int[] xpoints, int npoints) that fills a polygon with xpoints,ypoints as the verticies.... or at least thats what i think it does.... the only problem with everything im finding is that this method is abstract.. and i cant find the appropriate documentation on it

sosleepya at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...
# 14

> GeneralPath just constructs a polygon for the

> fill(Shape) method....

not quite. I mean, it does do that, but it does a lot more too. It makes use of quadratic and cubic bezier spline curves, so you don't have to join every pair of points with a straight line. You can join the with curves too. This is why it is called general path, instead of general polygon. Also, I believe a general path can contain several closed paths (i.e. several polygons).

> there is a fillPolygon(int[]

> ypoints, int[] xpoints, int npoints) that fills a

> polygon with xpoints,ypoints as the verticies.... or

> at least thats what i think it does....

yeah, that's correct.

> the only

> problem with everything im finding is that this

> method is abstract..

yeah, actually the entire graphics class is abstract. The actual implementation of Graphics that you use is a subclass of Graphics. That's where fillPolygon gets defined.

> and i cant find the appropriate

> documentation on it

the documentation you need is provided in the javadoc comments for the fill polygon method.

Basically:

if you specify seven points, it will draw a line between points 1 and 2, a line between points 2 and 3, and between 3 and 4, etc. It will also automatically draw the line between points 7 and 1. It will not draw any lines between any two non-consecutive points (i.e between 1 and 4, for example).

If you need to understand how it fills the polygon, google for the even-odd fill rule.

If you want, you could also look into java.awt.Polygon. This class has a little bit more documentation in it, and you can draw it using a Graphics2D object, calling Graphics2D.fill(Shape shape)

- Adam

guitar_man_Fa at 2007-7-14 1:16:02 > top of Java-index,Security,Cryptography...