2013-03-29 64 views
0

嘿,我想創建一個小程序「白板」,在瀏覽時做粗糙的工作。它包括繪圖,粗糙,書寫等功能。 當用戶單擊並拖動鼠標時,使用drawLine命令繪製路徑。 我已經創建了一個ObjectDrawer類,該類偵聽MouseDrag事件,然後使用Graphics g繪製對象。 我使用這個命令來獲取圖形g,並且我知道它不正確,但我找不到解決方案 Graphic g = obj.getGraphics();在java中使用圖形g的Paint()外部

Morever在創建Applet時,我們不創建任何初始化該過程的對象,它會自動調用init()。所以如何使用applet類的變量。我的意思是如果我爲WhiteBoard類創建一個對象,無論我創建了多少個對象,變量始終具有相同的值? Eg.Suppose Applet正在使用drawstatus變量作爲circle.Now我創建一個對象obj.Does obj.drawStatus是行還是圓?

public class WhiteBoard extends Applet { 
    public static int lastx=0;public static int lasty=0;public static String drawStatus="line"; 

    public void init(){ 
     setLayout(new BorderLayout()); 
     MainPanel p=new MainPanel(); 
     add(p,BorderLayout.SOUTH); 
     setBackground(Color.WHITE); 
     setForeground(Color.BLUE); 
     addMouseListener(new PositionRecorder()); 
     addMouseMotionListener(new ObjectDrawer()); 
    } 

    public void record(int x,int y){ 
     lastx=x; 
     lasty=y; 
    } 

} 


public class ObjectDrawer extends MouseAdapter { 
    WhiteBoard obj=new WhiteBoard(); 
    int lastx=WhiteBoard.lastx; 
    int lasty=WhiteBoard.lasty;int x,y; 
    String status=WhiteBoard.drawStatus; 
    public void MouseDragged(MouseEvent event){ 
     x=event.getX(); 
     y=event.getY(); 
     Graphics g=obj.getGraphics(); 
     g.setColor(Color.cyan); 

     if(status.equals("line")){ 
      g.drawLine(lastx,lasty,x,y); 
     } 
     if(status.equals("rectangle")){ 

      g.drawRect(lastx,lasty,x-lastx,y-lasty); 
     } 
     if(status.equals("circle")){ 
      int r=(int)Math.sqrt(Math.pow(x-lastx,2)+Math.pow(y-lasty, 2)); 
      g.drawOval(lastx,lasty,2*r,2*r); 
     } 

    } 
} 

,這裏將在applet或其他地方的克(圖形)漆呢,是不是正確的創建對象和使用的getGraphics呢?因爲它初始化小程序,這obj中的對象的arent唯一same.I意思初始化applet的obj可以改變圖形?

public class PositionRecorder extends MouseAdapter { 
    WhiteBoard obj=new WhiteBoard(); 
    public void mouseEntered(MouseEvent event) { 
     //requestFocus(); // Plan ahead for typing 
     obj.record(event.getX(), event.getY()); 
    } 

    public void mousePressed(MouseEvent event) { 
     obj.record(event.getX(), event.getY()); 
    } 
} 

對不起,這個問題變得有點漫長和困惑,因爲我不明白真正的問題是什麼。 感謝您的閱讀 編輯: 我的新代碼which worked.Pleasse解釋爲什麼現在它的工作?我的面板(按鈕)只有當我在南部地區移動我的鼠標,否則它根本不可見嗎?在面板的applet中設置可見(true)?

import javax.swing.*; 
    import java.awt.BorderLayout; 
    import java.awt.Color; 
    import java.awt.Font; 
    import java.awt.Graphics; 
    import java.awt.event.*; 
    import java.awt.event.MouseEvent; 

    public class WhiteBoard extends JApplet implements   MouseListener,MouseMotionListener,KeyListener{ 
public int lastx=0;public int lasty=0; 
Graphics g;Font f; 
public void init(){ 

    MainPanel p=new MainPanel(); 
    getContentPane().add(BorderLayout.SOUTH,p); 
    setBackground(Color.WHITE); 

    addMouseListener(this); 
    addMouseMotionListener(this); 
    addKeyListener(this); 

    g=getGraphics();g.setColor(Color.BLUE); 
    f=new Font("SERIF",Font.BOLD,16); 
    g.setFont(f); 
    } 

public void record(int x,int y){ 
    lastx=x; 
    lasty=y; 
} 

public void paint(Graphics g){ 



} 
public void mouseMoved(MouseEvent event){ 
    record(event.getX(),event.getY()); 
} 
public void mouseEntered(MouseEvent event) { 
     requestFocus(); // Plan ahead for typing 
     record(event.getX(), event.getY()); 

    } 

    public void mousePressed(MouseEvent event) { 
     record(event.getX(), event.getY()); 

    } 

    public void mouseDragged(MouseEvent event){ 
     int x,y; 
     x=event.getX(); 
     y=event.getY(); 


      g.drawLine(lastx,lasty,x,y); 
      record(x,y); 
     } 


    public void keyTyped(KeyEvent ke){ 

     String msg=String.valueOf(ke.getKeyChar()); 
     g.drawString(msg,lastx,lasty); 
     record(lastx+9,lasty); 
    } 

    public void keyReleased(KeyEvent ke){} 
    public void keyPressed(KeyEvent ke){} 
    public void mouseClicked(MouseEvent event){} 
    public void mouseExited(MouseEvent event){} 
    public void mouseReleased(MouseEvent event){} 

    } 
+0

和斜面的drawRect,drawOval取float值?還有其他方法可以繪製,以便它們可以採用浮點值以便圖形平滑嗎? – Atul

+0

爲什麼白板類有靜態字段?實例變量不會更有意義嗎? – Cruncher

+0

在使用JApplet時,背景色未發生變化。這就是爲什麼 – Atul

回答

0

您需要重寫applet的paint()方法。

@override 
public void paint(Graphics g) 
{ 
    super.paint(g); 
    g.setColor(Color.cyan); 

    if(status.equals("line")){ 
      g.drawLine(lastx,lasty,x,y); 
    } 
    if(status.equals("rectangle")){ 
      g.drawRect(lastx,lasty,x-lastx,y-lasty); 
    } 
    if(status.equals("circle")){ 
      int r=(int)Math.sqrt(Math.pow(x-lastx,2)+Math.pow(y-lasty, 2)); 
      g.drawOval(lastx,lasty,2*r,2*r); 
    } 

} 

在的mouseDragged()你將需要x和y保存爲實例變量雖然

後,在的mouseDragged()調用重繪()

+1

請注意,這隻會實際繪製最後一件事。如果您希望在繪製後保存它,則需要保存信息以將所有內容都繪製在繪圖中。策略設計模式可能是有用的 – Cruncher

+0

使用策略/命令繪製事物的好處是,它使撤消非常容易 – Cruncher

+0

:我們的老師說它非常簡單。我認爲一定有其他方式來做到這一點。但是,如果我遵循你所建議的,保存在整數x和y中的實例變量需要傳遞給paint方法/或repaint.And我將如何通過它?我google了一下,我來了解一些bufferImage/paint,我沒有請記住,它會有幫助嗎? – Atul