Java更新小圈子 [英] Java Updating Small Circles

查看:85
本文介绍了Java更新小圈子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表单上显示大量(500多个)小圆圈以模拟LED.但是,这些圆圈必须很小,直径大约为8或9像素.

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.

到目前为止,在我的测试中,我整理了一些代码,创建了一个使用Shape(Ellipse2D.Double)的Led类,并通过JFrame的paint方法将其直接显示在JFrame上.

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)首先,除非有其他方法,否则Java在绘制小圆圈方面似乎有麻烦.它们似乎在右下角以默认宽度(或1像素)的笔宽度折断",从而切断了该部分,留下了变形的圆.有什么办法可以画出(很多)小圆圈并使它们看起来正确吗?

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)我的子类JFrame重写了paint方法以绘制这些"led",尽管也调用了super.paint以确保绘制JFrame.但是,我看到它很少在初次出现时绘制led,或者当表单在屏幕外移回时,或者在应用程序放在前面并再次移开时,绘制方法很少.当我最小化/最大化表单时被称为.不应在每次需要绘画时都调用绘画吗?

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?

推荐答案

您不应覆盖paint().请改用paintComponent().另外,JFrames有点奇怪,我会使用JPanel作为我的BaseClass.

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

关于您的观察:这可能是由抗锯齿引起的吗?您是否尝试通过setRenderingHints()关闭抗锯齿功能?

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

在下面的评论之后,我编写了一个小型测试程序.圆圈看起来不错:

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);
    }
}

这篇关于Java更新小圈子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆