다른 클래스에서 선언 된 이미 선언 된 인스턴스에서 메서드를 가져 오는 방법에 대한 아이디어는 무엇입니까?새 인스턴스를 선언하지 않고 메서드를 얻는 방법
class GrowPanel extends JComponent {
private int x;
private int y;
private Timer timer;
ButtonPanel b;
public GrowPanel() {
x = 10;
y = 10;
startPaiting();
}
public void startPaiting() {
timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
changeState();
repaint();
}
},0, 100);
}
public void pause(){
timer.cancel();
startPaiting();
}
public void start(){
timer.cancel();
x = 10;
y = 10;
startPaiting();
}
public void paintComponent(Graphics g){
g.fillOval(x, y, 10, 10);
}
private void changeState(){
x+=10;
if(x >= 400) {
y+=10;
x = 0;
}
if(y >= 300){
y = 10;
}
}
클래스
public class Grow {
public static void main(String [] args) {
JFrame frame = new JFrame();
final GrowPanel growPanel = new GrowPanel();
ButtonPanel btnPanel = new ButtonPanel();
frame.add(growPanel);
frame.add(btnPanel, BorderLayout.SOUTH);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
ButtonPanel 클래스
public class ButtonPanel extends JPanel implements ActionListener{
JButton btn;
public ButtonPanel() {
btn = new JButton("Pause");
add(btn);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("Pause")){
System.out.println("RESUME");
//growPanel.pause();
btn.setText("Resume");
} else {
System.out.println("PAUSE");
// growPanel.start();
btn.setText("Pause");
}
}
}
GrowPanel 클래스 성장
}
Grow에서 GrowPanel의 새 인스턴스를 이미 선언했습니다. ButtonPanel 안에 GrowPanel의 새 인스턴스를 선언하지 않고 ButtonPanel에서 GrowPanel의 메서드를 가져 오는 방법을 알지 못합니다. 그 아이디어는 가능한가? 지금까지 나는 setters/getters, Singleton Pattern ...을 도울 수있는 몇 가지 주제를 들었다. 그러나 지금까지 그 아이디어는 파악하기 힘들다.
리플렉션을 사용해 보셨습니까? –