Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By jim829

Thread: Draw in Jframe without extends

  1. #1
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Draw in Jframe without extends

    Hello guys,

    It's possible to draw a ball in JFrame without the ball class extends JFrame?

    I have to make it a JComponent?

    The code I have for the ball class is this:

    Java Code:
    public class Ball implements Runnable{
    
    	public Ball(){
    			}
    
    	public void draw(Graphics x){
    		Random random = new Random();
    		x.setColor(new Color(
    				random.nextInt(200), random.nextInt(200), random.nextInt(255)));
    		x.fillOval(50, 50, 50,50);
    	}
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    
    	}

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: Draw in Jframe without extends

    You can but you shouldn't. You should draw in JPanel which is added to an instance of JFrame. There is nothing wrong with extending JPanel. But if you don't want to, you can override paintComponent using anonymous class syntax when you create your JPanel instance.

    In any event your code won't work as written. Among other things, what calls the draw method and from where does the Graphics argument come from?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Draw in Jframe without extends

    I also have tha Layout class like this:

    Java Code:
    public class Gui {
    
    	
    	private JFrame frame;
    	
    	public Gui(){
    		frame = new JFrame();
    	}
    
    	public void init(){
    		setUp();		
    	}
    
    	private void setUp(){
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500,500);
    		frame.add(new JButton("Start"), BorderLayout.NORTH);
    		frame.add(new JButton("Exit"), BorderLayout.SOUTH);
    				
    	}
    What I'am trying to do is: have multiple balls that will be associated to a thread ( I'am learning about pool's), and the balls will have to make random moves arround the jframe. But first I need to draw them on JFrame...Because the balls will implement runnable, I don't want them to extend JFrame or JPanel..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Draw in Jframe without extends

    The balls would not extend a component, they would be drawn by the paintComponent() method in the class that extends JPanel.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Draw in Jframe without extends

    Quote Originally Posted by Norm View Post
    The balls would not extend a component, they would be drawn by the paintComponent() method in the class that extends JPanel.
    So I should do something like BallsWindow extends JPanel or JComponent, and make the Balls class.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: Draw in Jframe without extends

    Yes. Then you can iterate over an array of Ball instances, invoking the draw method with the Graphics instance passed to the paintComponent method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Draw in Jframe without extends

    Quote Originally Posted by jim829 View Post
    Yes. Then you can iterate over an array of Ball instances, invoking the draw method with the Graphics instance passed to the paintComponent method.

    Regards,
    Jim
    Ok, i get it and I already can draw it. But now i have another problem, each ball I draw should be part of a Ball class instance, because I will have to run a Ball as thread to make it move around the JFrame. As I'am doing I can't because I'm drawing a ball when I call a new WindowBall..

    (sorry i'm new at this )

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Draw in Jframe without extends

    If you have a list of Ball instances, the code in the drawing method can go through the list of Ball objects and call each one to have it draw itself.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Draw in Jframe without extends

    Java Code:
    public class WindowBall extends JPanel{
    
    	private int cord = 0;
    
    	@Override
    	public void paintComponent(Graphics x) {
    		int r= (int)Math.round((Math.random()*255));
    		int g= (int)Math.round((Math.random()*255));
    		int b= (int)Math.round((Math.random()*255));
    
    		x.setColor(new Color(r,g,b));
    		x.fillOval(cord, 50, 10,10);
    	}

    Java Code:
    public class Ball {
    
    	ArrayList<Ball> ball_array = new ArrayList<Ball>();
    
    	public Ball(){
    
    	}
    
    	public void addBall(Ball x){
    		ball_array.add(x);
    	}
    	
    	public void move(){
    		// method to repaint position
    	}
    Java Code:
    public class Gui {
    
    
    	private JFrame frame;
    	private WindowBall ball_window = new WindowBall();
    
    	public Gui(){
    		frame = new JFrame();
    	}
    
    	public void init(){
    		setUp();		
    	}
    
    	private void setUp(){
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500,500);
    		frame.add(new JButton("start"), BorderLayout.NORTH);
    		frame.add(new JButton("Exit"), BorderLayout.SOUTH);
    		frame.add(ball_window , BorderLayout.CENTER);
    	}
    
    
    
    }

    Now i will have to associate each ball instance to each ball drawed in WindowBall

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Draw in Jframe without extends

    The code that does the drawing for a Ball could be in the Ball class, not the WindowBall class. The paintComponent() method would go through the list of Ball objects and call each one for it to draw itself.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,804
    Rep Power
    5

    Default Re: Draw in Jframe without extends

    And although what I am about to suggest is not needed for your application it will make it more versatile and can be a learning experience. Create an interface like the following:

    interface DrawObject {
    public void draw(Graphics g);
    }

    This can now be used to group together instances of anything that implements the interface so you can iterate over a common list of differing shapes. Interfaces are very powerful and worth learning about.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    tcms is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: Draw in Jframe without extends

    Oh, thanks to both, I already made it work. Now is time for the hard part, threads !

Similar Threads

  1. Replies: 5
    Last Post: 07-24-2012, 10:35 AM
  2. Disposing JFrame while using extends
    By SteJ in forum New To Java
    Replies: 4
    Last Post: 04-21-2012, 12:57 AM
  3. Replies: 6
    Last Post: 04-27-2011, 06:27 AM
  4. Class that extends JFrame help
    By javaman1 in forum New To Java
    Replies: 5
    Last Post: 11-10-2010, 03:29 AM
  5. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 04:43 AM

Posting Permissions

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