為何一開jColorChooser.原本畫面就會變空白 |
尚未結案
|
totol
一般會員 發表:3 回覆:2 積分:1 註冊:2003-05-14 發送簡訊給我 |
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
|
totol
一般會員 發表:3 回覆:2 積分:1 註冊:2003-05-14 發送簡訊給我 |
我是用jbuilder寫的..
是寫三個按鈕,一個筆,一個擦子,一個是改筆的顏色。
不過當我要按下改顏色的按鈕時,原本畫板的線條也就不見了
所以我想問的是,是不是叫出JColorChooser時,就會把畫面清除啊???
謝謝... package test1; import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*; public class Applet1 extends Applet {
boolean isStandalone = false;
Button button1 = new Button();
Button button2 = new Button(); final int CIRCLESIZE=20;
private Point lineStart=new Point(0,0);
private Graphics g;
int x;
Color color;
JButton jButton1 = new JButton(); //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public Applet1() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
button1.setLabel("筆");
button1.setBounds(new Rectangle(14, 7, 75, 41));
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.setLayout(null);
button2.setLabel("擦");
button2.setBounds(new Rectangle(93, 7, 83, 40));
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
this_mousePressed(e);
}
});
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
this_mouseDragged(e);
}
});
jButton1.setBounds(new Rectangle(180, 8, 75, 39));
jButton1.setText("換顏色");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
this.add(button1, null);
this.add(button2, null);
this.add(jButton1, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} void this_mousePressed(MouseEvent e) {
lineStart.move(e.getX(),e.getY());
} void this_mouseDragged(MouseEvent e) {
g=getGraphics(); if(x==2)
{
g.setColor(getBackground());
g.fillOval(e.getX()-(CIRCLESIZE/2),e.getY()-(CIRCLESIZE/2),CIRCLESIZE,CIRCLESIZE);
}
if(x==1)
{
g.setColor(color);
g.drawLine(lineStart.x,lineStart.y,e.getX(),e.getY());
}
lineStart.move(e.getX(),e.getY());
g.dispose();
} void button1_actionPerformed(ActionEvent e) {
x=1;
} void button2_actionPerformed(ActionEvent e) {
x=2;
} void jButton1_actionPerformed(ActionEvent e) {
color=JColorChooser.showDialog(this,"換顏色",color);
if(color!=null)
g.setColor(color);
}
}
|
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
totol您好: 您的問題應該是:您只是把圖形暫時畫在該applet所顯示的位置上,這些畫上去的圖形並沒有成為該applet的一部分,所以即便是您將該程式先最小化,再還原其大小,可能原先您畫的東西亦將消失無蹤。
因此當您開啟對話盒時,被對話盒蓋住的部分在對話盒消失後,應該也就跟著不見了。
這是因為您在程式啟動之後,所畫的圖形,並沒有動態的加入此applet的重繪內容中,或者說當applet重繪時,根本不知道您加了多少資料,只會將原程式的初始畫面重繪出來而已。
所以如果要避免當程式被其他元件覆蓋後消失不見,就必須將新繪的內容存下來,並在重繪的程式部份paint()中,將他們畫出來。 希望對您的問題有幫助!
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |