如果其被递归函数调用,paintComponent 不起作用? [英] paintComponent does not work if its called by the recursive function?

查看:30
本文介绍了如果其被递归函数调用,paintComponent 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我想一个接一个地查看所有 Points 但我只能看到 1 个观点.我要更改什么才能看到所有 Points?
  2. System.out中你可以看到10次set",然后是2次油漆组件".每次设置后我应该改变什么调用它更改paintComponente"?
  1. I want to see all the Points one after another but I see only able to see 1 point. What shold I change to see all the Points ?
  2. In the System.out you can see 10 times "set" and then 2 times "paintComponent". what should I change that after each time set is called it change the "paintComponente" ?

====================================================================================

==================================================================================

public class exampe extends JPanel  
{
    int x; 
    int y;

    public void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;
        g2.fillOval(x-2,y-2,4,4);
        System.out.println("paintComponent");        
    }

    public void set(int X, int Y)
    {
        x = X;
        y = Y;
        System.out.println("set");
        super.repaint();
    }

    public static void main(String args[]) 
    {   
        int e=1;

        JFrame frame = new JFrame("TEST");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        exampe ex= new exampe();
        JScrollPane scroll = new JScrollPane(ex);
        frame.getContentPane().add(scroll);

        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        for(int i=0; i< 10; ++i)
            ex.set(e+i,e+i);         
    }
}

推荐答案

*关于为什么你只能看到最后一次更新的简单解释:*

引自 Chet Haase 和 Romain Guy 的 Filthy Rich Clients

It is important to note that repaint requests get "coalesced," or combined. 
So, for example, if you request a repaint and there is already one on the 
queue that has not yet been serviced, then the second request is ignored 
because your request for a repaint will already be fulfilled by the earlier 
request. This behavior is particularly helpful in situations where many
repaint requests are being generated, perhaps by very different situations 
and components, and Swing should avoid processing redundant requests and 
wasting effort.

试试这个,问你不清楚的地方:

Try your hands on this, and ask what is not clear to you :

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class PointsExample
{   
    private CustomPanel contentPane;
    private Timer timer;
    private int x = 1;
    private int y = 1;
    private ActionListener timerAction = new ActionListener()
    {   
        public void actionPerformed(ActionEvent ae)
        {
            contentPane.set(x, y);
            x++;
            y++;
            if (x == 450)
                timer.stop();
        }
    };
    /*
     * This is just JFrame, that we be 
     * using as the Base for our Application.
     * Though here we are calling our
     * JPanel (CustomPanel), whose
     * paintComponent(...) method, we had
     * override.
     */
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Locate Mouse Position");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = new CustomPanel();
        frame.setContentPane(contentPane);  
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        timer = new Timer(100, timerAction);
        timer.start();
    }

    public static void main(Stringu005Bu005D args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PointsExample().createAndDisplayGUI();
            }
        });
    }
}

class CustomPanel extends JComponent
{
    private int x;
    private int y;

    public void set(int a, int b)
    {
        x = a;
        y = b;
        repaint();
    }   

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }

    @Override
    public void paintComponent(Graphics g)
    { 
        g.clearRect(0, 0, getWidth(), getHeight());
        Graphics2D g2 =(Graphics2D) g;
        g2.fillOval(x, y, 4, 4);        
    }
}

这是代码,它允许您在 for 循环中进行迭代时查看您的观点,尽管非常不鼓励这种方法,因为它有许多与之相关的缺点.虽然试试这个而不是调用 repaint() 调用 paintImmediately(int ...)paintImmediately(矩形矩形)

Here is the code, that will allow you to have a look at your points while iterating inside a for loop, though this approach is highly discouraged, for many cons associated with it. Though try your hands on this instead of calling repaint() call paintImmediately(int ...) or paintImmediately(Rectangle rect)

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class PointsExample
{   
    private CustomPanel contentPane;
    private Timer timer;
    private int x = 1;
    private int y = 1;

    /*
     * This is just JFrame, that we be 
     * using as the Base for our Application.
     * Though here we are calling our
     * JPanel (CustomPanel), whose
     * paintComponent(...) method, we had
     * override.
     */
    private void createAndDisplayGUI()
    {
        JFrame frame = new JFrame("Locate Mouse Position");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        contentPane = new CustomPanel();
        frame.setContentPane(contentPane);  
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);

        for (int i = 0; i < 500; i++)
        {
            contentPane.set(x, y);
            x++;
            y++;
            if (x == 450)
                break;
        }
    }

    public static void main(Stringu005Bu005D args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new PointsExample().createAndDisplayGUI();
            }
        });
    }
}

class CustomPanel extends JComponent
{
    private int x;
    private int y;

    public void set(int a, int b)
    {
        x = a;
        y = b;
        paintImmediately(0, 0, getWidth(), getHeight());

    }   

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(500, 500));
    }

    @Override
    public void paintComponent(Graphics g)
    { 
        super.paintComponent(g);
        g.fillOval(x, y, 4, 4);         
    }
}

这篇关于如果其被递归函数调用,paintComponent 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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