[英]How do I implement a method that returns in Java without knowing what object type I'm returning?
[英]How do I find a certain method to use in java knowing only what the method should do?
我正在為使用簡單Java形狀的游戲設計背景。
我正在嘗試引用Waves類,該類繪制一個對象,並將其像另一個類中的對象一樣使用,以便可以通過X,Y坐標移動它。 我這樣做是因為我需要多次使用它。 我不知道移動被調用對象的方法。
就我而言,我可以使用哪種方法和/或在API中搜索什么? Waves是否還會擴展JPanel?
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);
}
}
因此,請調用wave的paintComponent()。
盡管問題在於您在每幅繪畫上都每次都重新創建波浪。 Imo它應該是Stickman的成員,並且只能在構造函數中初始化/創建一次,而不是每次繪制窗口時都進行初始化/創建。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.