Results 1 to 7 of 7
Thread: Making a canon fire at a balloon
- 02-15-2011, 09:36 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Making a canon fire at a balloon
I'm trying to make a Java Program with a canon which will fire at a balloon using swing.
I'm off to a rough start with my balloon class--I don't get how to make it draw on a panel from another class...
Here's the balloon:and here is the panel I'd like to put it into...Java Code:import java.applet.*; import java.awt.*; public class Balloon { int radius; int x; int y; public Balloon(int r, int x, int y) { this.radius = r; this.x = x; this.y = y; } public void paint(Graphics g) { g.drawOval(x, y, 10, 10); } }
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Q1625 extends JFrame { Balloon myBalloon = new Balloon(5, 12, 5); public Q1625() { JPanel upper = new JPanel(); upper.setLayout(new BorderLayout()); upper.add(myBalloon.paint()); // it doesn't let me do this! any ideas? } public static void main(String[] args) { Q1625 frame = new Q1625(); frame.setSize(400, 200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
- 02-16-2011, 02:05 PM #2
Yeah, first off, you have a JFrame and a JPanel, but you never added your JPanel to your JFrame. Also, you should use setPreferredSize() on the JPanel, add it to the JFrame, and then call pack() on the JFrame before displaying it.
Then, you need to override the paintComponent() method of the JPanel and from inside there, you can pass the graphics object to your balloon class.
I suggest you make a nested subclass of JPanel to do this. See what you come up with based on my suggestions, and we can help you further :D
- 02-16-2011, 08:47 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Hi Quad. thanks for the reply. I follow you in theory, but in terms of syntax I'm not sure how to override the paintComponent() method of the JPanel since the panel is a part of my class (not the class itself--which is a frame)..
This (see code below) is how I know how to override the paintComponent() method, but I think this won't work because it's trying to override it for the wrong class--are you suggesting that I write another class entirely?
and my Balloon class:Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Q1625 extends JFrame { Balloon myBalloon = new Balloon(5, 12, 5); public Q1625() { JPanel upper = new JPanel(); upper.setLayout(new BorderLayout()); add(upper); } public void paintComponent(Graphics g) { myBalloon.paint(g); } public static void main(String[] args) { Q1625 frame = new Q1625(); frame.setSize(400, 200); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
Thanks!!Java Code:import java.applet.*; import java.awt.*; public class Balloon { int radius; int x; int y; public Balloon(int r, int x, int y) { this.radius = r; this.x = x; this.y = y; } public void paint(Graphics g) { g.drawOval(x, y, 10, 10); } }
- 02-16-2011, 09:01 PM #4
Alright, so I'm gonna spoon you a little bit of code for the sake of keeping this thread from going on for miles.
So we need a JPanel to work with, but that would include a subclass of JPanel too! There are two ways to do this, anonymously and explicitly. I'll do it the second way for ease of reading.
This code is very very simplified, GUI elements should be invoked by a new thread, but this will get something to display. In order for the balloon to move, you will need a loop that changes its position and then calls the repaint() method on the JFrame.Java Code:public class CanvasDemo extends JFrame{ public static void main(String[] args) { new CanvasDemo(); } private TinkerToy tt; public CanvasDemo(){ tt = new TinkerToy(); //Make an instance of our 'canvas' Canvas canvas = new Canvas(); //set its size canvas.setPreferredSize(new Dimension(640, 480)); //add it to the JFrame (this class) this.add(canvas); //Shrink/grow the window to fit the panel this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); //now if you need to refresh the window along with //its contents, just repaint the window. All contained //components will refresh too this.repaint(); } //Private inner class, only this class can use it private class Canvas extends JPanel{ //override the paintComponent method to replace it with our //drawing code. In this case, TinkerToy draws itself, so //we simply pass the Graphics object to it @Override public void paintComponent(Graphics g){ //Always do this first!!!! super.paintComponent(g); //draw a toy tt.drawYourself(g); } } }
- 02-16-2011, 09:03 PM #5
P.S. there is actually a java Canvas class meant for drawing. As you get better at this, you can learn more advanced techniques like setting up a buffer strategy and drawing with the official Canvas class. In this case, we're making our own using a simple JPanel.
- 02-18-2011, 02:59 AM #6
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Hey, I really appreciate the time you took to help me with this...
I think I'm getting closer to understanding, but it still looks a little weird to me.. Shouldn't I be trying to make stand alone classes, rather than inner classes so that I can more easily reuse them later?
Here's what I've got so far based on your inner class suggestion. It works!! So now I want to add a Canon object. Do I do it as I've indicated below, or do I need to create a new panel to do this?
Thanks!
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Q1625 extends JFrame { Balloon myBalloon = new Balloon(25, 324, 5); Canon myCanon = new Canon(); public Q1625() { Canvas c = new Canvas(); c.setLayout(new BorderLayout()); c.setPreferredSize(new Dimension(640, 480)); this.add(c); this.pack(); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.add(c); this.repaint(); } private class Canvas extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); myBalloon.paint(g); myCanon.paint(g); ///// <---- is this correct? or do I need another Canvas object? } } public static void main(String[] args) { new Q1625(); } }
- 02-18-2011, 02:55 PM #7
Yeah, thats correct. You only need 1 canvas to paint on. You can paint directly on it with the graphics object, or you can pass the graphics object to another class so it can use it. No matter where you send it, the graphics object will draw on the canvas its from.myCanon.paint(g); ///// <---- is this correct? or do I need another Canvas object?
Sure! I did it this way only for simplicity of example. You can make the canvas class public and stand alone, and then write accessor methods to pass the objects in and out.Shouldn't I be trying to make stand alone classes, rather than inner classes so that I can more easily reuse them later?
One last note - in my example I forgot to mention: call g.dispose() at the end of the paintComponent() method (right after myCanon.paint()) - that frees up its resources when you're done painting.
Similar Threads
-
Timer fire twice?
By TechMax in forum New To JavaReplies: 1Last Post: 02-03-2011, 01:22 PM -
Plzz Help...Issue when doing Text Printing from Java to Canon iR1018, iR1020
By rameshZSL in forum Advanced JavaReplies: 1Last Post: 01-21-2011, 11:46 AM -
SWT balloon Popup
By prasannakum in forum SWT / JFaceReplies: 0Last Post: 04-21-2010, 09:58 AM -
KeyAdapter won't fire
By billq in forum New To JavaReplies: 5Last Post: 04-14-2010, 02:37 AM -
balloon burst source code
By satti in forum Java AppletsReplies: 20Last Post: 04-21-2009, 05:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks