Results 1 to 17 of 17
  1. #1
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Exclamation Using Java AWT and Canvas to draw an array of circles (big headache)

    I've been trying to get this code to work for quite some time.
    The program was designed to create an array of circles and then draw each one. I'm having an issue with my Ball2D class. What I'm thinking is that since it extends canvas, it creates a new canvas for every ball being drawn when its called and so then it overlaps each one. Is there ANY way to make the canvas transparent so that it can see the other canvas layers below it? Or, better yet, I would like to call my paint method directly in main with my array like balls[1].paint(Graphics g); if I could

    Anyway, here is the code of what I have thus far:

    This is my class for my vector work
    Java Code:
    package models;
    
    public class Vector2D
    {
    	int posX;
    	int posY;
    	public Vector2D(int x, int y)
    	{
    		posX = x;
    		posY = y;
    	}
    	
    	public int getX(){
    		return posX;
    	}
    	
    	public int getY(){
    		return posY;
    	}
    }
    This is my circle drawing class:
    Java Code:
    package models;
    import java.awt.*;
    
    public class Ball2D extends Canvas
    {
    	Vector2D ballCenter;
    	float ballRadius;
    	Color ballColor;
    	
    
    	
    	public Ball2D(Vector2D center, float radius, Color color)
    	{
    
    		ballCenter = center;
    		ballRadius = radius;
    		ballColor = color;
    
    	}
    
    		
    	public void paint(Graphics g)	
    	{
    
    		super.paint(g);
    		g.setClip(ballCenter.getX(),ballCenter.getY(),(int)ballRadius,(int)ballRadius);
    		g.setColor(ballColor);
    		g.fillOval(ballCenter.getX(),ballCenter.getY(),(int)ballRadius,(int)ballRadius);
    
    	
    	}
    	
    
    }

    And this is my main method here:
    Java Code:
    package demos;
    import models.Ball2D;
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import models.Vector2D;
    
    public class OrderedPainting 
    {
    	public static void main(String[]args)
    	{
    	Ball2D[] balls;
    	int numberOfBalls;
    	Frame frame;
    	Vector2D testVec = new Vector2D(20,20);
    //	Ball2D tester = new Ball2D(testVec,(float)20.2121,Color.RED);
    	
    	final int APP_WIDTH = 640;
    	final int APP_HEIGHT = 480;
    	
    	
    /*	Vector2D vector = new Vector2D(2,2);
    	Color test = new Color(30,20,10);	
    	Ball2D bawls = new Ball2D(vector, (float)20.33, test); */
    	
    	
    	
    	balls = new Ball2D[10];
    //	double something = Math.random();
    //	System.out.println(something);
    	
    	for(int i = 0; i < 10; i++)
    	{
    	double randomDouble = Math.random();
    	double randomX = Math.random()*APP_WIDTH;
    	double randomY = Math.random()*APP_HEIGHT;
    	double randomColor = Math.random()*255;
    	
    	
    	
    	Vector2D vector = new Vector2D((int)randomX,(int)randomY);
    	System.out.println("Vector Component of "+i+" is: "+(int)randomX+", "+(int)randomY);
    	Color test = new Color(180,(int)randomColor,(int)randomColor);	
    	
    	Ball2D bawls = new Ball2D(vector, (float)(randomDouble*640), test);		
    		
    	balls[i] = bawls;
    	
    	}
    
    
    	//balls[1].paint(g);
    	
    	
    	frame = new Frame("Random Balls");
    	
    	Color frameBackground = new Color(0,0,0,255);
    	frame.setBackground(frameBackground);
    	frame.setPreferredSize(new Dimension(APP_WIDTH, APP_HEIGHT + frame.getInsets().top));
    
    	frame.setVisible(true);
    	
    	//HERE IS THE PROBLEM, IT WILL CALL THIS 10 TIMES AND ONLY DRAW ONE CIRCLE.  THE ARRAY IS FULL
    	for(int i = 0; i < 9; i++){
    	frame.add(balls[i]);
    	}
    
    	
    
    	
    	frame.addWindowListener(new WindowAdapter() 
    	{
    		public void windowClosing(WindowEvent we) 
    		{
    			System.exit(0);
    		}
    	});
    	
    	frame.pack();
    	
    	}
    
    }

    The error starts occurring where I left the comment in capital letters, I'm new to AWT and we CANNOT USE SWING (which I wish we could!) So any help would be great. I've been scratching my head at this for quite some time now. It will just draw that SINGLE circle and that's it. There has to be a way to draw multiple circles on multiple canvases I can't change the logic, the paint(Graphics g) method has to stay in the Ball2D class
    Last edited by RET80; 01-29-2012 at 11:54 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Your problem is that you're adding a component to a top level window that by default uses BorderLayout, and so only one component will be shown. This issue is no different for AWT as it is for Swing. The solution -- use different layout managers. I'll let you decide which one(s) to use. You can find the tutorial here: Laying Out Components in a Container

  3. #3
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    I'm not too sure what you mean by BorderLayout and what do layout managers do?
    Also, the thing is, the canvases are layered ON TOP of each other. Does that make a difference?

  4. #4
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Well come to find out, in order to use a Layout Manager, I have to use a Jpanel (JPanel panel = new JPanel(new BorderLayout());)
    and that's part of the javax.swing package and I'm NOT allowed to use it.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by RET80 View Post
    Well come to find out, in order to use a Layout Manager, I have to use a Jpanel (JPanel panel = new JPanel(new BorderLayout());)
    Completely wrong. Layout managers are part of AWT and can be added to anything that extends Container such as your Frame, a Canvas or a Panel.

  6. #6
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by Fubarable View Post
    Completely wrong. Layout managers are part of AWT and can be added to anything that extends Container such as your Frame, a Canvas or a Panel.
    okay, so I used Panel instead, like the example shown like this:
    Panel panel = new Panel(new BorderLayout());

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());

    But the problem is that getContentPane() doesn't exist with AWT. I'm completely new to this so I don't understand very much as of yet. More help would be more appreciated, these example really don't show much, they're for more experienced users.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Set the layout on the Frame itself.

  8. #8
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by Fubarable View Post
    Set the layout on the Frame itself.
    Uh. Where, OR how. That's very vague of you.

    Are you talking about this line of code?
    contentPane.setLayout(new FlowLayout());

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by RET80 View Post
    Uh. Where, OR how. That's very vague of you.

    Are you talking about this line of code?
    contentPane.setLayout(new FlowLayout());
    Vague? You've got a Frame variable -- how vague is it to tell you to set its layout? Not its contentPane (which doesn't exist), but the Frame itself which again is referred to by the frame variable.

  10. #10
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by Fubarable View Post
    Vague? You've got a Frame variable -- how vague is it to tell you to set its layout? Not its contentPane (which doesn't exist), but the Frame itself which again is referred to by the frame variable.
    Well I set my frame variable to: frame.setLayout(new BorderLayout());
    and it still only draws one thing. Flow layout does nothing as well.

  11. #11
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Also grid layout seems to show more, but it chops up all the circles, which makes sense because its one section after another.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Argggh, nevermind. you want overlapping canvases....

    Sorry, let me look further....

  13. #13
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by Fubarable View Post
    Argggh, nevermind. you want overlapping canvases....

    Sorry, let me look further....
    Thank you very much :) The thing is, the program generates random circles of sizes and position on a singular canvas each time its called, so even if the layouts were called, it still draws that single object on its respective portion of the layout. Sorry to be such a hassle :(

    If there was a way for me to just call the canvas creation once and just keep using the draw method over and over again, I'd rather take that, but I wouldnt know exactly how to do that by pulling apart the Ball2D[] array...

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    As far as I know, you can't make Canvas transparent. A solution is to not have Ball2D extend Canvas, but still give it a paint or perhaps less misleading a draw method (similar to paint but different to note that Ball2D is no longer a Component-derived class):

    Java Code:
    class Ball2D { //!!
       Vector2D ballCenter;
       float ballRadius;
       Color ballColor;
    
       public Ball2D(Vector2D center, float radius, Color color) {
          ballCenter = center;
          ballRadius = radius;
          ballColor = color;
       }
    
       public void paint(Graphics g) {
          //!! super.paint(g);  //!! no longer a super method
          g.setClip(ballCenter.getX(), ballCenter.getY(), (int) ballRadius,
                (int) ballRadius);
          g.setColor(ballColor);
          g.fillOval(ballCenter.getX(), ballCenter.getY(), (int) ballRadius,
                (int) ballRadius);
       }
    }
    Then create another class that extends Canvas, say called DrawCanvas, where you will hold your balls (this sounds bad!) and draw them all, and give it a method to allow other classes to add balls, say addBall(Ball2D ball), and have this class's paint method iterate through the balls and draw them all:

    Java Code:
    class DrawingCanvas extends Canvas {
       List<Ball2D> balls = new ArrayList<Ball2D>();
       
       @Override
       public void paint(Graphics g) {
          super.paint(g);
          for (Ball2D ball : balls) {
             ball.paint(g);
          }
       }
    
       public void add(Ball2D ball) {
          balls.add(ball);
       }
    }
    Then you could add this to your frame:

    Java Code:
          frame = new Frame("Random Balls");
          DrawingCanvas drawCanvas = new DrawingCanvas(); //!!
          frame.add(drawCanvas); //!!
    
          Color frameBackground = new Color(0, 0, 0, 255);
          frame.setBackground(frameBackground);
          frame.setPreferredSize(new Dimension(APP_WIDTH, APP_HEIGHT
                + frame.getInsets().top));
    
          frame.setVisible(true);
    
          for (int i = 0; i < 9; i++) {
             drawCanvas.add(balls[i]); //!!
          }

  15. #15
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Strange. Even after all of that, and all of the changes that were appied, it does not draw anything to the screen; completely blank. You'd think once it hit 'drawCanvas.add(balls[i]);' it would have drawn them all. I'm looking into it now...

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    No, the addBalls doesn't draw anything. It just add's Ball2D's to the List. It only draws if you add your drawing canvas to the Frame. The Frame now should use its default BorderLayout and you shouldn't add anything else to the Frame.

  17. #17
    RET80 is offline Member
    Join Date
    Jan 2012
    Posts
    10
    Rep Power
    0

    Default Re: Using Java AWT and Canvas to draw an array of circles (big headache)

    Quote Originally Posted by Fubarable View Post
    No, the addBalls doesn't draw anything. It just add's Ball2D's to the List. It only draws if you add your drawing canvas to the Frame. The Frame now should use its default BorderLayout and you shouldn't add anything else to the Frame.
    Ah-ha! I got it to work! Thank you SO much Fubarable for your time and patience! You rock! :)
    I'm more of a C/C++ guy, java isn't my strength as of yet, but I'm working on it.

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  2. Object Draw canvas Text
    By kyle.g in forum New To Java
    Replies: 0
    Last Post: 07-27-2010, 09:54 PM
  3. Replies: 2
    Last Post: 06-01-2010, 09:37 AM
  4. Draw circles, select circles
    By cselic in forum Java 2D
    Replies: 2
    Last Post: 05-17-2010, 02:02 PM
  5. Problems With Using Canvas to draw On...,
    By Mpisha in forum NetBeans
    Replies: 1
    Last Post: 10-14-2008, 05:27 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
  •