如果由recursion函数调用paintComponent不起作用?
- 我想要看到所有的积分 ,但是我只能看到1分。 我改变了什么看到所有的点 ?
- 在
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); } }
* 简单说明为什么你只能看到最新的更新:*
来自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循环中迭代时看看你的观点,尽pipe这种方法是非常不鼓励的,因为许多与之相关的缺点。 尽pipe试试你的手而不是调用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); } }
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.
这里的问题在哪里?
编辑 – 摇摆定时器
你的代码是好的,但function处理比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(); } }