![](/img/trans.png)
[英]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.