Hello all! I am attempted to re-write my version of the brickbreaker game that I did back in high school, and I'm having some issues getting started - the logic behind using multiple classes and how I need to format it seems to be lost on me at the moment.
My short-term goal is to be able to write a class that draws the ball on the screen. Later on I'm going to obviously have the ball moving along the screen and interacting with the walls and paddle and such, but I'm not there yet. Right now I'm just trying to determine how to format everything in the main class and the ball class to draw it on the screen. I'll need to be able to draw the circle on the screen at the x, and y, and give it a size and such, which is normally easy to do, but I don't know where to actually put the code if I'm using a separate class to format the ball..
I've just been adding and deleting stuff repeatedly to the point I'm getting frustrated and don't know where to begin... Here is what I've currently got:
Main class:
Ball Class:Code:import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class brickbreaker extends JApplet { public static void main (String[] args){ JFrame frame = new JFrame("BrickBreaker: By Michael Amaral (2011)"); frame.setVisible(true); frame.setSize(1000,1000); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBackground(Color.BLACK); int ballX=500; int ballY=500; int diamater = 20; Ball mainBall = new Ball(); mainBall.setPos(ballX, ballY); } }
Any ideas?Code:import javax.swing.*; import java.awt.*; public class Ball extends JPanel { int ballXPos; int ballYPos; int diameter=30; public void setPos (int ballX, int ballY){ ballXPos = ballX; ballYPos = ballY; } public int getXPos (){ return ballXPos; } public void paint(Graphics g){ g.setColor(Color.RED); g.fillOval(ballXPos,ballYPos,diameter,diameter); } }


Reply With Quote
Bookmarks