Need Help with Drawing using different Classes
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Need Help with Drawing using different Classes

  1. #1
    Join Date
    Oct 2011
    Posts
    1

    Need Help with Drawing using different Classes

    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:

    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);
    
        }
    
        }
    Ball Class:

    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);
    
       }
    }
    Any ideas?

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Need Help with Drawing using different Classes

    A minor point but your Ball class should probably extend JComponent rather than JPanel.

    Why is brickbreaker (which should really be called BrickBreaker to conform to Java naming standards) extending applet and then using a main method to create a standalone app? Are you writing an Applet or a standalone application?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center