I made a creature composed of shapes in a Java Applet. I want to basically copy and paste the creature I made so I have say 7-8 copies on the same applet. but in different locations. Is there an easy way besides copying the entire code, and moving each coordinate by the same amount?

Thanks

Can you explain what you want to copy?
Is it the image that is displayed on the screen? Use the Robot class to capture the screen and then write it to a disk file.

What do you mean by "copies of the same applet"? Are you talking about java classes that extend the Applet class?

Please explain what you are trying to do

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

public class extracred extends JApplet
{
    Container c;

public void init()
{
    c = getContentPane();
    c.setBackground(Color.white);
}

public void paint(Graphics g)
 {
     super.paint(g);
    int radius, x = 150, y = 220;

     g.fillOval(100,100,100,50);
     g.fillOval(110,70,30,30);
     g.fillOval(165,70,30,30);
     g.drawLine(110,130,40,40);
     g.drawLine(190,130,250,40);
     g.drawLine(40,40,30,100);
     g.drawLine(250,40,270,100);

 }
}

I drew the code using lines and shapes this way.
I want to make say 7 or 8 of these in the applet, but I don't want to have to copy and paste and slightly change the coordinates of each one

Please explain what "drew the code" means? Did you mean: drew the shapes?
Are you asking about the calls to the draw... methods in the paint() method?
Do you want to call the draw methods with different x,y locations?
How are the x,y locations shown in the posted code related? Can you write an expression to compute the x,y locations for the draw methods based on what the first draw method uses?

Make a method and pass the method the x,y locations.

but I don't want to have to copy and paste and slightly change the coordinates of each one

you mean you wan't them to have the same dimensions but positioned in different parts of the applet right?

I want to basically copy and paste the creature I made so I have say 7-8 copies on the same applet.

Is it an image or made with drawLines similar to the code you posted?

Now back then, I made a game that's suppose to have multiple characters that have the same function throughout the game but each is uniquely affected by the user interactiona and gaming environment so I made several instance of the class that contains the game character information and positioned them in different parts of the applet which wasn't too hard since I only changed a few coordinate that affects how they are initially positioned at the start of the game

Note I'm using an image for the character so the changing coordinates didn't became much of a problem

Now if it's just a simple shape that is drawn in the paint method it would be different, I guess using a loop might be hard(unless you found a solution/algorithm to precisely place the object in the different coordinates you want) since much of the work would be trial and error

Now if the case is the latter there might be a simpler solution to this that I'm not familiar with yet so maybe we should wait and see for a response from another member

I guess I'll just make copies by copying the shapes I drew and moving the coordinates.

How do you "copy the shapes"? Do you mean copy the source code that draws the shapes?
If all the shapes are unique, then you would need unique code to draw each shape.
If the shapes are the same but at different locations, then a make a method to draw the shape and pass it the location where the shape is to be drawn.

Norm's advice makes perefect sense. Alternatively, if you are going to be doing a lot of these drawings (eg animation) then you can create a new BufferedImage, get its Graphics, and draw your Shapes into that Graphics. Now you have your creature as a simple Image that you can draw with a single method call wherever/whenever you want.

make a method to draw the shape and pass it the location where the shape is to be drawn

That sounds like a good idea. The thing is I'm not very good enough to figure out how to do that. Could you give me any hints/tips on how I can go about doing this?

Create method that takes 3 args: Graphics object, x and y.
Move the draw methods from the paint() method to the new method. Change the calls to the draw methods to use the x and y values passed to the method.

You should use the double buffering provided by Swing. Define a new class that extends JPanel and move the paint method (renamed to paintComponent) to the new class.
Create an instance of the class and add it to your GUI.

Edited 4 Years Ago by NormR1

O Alright that helps so much! Thanks for all the help even though I'm such a noob.

This article has been dead for over six months. Start a new discussion instead.