Re: Graphics help please



Rexx Magnus wrote:
I've got some code below, in its early stages - my ultimate goal is to be able to render pixel graphics to the screen, but I don't want to render an entire image and then display it. I'd prefer to have it shown as it draws, so that I can see the activity (for the time being).
At a later stage I may want to render a chunk to the buffer and then display it in one go (doublebuffering maybe).

How do I get it to render after the initial display of the applet? I tried adding a loop where I draw the circle in the for loop, but it only displays after it's finished. I assume it's because the loop is running after the initial paint, but before the display is finally rendered to the screen. What I need to know is how to run it outside of this loop, and being a total java newbie, I can't see how to do it.

Suggestions as to how to rearrange this code would be greatly appreciated. It took me long enough to find out how to render single pixels directly without tons of code (lines with the same start and end coordinates are not suitable for my ultimate goal).



import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.lang.Math.*;

public class Pixelgraphics extends JFrame {
public static void main(String [] args)
{
new Pixelgraphics();
}
public Pixelgraphics()
{
this.setSize(300,300);
this.setTitle("Pixel Graphics");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent
{
public void paint(Graphics g)
{
//drawing code goes here
Graphics2D g2 = (Graphics2D)g;
BufferedImage buf = g2.getDeviceConfiguration
().createCompatibleImage(300, 300, Transparency.OPAQUE);
for(int i = 0; i <360; i++)
{
int x = 150 + (int)(20 * Math.sin(i));
int y = 150 + (int)(20 * Math.cos(i));
buf.setRGB(x, y, new Color(255,255, 255).getRGB()); g2.drawImage(buf,0,0,null);

// I want to be able to pause here to see the screen as it draws
}
}
}
}

You've got a bunch of problems. Drawing Swing components must occur in the paintComponent() method not paint(). You can't pause in the paintComponent() method and get it to work. So what you need to do is keep the state somewhere else and draw it in the paintComponent(). You can use an offline image as you did but it is not necessary. I use the volatile variable 'angle' to store the current drawing state. Using a separate thread to set the angle makes it really easy to have many different threads doing different things. In the example below I start the thread running right after setting the frame visible but it could easily be started with a button.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class test2 extends JPanel implements Runnable {
private volatile int angle;

public test2() {
setPreferredSize(new Dimension(300,300));
}

public void run() {
while (--angle >= -360) {
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}

public void paintComponent(Graphics g2D) {
Graphics2D g = (Graphics2D)g2D;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.drawArc(0,0,getWidth()-1,getHeight()-1,90,angle);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test2 t2 = new test2();
f.add(t2);
f.pack();
f.setVisible(true);
new Thread(t2).start();
}
});
}
}

--

Knute Johnson
email s/nospam/knute/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.