คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 1
Painting in AWT and Swing
http://www.oracle.com/technetwork/java/painting-140037.html
สรุปง่ายๆ คือ คำสั่งนี้ใช้ทำให้มีการวาดรูป component ใหม่
เช่น เวลาเราค่าตัวแปรอะไรที่ทำให้รูป component เปลี่ยนไป
เราต้องการให้มีการแสดงสถานะใหม่ ก็สั่ง repaint() เพื่อบอกว่า ต้องวาดใหม่นะ
มันจะจำไว้ว่าพื้นที่ตรงไหนบ้างที่ต้องวาดใหม่ แล้วมันจะไปเรียก paint() ภายหลัง
ถ้าสั่ง repaint() หลายๆ ครั้งติดๆ กันก่อนที่มันจะได้วาดจริง มันอาจจะรวมพื้นที่สี่เหลี่ยมที่ต้องวาด แล้วค่อยไปเรียก paint() ทีเดียวเลยก็ได้ ทำให้การทำงานมีประสิทธิภาพสูงขึ้น
http://www.oracle.com/technetwork/java/painting-140037.html
สรุปง่ายๆ คือ คำสั่งนี้ใช้ทำให้มีการวาดรูป component ใหม่
เช่น เวลาเราค่าตัวแปรอะไรที่ทำให้รูป component เปลี่ยนไป
เราต้องการให้มีการแสดงสถานะใหม่ ก็สั่ง repaint() เพื่อบอกว่า ต้องวาดใหม่นะ
มันจะจำไว้ว่าพื้นที่ตรงไหนบ้างที่ต้องวาดใหม่ แล้วมันจะไปเรียก paint() ภายหลัง
ถ้าสั่ง repaint() หลายๆ ครั้งติดๆ กันก่อนที่มันจะได้วาดจริง มันอาจจะรวมพื้นที่สี่เหลี่ยมที่ต้องวาด แล้วค่อยไปเรียก paint() ทีเดียวเลยก็ได้ ทำให้การทำงานมีประสิทธิภาพสูงขึ้น
แสดงความคิดเห็น
repaint(); ในภาษา JAVA คืออะไรเหรอค่ะ
ขออนุญาต แสดงโคด นะค่ะเพื่อที่จะได้เข้าใจ จุดประสงค์มากขึ้น
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class HittingRec extends JPanel implements ActionListener, MouseListener{
// position, width, and height of a moving character
Timer timer = new Timer(5000, this);
int xPos = 0;
int yPos = 0;
int width = 40;
int height = 40;
int x=0;
int y=0;
// player’s scores
int score = 0;
HittingRec(){
timer.start();
addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
customDrawingMethod(g);
}
public void customDrawingMethod(Graphics g) {
setBackground(Color.black);
double w = (getSize()).getWidth();
double h = (getSize()).getHeight();
double r1 = Math.random();
double r2 = Math.random();
int midX = (int)(w*r1);
int midY = (int)(h*r2);
xPos = midX-width/2;
yPos = midY-height/2;
g.setColor(Color.white);
g.fillRect (xPos, yPos, width, height); // head
g.setColor (Color.black);
g.fillOval (midX-10, midY-10, 5, 5); // left eye
g.fillOval (midX+5, midY-10, 5, 5); // right eye
g.drawArc (midX-10, midY, 20, 10, 190, 160); // smile
g.setColor(Color.yellow);
g.drawString("Hits = " + score, (int)(w-100), 20 );
}
public static void main(String[] args) {
JFrame frame = new JFrame("Hitting a Cartoon");
frame.add(new HittingRec());
frame.setSize(new java.awt.Dimension(600,400));
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void mouseReleased(MouseEvent m){
}
public void mouseEntered(MouseEvent m){
}
public void mousePressed(MouseEvent m){
x=m.getX();
y=m.getY();
if(x>=xPos&&y>=yPos&&x<=xPos+width&&y<=yPos+height){
score++;
}
x=0;
y=0;
}
public void mouseClicked(MouseEvent m){
}
public void mouseExited(MouseEvent m){
}
}
ขอบคุณมาก ๆ ค่ะ