Results 1 to 3 of 3
  1. #1
    Pillow is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default Method for adding and drawing several objects

    Hello!

    My Issue:

    I've made a block class and I want to add and draw several of my blocks to the frame window. As you can see in the code, i've done a temporary "solution", but there should be a better way to add my block objects. I've barely tried to make a arrayList and add each object using loop, something like this:

    ArrayList blockList = new ArrayList();

    for (int i = 0; i < 13; i++ {
    block = new Block(x_pos, y_pos, width, height, color);
    blockList.add(i, block);
    }

    but if I do that, I initiate the object inside the loop making all the block methods unusable.
    Is possible to use the objects inside the list as referance like:
    blockList.get(5).someBlockMethod();

    The issue again if I didn't make myself clear:
    Can I add the block objects with a loop or something in that direction and still be able to use it's methods.

    (Since I'm pretty new to this, I do not exactly know what I'm doing atm, please bare with me :confused:)
    Here's a chunck of my code:

    The main method:

    public class Pong extends JPanel {

    // Oppretter en timer
    private javax.swing.Timer timer;

    Ball ball;
    Player player;
    Block block;
    Block block1;
    Block block2;
    Block block3;

    // Size of the blocks
    private int block_width = 100;
    private int block_height = 30;

    Color color;

    // Initiates the Ball, Player and Blocks
    Pong(Color backColor, int width, int height) {

    setBackground(backColor);

    setPreferredSize(new Dimension(width, height));

    // Positions the ball, gives it a color and sets the direction
    // and velocity.
    ball = new Ball(width / 3, height / 3, 10, Color.black);
    ball.setDirection(45);
    ball.setVelocity(1);

    // Positions the player and sets its velocity.
    player = new Player(300, 550, 100, 30, color.black);
    player.setVelocity(5);


    // !*TEMPORARY*!
    // Initiates 13 blocks with diffrent coordinates
    block = new Block(0, 0,
    block_width, block_height, Color.blue);


    block1 = new Block(100 , 0,
    block_width, block_height, Color.yellow);


    block2 = new Block(200, 0,
    block_width, block_height, Color.blue);


    block3 = new Block(300 , 0,
    block_width, block_height, Color.yellow);

    Where the everything is drawn:

    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ball.drawBall(g);
    player.drawPlayer(g);

    block.drawBlock(g);

    block1.drawBlock(g);

    block2.drawBlock(g);

    block3.drawBlock(g);

    the block class:

    class Block {

    private int x_pos, y_pos, height, width;

    private Color color;

    Block(int x, int y, int w, int h, Color c) {

    x_pos = x;
    y_pos= y;
    width = w;
    height = h;
    color = c;

    }

    // Draws the blocks
    void drawBlock(Graphics g) {
    g.setColor(color);
    g.fillRect(x_pos, y_pos, width, height);
    }

    I've uploaded my code in .txt files if you need a closer look.
    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Is possible to use the objects inside the list as referance like:
    blockList.get(5).someBlockMethod();
    Define the ArrayList using generics so its get method is defined to return a Block object.
    ArrayList<Block>

  3. #3
    Pillow is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2010, 12:00 PM
  2. How do i do this method(objects)
    By Meta in forum New To Java
    Replies: 1
    Last Post: 04-19-2010, 11:38 PM
  3. drawing ellipse by drawline method?
    By hopey in forum Java 2D
    Replies: 8
    Last Post: 04-19-2009, 12:52 AM
  4. Replies: 6
    Last Post: 04-06-2009, 12:28 AM
  5. Adding a Polyline drawing to a tab
    By CirKuT in forum New To Java
    Replies: 1
    Last Post: 11-19-2008, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •