Results 1 to 7 of 7
  1. #1
    intrepid604 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default 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:
    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);
    	}
    }
    and here is the panel I'd like to put it into...
    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);
    	}
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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

  3. #3
    intrepid604 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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?

    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);
    	}
    }
    and my Balloon class:

    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);
    	}
    }
    Thanks!!

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

    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);
            }
        }
    }
    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.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

  6. #6
    intrepid604 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    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();
    	}
    }

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    myCanon.paint(g); ///// <---- is this correct? or do I need another Canvas object?
    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.

    Shouldn't I be trying to make stand alone classes, rather than inner classes so that I can more easily reuse them later?
    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.

    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

  1. Timer fire twice?
    By TechMax in forum New To Java
    Replies: 1
    Last Post: 02-03-2011, 01:22 PM
  2. Replies: 1
    Last Post: 01-21-2011, 11:46 AM
  3. SWT balloon Popup
    By prasannakum in forum SWT / JFace
    Replies: 0
    Last Post: 04-21-2010, 09:58 AM
  4. KeyAdapter won't fire
    By billq in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 02:37 AM
  5. balloon burst source code
    By satti in forum Java Applets
    Replies: 20
    Last Post: 04-21-2009, 05:22 PM

Tags for this Thread

Posting Permissions

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