没看懂repaint()的实现,找源码也没找到paint,都没调用重写的paint,那是怎么画出黑点的呢?
******************************************
打开repaint,上面有提示see update,点开update发现paint,这里的原理是一种回调,传统的paint每次画都是重新绘制一个屏幕,随着东西越来越多,绘制的东西也就越来越多,而repaint则是在指定的dirty区域重新绘制,提高了效率。
App.java:
package com.zmz.ui; public class App { public static void main(String[] args) { GameFrame frame = new GameFrame(); GamePanel panel = new GamePanel(); frame.add(panel); frame.setVisible(true); } }
GameFrame.java:
package com.zmz.ui; import javax.swing.JFrame; /** * 游戏窗口 * * @author Administrator * */ public class GameFrame extends JFrame { // 类长什么样子由构造方法决定 /** * 构造方法初始化 */ public GameFrame() { setTitle("五子棋"); setSize(506, 528); setResizable(false);// 不可调大小 setLocationRelativeTo(null);// 放中间 setDefaultCloseOperation(EXIT_ON_CLOSE);// 关闭窗口 } }
GamePanel.java:
package com.zmz.ui; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JPanel; import javax.swing.Timer; public class GamePanel extends JPanel{ int x = 0; Point[] ps = new Point[50]; int psSize = 0; /** * 构造方法 */ public GamePanel() { //注册鼠标监听器 // addMouseListener(new Listener()); //适配器模式 addMouseListener(new MListener()); // addMouseMotionListener(l); // addMouseWheelListener(l); //自动重绘制 //定时器,每隔0.02s调用一次Timerlistener里面的方法, //1000是1s // Timer timer = new Timer(20, new Timerlistener()); // timer.start(); } int w; int h; @Override public void paint(Graphics g) { // TODO Auto-generated method stub super.paint(g); w = getWidth(); h = getHeight(); for(int i = 0; i < psSize; i++) { Point point = ps[i]; //画图 g.fillOval(point.x, point.y, 10, 10); } // g.drawLine(100, 100, 100, 100); // g.drawOval(0, 0, 300, 200);//内切圆 // g.setColor(new Color(0, 255, 0, 100)); // g.drawRect(w/2-150, h/2-150,300,300); //// g.setColor(Color.BLUE); // g.setColor(new Color(255, 0, 0,100));//最后一个为透明度 // g.drawOval(w/2-150, h/2-150,300,300);//左上角点和长宽 // // g.fillArc(w/2-150, h/2-150, 300, 300, x, 60); // g.fillArc(w/2-150, h/2-150, 300, 300, x+120, 60); // g.fillArc(w/2-150, h/2-150, 300, 300, x+240, 60);//逆时针 } // class Listener implements MouseListener { // @Override // public void mouseClicked(MouseEvent e) { // System.out.println("鼠标点击"); // x += 15; // if(x > 360) { // x = 0; // } // // 重绘制 // repaint(); // } // // @Override // public void mousePressed(MouseEvent e) { // System.out.println("按下"); // } // // @Override // public void mouseReleased(MouseEvent e) { // System.out.println("释放"); // } // // @Override // public void mouseEntered(MouseEvent e) { // System.out.println("进入"); // } // // @Override // public void mouseExited(MouseEvent e) { // System.out.println("离开"); // } // // } class Timerlistener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub x += 15; if(x > 360) { x = 0; } repaint(); } } class MListener extends MouseAdapter { @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub // super.mouseClicked(e); //这个对象e可以获得光标的信息,坐标,压力等等 System.out.println(e.getX()+", "+e.getY()); Point point = new Point(e.getX(), e.getY()); ps[psSize++] = point; int ox = e.getX(); int oy = e.getY(); x += 15; if(x > 360) { x = 0; } repaint(); } } }