简体   繁体   English

我如何仅在知道该方法应该做什么的情况下找到一种在Java中使用的方法?

[英]How do I find a certain method to use in java knowing only what the method should do?

I'm Designing a background for a game using simple java shapes. 我正在为使用简单Java形状的游戏设计背景。

I'm trying to refer to a class Waves, that draws an object, and use it like an object within another class so I can move it via X,Y coordinates. 我正在尝试引用Waves类,该类绘制一个对象,并将其像另一个类中的对象一样使用,以便可以通过X,Y坐标移动它。 I'm doing this because I need to use it multiple times. 我这样做是因为我需要多次使用它。 I do not know the method by which to move a called object though. 我不知道移动被调用对象的方法。
In my case what method could I use and/or what would I search for in the API? 就我而言,我可以使用哪种方法和/或在API中搜索什么? Does Waves also extend JPanel? 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);

    }
}

So then call paintComponent() of waves. 因此,请调用wave的paintComponent()。

Though the problem is that on each paint you recreate waves each time anew. 尽管问题在于您在每幅绘画上都每次都重新创建波浪。 Imo it should be a member of Stickman and be only initialized/created once, in constructor, not every time the window is painted. Imo它应该是Stickman的成员,并且只能在构造函数中初始化/创建一次,而不是每次绘制窗口时都进行初始化/创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM