首頁>Program>source
  1. 我想一遍又一遍地查看所有,但我只能看到1 點.我更改為查看所有Points的架子吗?
  2. System.out 您可以看到10次"設置",然後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);         
    }
}
最新回復
  • 5月前
    1 #

    *關於您為什麼只能看最後更新的簡單說明:*

    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.
    

    尝試一下,然後問您不清楚什麼:

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

  • 5月前
    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()不会绘製任何东西。

    EDIT

    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.
    

    問题出在哪裏?

    EDIT- SWING TIMER

    您的代碼還可以,但是函式處理比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();
        }
    }
    

  • 使用Bash您最類似的命令列技巧是什麼?
  • JSON還是XML? 哪个更好?