2014-02-13 44 views
1

我有一個具有swing用戶界面類的應用程序,它具有將變量發送到canvas類的按鈕,在另一個函數中聲明的畫布上繪圖

public class createWindow extends JFrame implements ActionListener 
{ 
    createCanvas canvas = new createCanvas(10, 10); 
    JPanel mainPanel = new JPanel(); 
    public createWindow() 
    { 
     mainPanel.add(canvas, BorderLayout.CENTER); 
    } 
} 

createCanvas是聲明paintComponent的類;

public class createCanvas extends JPanel 
{ 
    int xValue; 
    int yValue; 
    int xCoordinate; 
    int yCoordinate; 

    public createCanvas(int x, int y) 
    { 
     xValue = x; 
     yValue = y; 
    } 

    public void setXCoordinate(int x) 
    { 
     xCoordinate = x; 
    } 

    public void setYCoordinate(int y) 
    { 
     yCoordinate = y; 
    } 

    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     drawGrid(g, this.xValue, this.yValue); 
     g.fillArc(xCoordinate, yCoordinate, 6, 6, 0, 360); 
    } 

    public drawGrid(Graphics g, int xLength, int yLength) 
    { 
     //creates a grid that is xLength x yLength 
    } 
} 

然而,也有我的選擇,我想有一個.draw()函數,它可以使用在createCanvas中的paintComponent對象。 問題是,當我需要在網格上繪製節點時,我可以設置座標,但是如何在createWindow中聲明的畫布上顯示節點?

public class Node() 
{ 
    int xCoordinate; 
    int yCoordinate; 

    //Suitable constructors, sets, gets 
    public void draw() 
    { 
     createCanvas canvas = new createCanvas(); 
     canvas.setXCoordinate(this.xCoordinate); 
     canvas.setYCoordinate(this.yCoordinate); 
     canvas.repaint(); 
    } 
} 

所以,我想知道是否有我的方式讓我所繪製CreateWindow的畫布上,還有什麼我在Object類畫。

謝謝。

+2

提示:使用的Java命名約定。班級名稱以大寫字母 –

+0

開頭謝謝您的提示,對此問題的任何幫助或問題不明確? – user3304183

+0

你的問題不清楚。什麼是'createCanvas'和你試圖完成什麼exactyl。你在說什麼東西? –

回答

1

你想要做的是有你的對象draw方法採取Graphics參數。此Graphics對象將與您的paintComponent方法中的相同Graphics上下文相同。您可以在您的JPanel類中創建該對象。事情是這樣的

public class Circle { 
    int x, y; 

    public Circle(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 

    public void drawCirlce(Graphics g) { 
     g.fillRect(x, y, 50, 50); 
    } 
} 

然後在你JPanel

public class CirclePanel extends JPanel { 
    Circle circle = new Circle(100, 100); 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     circle.drawCircle(g); 
    } 
} 

您可以在Circle類具有對xy一個setter。你應該改變他們在你的JPanel類的某個地方,然後再打repaint()。您可以通過按一個鍵來移動它,或者您可以使用java.util.Timer對其進行設置。例如

隨着Timer

public class CirclePanel extends JPanel { 
    Circle circle = new Circle(100, 100); 

    public CirclePanel() { 
     Timer timer = new Timer(50, new ActionListener(){ 
      @Override 
      public void actionPerfomed(ActionEvent e) { 
       circle.x += 10; 
       repaint(); 
      } 
     }); 
     timer.start(); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     circle.drawCircle(g); 
    } 
} 

隨着key binding

public class CirclePanel extends JPanel { 
    Circle circle = new Circle(100, 100); 

    public CirclePanel() { 
     InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED_IN_WINDOW); 
     inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "moveRight"); 
     getActionMap().put("moveRight", new AbstractAction(){ 
      public void actionPerformed(ActionEvent e) { 
       circle.x += 10; 
       repaint(); 
      } 
     }); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     circle.drawCircle(g); 
    } 
} 

隨着button press

public class CirclePanel extends JPanel { 
    Circle circle = new Circle(100, 100); 

    public CirclePanel() { 
     JButton button = new JButton("Move Right"); 
     button.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) { 
       circle.x += 10; 
       repaint(); 
      } 
     }); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     circle.drawCircle(g); 
    } 
} 
+0

按下按鈕怎麼樣? – user3304183

+0

這也可以正常工作。只需更改按鈕偵聽器的'actionPerformed'中的座標和'repaint' –

+0

我已經添加了按鈕按下的示例。 –

相關問題