Java 如果递归函数调用了paintComponent,则paintComponent不工作? 我想一个接一个地看到所有的点,但我只能看到1点 指向我应该改变什么来查看所有的点 在System.out中,您可以看到10次“设置”,然后是2次 “油漆组件”。每次设置时间后,我应该更改什么 叫它改变“颜料成分”

Java 如果递归函数调用了paintComponent,则paintComponent不工作? 我想一个接一个地看到所有的点,但我只能看到1点 指向我应该改变什么来查看所有的点 在System.out中,您可以看到10次“设置”,然后是2次 “油漆组件”。每次设置时间后,我应该更改什么 叫它改变“颜料成分”,java,swing,paintcomponent,Java,Swing,Paintcomponent,================================================================================== public class exampe extends JPanel { int x; int y; public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g

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

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);         
    }
}
公共类exampe扩展了JPanel
{
int x;
int-y;
公共组件(图形g)
{
超级组件(g);
图形2d g2=(图形2d)g;
g2.椭圆形(x-2,y-2,4,4);
System.out.println(“油漆组件”);
}
公共无效集(整数X,整数Y)
{
x=x;
y=y;
系统输出打印项次(“设置”);
super.repaint();
}
公共静态void main(字符串参数[])
{   
int e=1;
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
exampe ex=新的exampe();
JScrollPane scroll=新的JScrollPane(ex);
frame.getContentPane().add(滚动);
框架。设置尺寸(400300);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
对于(int i=0;i<10;++i)
ex.set(e+i,e+i);
}
}

1:paintComponent()的第一行应该是您的
super.paintComponent()

2:为什么要调用super.repaint(),使其简单化
repaint()

你的卓尔应该是这样的

public class drow extends JPanel {
 ...........
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 =(Graphics2D) g;

}
public void set_list(LinkedList <point> p){
Points =p;     
repaint();
}
问题出在哪里

编辑-摆动计时器

您的代码还可以,但是函数处理要比GUI更新快得多,这就是为什么您无法看到前面的更改。在函数调用之间调用thread.sleep()以降低其调用速度的方法不是一种好方法。对于swing中的任何计时功能,请使用swing timer,我更改了swing timer的代码

使用摆动计时器:

public class exampe extends JPanel implements ActionListener {

    int x;
    int y;
    int temp = 0;

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.fillOval(x - 2, y - 2, 4, 4);
    }

    public void set(int X, int Y) {

        x = X;
        y = Y;
    }

    public static void main(String args[]) {

        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);
        Timer PointTimer = new Timer(1000, ex);
        PointTimer.setInitialDelay(1000);
        PointTimer.start();
        System.out.println("started");
    }

    @Override
    public void actionPerformed(ActionEvent e) {

       // set(rand.nextInt(350), rand.nextInt(350));
          set(temp+10,temp+10);
          temp=temp+2;
          repaint();
    }
}
*关于为什么只能看到上次更新的简单解释:*

切特·哈斯(Chet Haase)和罗曼·盖伊(Romain Guy)从肮脏的有钱客户那里引用的一句话

试着把手放在这上面,问问你不清楚的地方:

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(String\u005B\u005D 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()
call或

import java.awt.*;
导入java.awt.event.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
公共类点示例
{   
私人定制面板内容窗格;
私人定时器;
私有整数x=1;
私有整数y=1;
/*
*这只是我们的JFrame
*用作我们应用程序的基础。
*虽然在这里我们称之为
*JPanel(CustomPanel),谁的
*paintComponent(…)方法,我们有
*覆盖。
*/
私有void createAndDisplayGUI()
{
JFrame frame=新的JFrame(“定位鼠标位置”);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane=新的CustomPanel();
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
对于(int i=0;i<500;i++)
{
contentPane.set(x,y);
x++;
y++;
如果(x==450)
打破
}
}
公共静态void main(字符串\u005B\u005D参数)
{
SwingUtilities.invokeLater(新的Runnable()
{
公开募捐
{
新建PointsSample().createAndDisplayGUI();
}
});
}
}
类CustomPanel扩展了JComponent
{
私人INTX;
私营企业;
公共无效集(int a,int b)
{
x=a;
y=b;
立即绘制(0,0,getWidth(),getHeight());
}   
@凌驾
公共维度getPreferredSize()
{
返回(新维度(500500));
}
@凌驾
公共组件(图形g)
{ 
超级组件(g);
g、 椭圆形(x,y,4,4);
}
}

Use,don't Use
Thread.sleep(…)
,这将冻结您的GUI并一定会给您带来这样的结果:(为了更快地获得更好的帮助,发布一条消息。他已经在一条消息中被告知了这一切。我在回答中描述了为什么调用
repaint()
在迭代
列表时,您无法在屏幕上获得您的观点,因为
重新绘制()请求本质上是异步的,因此如果已经有一个
重新绘制()请求
在队列中,因此任何新请求都将被Swing丢弃,因此您只能在屏幕上看到最后一次更新,为了实现您想要的内容,您必须使用
paintimmediate()
而不是
repaint()
。信息很好,但要更新,我必须等待6个小时,因为我的每日限制已经超过:(它不会改变任何东西。我在set_列表中打印,在paintComponent中我在set_列表中看到打印,但在paintComponent中看不到打印,我该怎么做?然后我们需要更多的代码,正如andrew所说,发布一个。在这段代码中我看到set_列表,但在没有paintCoponement之后。你能告诉我为什么吗?
公共类drow扩展了JPanel{……….@覆盖公共void paintComponent(Graphics g){super.paintComponent(g);Graphics2D g2=(Graphics2D)g;System.out.println(“paintCoponement”);}公共void set_list(LinkedList p){System.out.println(“set_list”);Points=p;repaint()}
请提供一个简短但完整的示例来说明问题。不,我有最后一个问题,我可以将新值放入已执行的操作中,但如果想要一个for(int i=0;i等待,请给我15分钟:-)@user1359266您仍然停留在

public class exampe extends JPanel implements ActionListener {

    int x;
    int y;
    int temp = 0;

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.fillOval(x - 2, y - 2, 4, 4);
    }

    public void set(int X, int Y) {

        x = X;
        y = Y;
    }

    public static void main(String args[]) {

        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);
        Timer PointTimer = new Timer(1000, ex);
        PointTimer.setInitialDelay(1000);
        PointTimer.start();
        System.out.println("started");
    }

    @Override
    public void actionPerformed(ActionEvent e) {

       // set(rand.nextInt(350), rand.nextInt(350));
          set(temp+10,temp+10);
          temp=temp+2;
          repaint();
    }
}
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.
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(String\u005B\u005D 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);        
    }
}
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(String\u005B\u005D 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);         
    }
}