-5
  1. 我想看到Points一个又一个,但我只能看到1点。我要改变什么才能看到所有的Points
  2. System.out您可以看到 10 次“set”,然后 2 次“paintComponent”。每次设置后我应该改变什么?它改变了“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);         
    }
}
4

2 回答 2

6

*关于为什么您只能看到最新更新的简单说明:*

摘自 Chet Haase 和 Romain Guy 的《肮脏的富客户》

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

这是代码,它可以让您在 for 循环中迭代时查看您的观点,尽管这种方法非常不鼓励,因为它有许多与之相关的缺点。虽然试试这个而不是调用repaint()call paintImmediately(int ...)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(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);         
    }
}
于 2012-04-27T14:52:32.010 回答
2

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

试试这个。我希望这只是一个结构,您的paintComponent() 没有绘制任何东西。

编辑

public void set_list(LinkedList <point> p){
Points =p;     
System.out.println("set_ist");// 1:First this line will be displayed then..
repaint();//2: Then this is called, which in turn calls your `paintComponent()`
}

现在,当你paintComponent()被调用时,它有

system.out.println("paintComponent");
//3: so now this will be displayed.

这里的问题在哪里?

编辑-摆动定时器

你的代码没问题,但是函数处理比 GUI 更新快得多,这就是为什么你看不到你面前的变化。您在函数调用之间调用 thread.sleep() 以减慢调用速度的方式不是一个好方法。对于摆动中的任何计时事物,请使用摆动计时器,我更改了摆动计时器的代码。

使用摇摆定时器:

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();
    }
}
于 2012-04-26T17:24:44.433 回答