2014-06-12 33 views
0

在NetBeans 8.0中,我製作了一個Paint程序,我需要我的主文件中的代碼影響我的其他文件(接口文件)中的某些內容。我怎樣才能做到這一點?我的代碼:我如何影響另一個包含代碼的文件?

package paintapp; 


import java.awt.event.ActionEvent; 
import javax.swing.*; 
import java.awt.event.ActionListener; 

public class PaintApp extends javax.swing.JFrame { 

    int colourR, colourG, colourB; 
    static String Ccolour = "BLACK"; 

    public static void main(String[] args) { 
     JFrame main = new JFrame("Tessellating Pi - Paint"); 
     PaintInterface pi = new PaintInterface(); 
     main.add(pi); 
     main.setSize(1000, 1000); 
     main.setVisible(true); 
     JFrame j = new JFrame("Colour Chooser"); 
     JButton c = new JButton("Change Colour"); 
     j.add(c); 
     j.setSize(150, 100); 
     j.setVisible(true); 
     c.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if ("BLACK".equals(Ccolour)) { 
        Ccolour = "RED"; 
        //code to change code in interface to set colour to red 
       } 
      } 
     } 
     ); 
    } 
} 

這是接口:

import java.awt.*; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import javax.swing.*; 

public class PaintInterface extends JPanel implements MouseListener, MouseMotionListener { 

    static int x = 0, y = 0; 

    @Override 
    public void paint(Graphics g) { 
     this.setBackground(Color.white); 
     this.addMouseMotionListener(this); 
     this.addMouseListener(this); 
     g.setColor(Color.black); 
     g.fillRect(x, y, 3, 3); 
    } 

    @Override 
    public void mouseDragged(MouseEvent e) { 
     x = e.getX(); 
     y = e.getY(); 
     repaint(); 
    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 
    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
    } 

    @Override 
    public void mouseMoved(MouseEvent e) { 
    } 
} 

我需要將消息發送給我換了顏色的接口。我該怎麼做呢?有沒有其他的方法來做到這一點?

+1

'..extends JPanel .. @Override .. public void paint(Graphics g){'對於Swing組件,重寫'paintComponent'而不是'paint'。當覆蓋任何一個時,首先調用超級方法繪製BG和邊框等。 –

回答

0

簡單:

  1. 創建一個setter方法來設置新的顏色。
  2. 致電repaint更新你的形狀。
0

首先,你可能想有Performing Custom PaintingPainting in AWT and Swing讀,然後從paint方法addMouseMotionListeneraddMouseListenersetBackground刪除(或使用paintComponent)來代替。

接下來,你需要決定的最好的方式爲您處理色彩管理,例如,你可以簡單地使用在JPanelsetForeground和調整時,你畫的Graphics顏色...

@Override 
public void actionPerformed(ActionEvent e) { 
    if ("BLACK".equals(Ccolour)){ 
     Ccolour="RED"; 
     pi.setForeground(Color.RED); 
     pi.repaint(); 
    } 

} 

這意味着,pi的人都需要成爲一個實例變量或final ...

final PaintInterface pi=new PaintInterface(); 

然後在您的PaintInterface類,你將需要更新ŧ他paintComponent方法

@Override 
protected void paintComponent (Graphics g) { 
    super.paintComponent(g); 
    g.setColor(getForeground()); 
    g.fillRect(x,y,3,3); 

} 

我也想阻止你從initi thread問題在main方法創建整個應用程序分開,你遇到了各種各樣的問題與static引用和重用性...

相關問題