Results 1 to 17 of 17
- 01-29-2012, 11:50 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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
This is my circle drawing class: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; } }
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.
-
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
- 01-29-2012, 11:59 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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?
- 01-30-2012, 12:06 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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.
-
Re: Using Java AWT and Canvas to draw an array of circles (big headache)
- 01-30-2012, 12:13 AM #6
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: Using Java AWT and Canvas to draw an array of circles (big headache)
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.
-
Re: Using Java AWT and Canvas to draw an array of circles (big headache)
Set the layout on the Frame itself.
- 01-30-2012, 12:17 AM #8
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
-
Re: Using Java AWT and Canvas to draw an array of circles (big headache)
- 01-30-2012, 12:35 AM #10
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
- 01-30-2012, 12:37 AM #11
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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.
-
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....
- 01-30-2012, 12:44 AM #13
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Re: Using Java AWT and Canvas to draw an array of circles (big headache)
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...
-
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):
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 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 you could add this to your frame: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); } }
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]); //!! }
- 01-30-2012, 04:42 AM #15
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
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...
-
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.
- 01-30-2012, 05:36 AM #17
Member
- Join Date
- Jan 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How to import image, draw circles/text on it, and save a new image to disk
By InTheEndo in forum Java 2DReplies: 1Last Post: 07-28-2011, 08:48 AM -
Object Draw canvas Text
By kyle.g in forum New To JavaReplies: 0Last Post: 07-27-2010, 09:54 PM -
To draw circles on JPanel using JComboBox to determine number of them
By mneskovic in forum New To JavaReplies: 2Last Post: 06-01-2010, 09:37 AM -
Draw circles, select circles
By cselic in forum Java 2DReplies: 2Last Post: 05-17-2010, 02:02 PM -
Problems With Using Canvas to draw On...,
By Mpisha in forum NetBeansReplies: 1Last Post: 10-14-2008, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks