Paint() และ Repaint()
- paint(): เมธอดนี้เก็บคำสั่งสำหรับวาดคอมโพเนนต์นี้ ใน Java Swing ควร override paintComponent() แทน paint() เพราะ paint() เรียก paintBorder() paintComponent() และ paintChildren() ไม่ควรเรียกเมธอดนี้โดยตรง ควรเรียก repaint() แทน
- repaint(): เมธอดนี้ไม่สามารถ override ได้ ควบคุมวงจร update() -> paint() เรียกเมธอดนี้เพื่อให้คอมโพเนนต์วาดตัวเองใหม่ หากมีการเปลี่ยนแปลงลักษณะแต่ไม่ใช่ขนาด ให้เรียกเมธอดนี้
ตัวอย่างโค้ด
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class PaintRepaintTest extends JPanel implements MouseListener {
private Vector v;
public PaintRepaintTest() {
v = new Vector();
setBackground(Color.white);
addMouseListener(this);
}
public void paint(Graphics g) { // paint() method
super.paint(g);
g.setColor(Color.black);
Enumeration enumeration = v.elements();
while (enumeration.hasMoreElements()) {
Point p = (Point) (enumeration.nextElement());
g.drawRect(p.x - 20, p.y - 20, 40, 40);
}
}
public void mousePressed(MouseEvent me) {
v.add(me.getPoint());
repaint(); // เรียก repaint() method
}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.getContentPane().add(new PaintRepaintTest());
frame.setTitle("PaintRepaint Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(375, 250);
frame.setVisible(true);
}
}
ในโปรแกรมข้างต้น หากคลิกบนหน้าจอ จะวาดสี่เหลี่ยมจัตุรัส ในเมธอด mousePressed() เราเรียก repaint()
ผลลัพธ์
