tags:

views:

244

answers:

3

I need to display a large number (500+) of small circles on a form to simulate LEDs. However, these circles need to be quite small, around 8 or 9 pixels diameter.

So far, in my testing, I've put together some code that creates an Led class that uses a Shape (Ellipse2D.Double) and displays it directly on the JFrame from the JFrame's paint method.

This has led me to two observations/issues:

1) Firstly, unless there is an alternate method, Java appears to have trouble in drawing small circles. They appear to 'break' in the lower right corner with a pen width of default (or 1 pixel), which cuts this part off leaving a deformed circle. If there any way I can draw (lots of) small circles and have them look right?

2) My subclassed JFrame overrides the paint method to draw these 'leds', although calls the super.paint as well to ensure the JFrame gets drawn. However, I'm seeing that it rarely draws the led on the first appearance, or when the form is moved off-screen and back, or when an application it put in front and moved away again, and the only time the paint method is called is when I minimize/maximize the form. Shouldn't paint be called every time the form needs painting?

+3  A: 

You shouldn't override paint(). Use paintComponent() instead. Also, JFrames are slightly strange things, I'd use JPanel as my BaseClass.

About your observation: Might this be caused by antialiasing? Did you try to turn antialiasing off via setRenderingHints()?

EDIT: After the comment below, I've written a small test program. Circles look nice with this:

import javax.swing.*;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.RenderingHints;

class Test extends JFrame {

    public Test() {
        setContentPane(new JPanel() {
                public void paintComponent(Graphics g){
                    super.paintComponent(g);
                    Graphics2D g2d = (Graphics2D) g;
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    for (int i = 0; i < 500; i++){
                        int x = (int) (Math.random() * getWidth());
                    int y = (int) (Math.random() * getHeight());
                    g.fillOval(x,y,8,8);
                    }
                }
        });
    }

    public static void main(String[] args){
        Test t = new Test();
        t.setSize(new Dimension(640, 480));
        t.setVisible(true);
    }
}
zedoo
Psychic
Hmm, I think the default is to have antialiasing on. This shows how to turn it on (to turn it off use RenderingHints.VALUE_ANTIALIAS_OFF): http://www.java2s.com/Code/JavaAPI/java.awt/Graphics2DsetRenderingHintsMaphints.htm
zedoo
I thought the default was *off*. It needs to be *on*, especially for small circles: `g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)`.
trashgod
trashgod, I've tested this on my box and you're right. Here it's off by default. Also, when enabling antialiasing 8px circles look neat here.
zedoo
Thank you, this is exactly what I was looking to achieve. Circles that are actually round :) I guess the anti-aliasing is far more critical in smaller draws to ensure that they look right. Although, I just noticed that the fix appears to come from using fillOval instead of Ellipse2D. Even antialised, the Ellipse2D has the breaking in the lower right.
Psychic
+1  A: 

These LEDs should be Components like everything else on the form. I think you should use Icons, maybe ImageIcons, to represent your LEDs. That way, you can essentially have them rendered once and after that the same image will be displayed whenever needed. It's handy that you can use images, because then you can use an image that has exactly the shape you'd like to see.

Carl Smotricz
+1  A: 

As far as the 'break' goes, I would look at the bevel setting of your graphics object.

But, I would recommend reading a .png at program start and then displaying that instead of drawing it on your own.


RE: paint() not being called all the time.

Yep, thats how it works. If you need your component to be redrawn at a certain time, you need to force it. Call repaint() to force a redraw.

If you a going to call repaint() from another thread, (ie. a timer thread), be sure to wrap the call in SwingUtilities.invokeLater():

SwingUtilities.invokeLater( new Runnable()
{
    @Override
    public void run()
    {
        myForm.repaint();
    }
} );

Update: maybe you should post some code... I threw together a small test app and didn't see any problems with small circles.

public class MyPanel extends JPanel
{
    public void paint(Graphics _g)
    {
        Graphics2D g = (Graphics2D) _g;

        g.setStroke( new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND) );
        for(int x = 10, w = 1; w < 20; x += w*2, w++)
        {
            Ellipse2D.Double ed = new Ellipse2D.Double(x, 10, w, w);
            g.fill( ed );
        }

        for(int x = 10, w = 1; w < 20; x += w*2, w++)
        {
            Ellipse2D.Double ed = new Ellipse2D.Double(x, 80, w, w);
            g.draw( ed );
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.add( new MyPanel() );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize( 400, 400 );

        frame.setVisible( true );
    }
}

I tried varying the stroke params to see if I could cause a visual break, but was unsuccessful. What do you see?


Not to beat a dead horse, but when I zoom into the output of that program, my circles are pretty much symmetric, minus a little pixel turd on the left:

circle pics

Is this similar to what you are getting?

Trevor Harrison
Psychic
I was kinda vague and misleading. Look at BasicStroke, and its join setting. (thats where my memory got the bevel from...) You probably want JOIN_MITER or JOIN_ROUND. After creating the BasicStroke object, do a g.setStroke( basicStroke ).
Trevor Harrison
Also, regarding your speed questions... if anything, copying a pre-rendered circle image onto your canvas will be faster than drawing circles each time.
Trevor Harrison
When using the code you posted, I see 7/9/11 circles having a straight diagonal in the lower right corner. This isn't obvious on the filled circles, but clearly visible on the outline ones.
Psychic
No, this isn't what I'm getting. That's what I was expecting. On my side, it appears to be more visibly wrong with odd pixel sizes. I'll post an image when I get back to work tomorrow and show you what I mean.
Psychic
Sorry, I've been snowed in for 2 days and only just got back to work.http://www.sleeping-dragons.co.uk/example.pngHrm, apparently I can't post actual images in my posts yet, so you'll need to click the link :/
Psychic
wow, that is pretty icky. Did you say what platform you are running on?
Trevor Harrison
Yeah, this is Windows XP SP3, using a pretty much default install of Netbeans 6.7
Psychic
pretty similar to what I'm running. I give up. :)
Trevor Harrison