2
我是java初學者,&我創建的程序使用箭頭鍵繪製線條,其中下一行從第一行的末尾開始。 現在,我創建了它,但問題在於,無論何時重新調整窗口大小,圖形都會消失。paintComponent中的圖形在調整JFrame大小後消失
這裏是我的代碼:
public class draw extends JPanel {
int up = 1, down = 0, left = 0, right = 0;
int beginX, beginY;
@Override
public void paintComponent(Graphics g) {
if(up == 1){
beginX = getWidth()/2;
beginY = getHeight()/2;
}
if (up > 0) {
g.drawLine(beginX, beginY, (beginX), (beginY - up));
beginY -= (up);
up = 0;
} else if (down > 0) {
g.drawLine(beginX, beginY, beginX, (beginY + down));
beginY += down;
down = 0;
} else if (right > 0) {
g.drawLine(beginX, beginY, (beginX + right), beginY);
beginX += right;
right = 0;
} else if (left > 0) {
g.drawLine(beginX, beginY, (beginX - left), beginY);
beginX -= (left);
left = 0;
}
}
public void drawUp() {
up += 3;
repaint();
}
public void drawDown() {
down += 3;
repaint();
}
public void drawLeft() {
left += 3;
repaint();
}
public void drawRight() {
right += 3;
repaint();
}
}
你能否給我一些關於緩衝區的信息,或者給我一些鏈接以供參考.. 謝謝 – Jaydeep 2012-02-12 07:48:08