Results 1 to 3 of 3
- 09-08-2010, 09:08 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 2
- Rep Power
- 0
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.
- 09-08-2010, 11:17 PM #2
Define the ArrayList using generics so its get method is defined to return a Block object.Is possible to use the objects inside the list as referance like:
blockList.get(5).someBlockMethod();
ArrayList<Block>
- 09-08-2010, 11:24 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
adding or removing an object from an array of objects
By javanew in forum New To JavaReplies: 1Last Post: 05-04-2010, 11:00 AM -
How do i do this method(objects)
By Meta in forum New To JavaReplies: 1Last Post: 04-19-2010, 10:38 PM -
drawing ellipse by drawline method?
By hopey in forum Java 2DReplies: 8Last Post: 04-18-2009, 11:52 PM -
Need help with drawing multiple objects with mouseClicked method.
By busdude in forum New To JavaReplies: 6Last Post: 04-05-2009, 11:28 PM -
Adding a Polyline drawing to a tab
By CirKuT in forum New To JavaReplies: 1Last Post: 11-19-2008, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks