2012-02-27 43 views
0

我正在設計使用簡單的Java形狀的遊戲的背景。

我想要引用一個類Waves,它繪製一個對象,並將它用作另一個類中的對象,以便我可以通過X,Y座標移動它。我這樣做是因爲我需要多次使用它。我不知道移動被調用對象的方法。
在我的情況下,我可以使用什麼方法和/或在API中搜索什麼?Waves是否也擴展JPanel?如何在java中使用某種方法來知道該方法應該做什麼?


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

public class Stickman extends JPanel{ 

public void paintComponent(Graphics g) 
{ 
    this.setBackground(new Color (135, 206, 235)); 
    //Check operation of "this" in API 

    final int XMID = 400; 
    final int YMID = 300; 

    Color Ocean = new Color (143, 188, 143); 
    Color Ship = new Color (139, 69, 19); 
    Color Sail = new Color (255, 228, 196); 

    Waves waves = new Waves(); //THIS IS THE PART WHERE I WANT TO CALL AND MOVE 
           //THE OBJECT 
    } 
} 

import java.awt.Color; 
import java.awt.Graphics; 

public class Waves 
{ 
public void paintComponent(Graphics g) 
{ 
    final int XMID = 400; 
    final int YMID = 300; 
    //small cirlce diameter 
    final int SMCD = 60; 
    double BGCD = SMCD * 2; 

    //wave base 
    g.fillRect(0, 462, 800, 28); 
    //first big circle (ARC) 
    g.fillArc(XMID-(SMCD/2) - 8, 480-SMCD - 8, (int)BGCD, (int)BGCD, 0, 130); 

    //first small circle 
    g.setColor(Color.CYAN); 
    g.fillOval(XMID-(SMCD/2),480-SMCD , SMCD, SMCD); 

    } 
} 
+0

Waves不會擴展任何東西或實現任何接口,所以您需要自己編寫方法!聽起來像是一個設計問題:你應該分開模型的實現「什麼是波?」 (包括wave的位置)從演示文稿中「我如何畫一個波?」 – John3136 2012-02-27 00:33:13

+0

我認爲他認爲Waves類是一個JPanel原因,有一個方法paintComponent。他或者錯過了他的代碼或者當他在SO – 2012-02-27 00:43:22

+0

中撰寫課程我是否也必須將Waves課程擴展到JPanel? – gnawthatthingoffyourface 2012-02-27 00:47:50

回答

0

所以隨後致電波的paintComponent()。

雖然問題在於每次刷新時都會重新創建波浪。 Imo它應該是火柴人的成員,並且只能在構造函數中初始化/創建一次,而不是每次窗口被繪製。

相關問題