简体   繁体   中英

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.

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. 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? Does Waves also extend 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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