Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31
  1. #21
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default awt canvas is a component

    you can stick an awt canvas inside a jpanel i've done it and then you can use the jpanel to repaint

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

    Default

    Quote Originally Posted by aaroncarpet View Post
    you can stick an awt canvas inside a jpanel i've done it and then you can use the jpanel to repaint
    Just that this can be done does not mean that it is good coding technique. In fact it is very bad coding technique and should only be done in dire need. Do you know what happens if you do this and then try to show a pop-up menu above the Canvas? Also, are you familiar with the effects that this will have on Swing's default double-buffering behavior?

    The accepted technique for drawing in Swing components is to subclass a JPanel or a JComponent, and there are many compelling reasons that this is so. And for this reason, this is the advice that we should be giving to posters in this forum. If you are not familiar with the adverse effects of the advice you are giving, then perhaps you shouldn't be giving this advice. Otherwise you will mislead the OPs who really don't know any better and can't distinguish good advice from bad.
    Last edited by Fubarable; 11-15-2009 at 07:19 AM.

  3. #23
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post

    You may wish to post your exact assignment instructions to help us better understand your needs and instructor-demanded limitations.

    Much luck!

    Here's the exact assignment question

    I have to implement inheritance in a program to draw various shapes. The x,y co ordinates together with height and width are given as input. Inheritance should be implemented in the following way.
    Define a class Shape.
    The Rectangle class inherits from the Shape class.
    Square Class inherits from the Rectangle.
    Similarly for Oval and Circle class.


    I tried implementing by using functions and not constructors; now the program doesnt get stuck while executing but nothing is drawn once the button is pressed.
    Your Shape class (again, the first letter of a class should be capitalized) should just have methods that are shared by all objects that subclass Shape. Period.

    Your events, buttons, and all that should go in your GUI class, call it ShapeGUI if you desire.
    The only functions to be used in subclasses is paint so isnt it necessary that the base class Shape should extend the JFrame?
    Last edited by primalpop; 11-15-2009 at 08:30 AM.

  4. #24
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    To give you a hint, one way to do this is like so:

    Java Code:
    // best would be to make this class abstract
    class MyShape {
       protected int x, y, width, height;
       
       // and to make this abstract
       public void paint(Graphics g) {
          
       };
       
       public MyShape(int x, int y, int width, int height) {
          this.x = x;
          this.y = y;
          this.width = width;
          this.height = height;
       }
    }
    
    class MyRectangle extends MyShape {
       
       public MyRectangle(int x, int y, int width, int height) {
          super(x, y, width, height);
       }
    
       public void paint(Graphics g) {
          g.setColor(Color.blue);
          g.drawRect(x, y, width, height);
       }
    }
    and a drawing panel could look like so:
    Java Code:
    class MyDrawingPanel extends JPanel {
       private MyShape myShape;
       
       public void setMyShape(MyShape myShape) {
          this.myShape = myShape;
       }
       
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (myShape != null) {
             myShape.paint(g);
          }
       }
    }

  5. #25
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Java Code:
    class MyDrawingPanel extends JPanel {
       private MyShape myShape;
       
       public void setMyShape(MyShape myShape) {
          this.myShape = myShape;
       }
       
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (myShape != null) {
             myShape.paint(g);
          }
       }
    }
    Thanks for the code. It was a great help. But I dont understand this part of the code. What is this supposed to do and why is it necessary?

  6. #26
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by primalpop View Post
    Thanks for the code. It was a great help. But I dont understand this part of the code. What is this supposed to do and why is it necessary?

    That class creates a JPanel that will draw the Shape (or in my program the MyShape) derived objects. The DrawPanel object will be placed in some main jpanel using BorderLayout and at the CENTER position. If you feed it MyShape objects via setMyShape(....) and the call repaint on it, it will show the selected shape.

  7. #27
    primalpop's Avatar
    primalpop is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Sorry if I am being adamant. But I cant understand the need for this extra level of abstraction in your program.

    I was able to draw various shapes using the paint method directly in a program(same as in the assignment but without inheritance). Whats the necessity of paintComponent() method if paint() does the job?

    While invoking the paint() function from MyDrawingPanel class with the object of the MyShape class isnt it the paint function of the same class that gets invoked(but it has no function body :-/) ?

  8. #28
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by primalpop View Post
    Sorry if I am being adamant. But I cant understand the need for this extra level of abstraction in your program.

    I was able to draw various shapes using the paint method directly in a program(same as in the assignment but without inheritance). Whats the necessity of paintComponent() method if paint() does the job?
    A very good question and one I feel is best answered in this article:
    Painting in AWT and Swing
    In particular please look at the the Painting in Swing subsection where it lists the advantages to drawing in a Swing component as opposed to a JFrame (painting directly in the JFrame is more like painting in AWT than in Swing).

    While invoking the paint() function from MyDrawingPanel class with the object of the MyShape class isnt it the paint function of the same class that gets invoked(but it has no function body :-/) ?
    I'm sorry, but I'm not clear on what you are asking here.

  9. #29
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default Apologies First

    I had to go to my java book last night...You are absolutely right
    their example code uses a JPanel to paint the graphics on ...I am Most likely a beginner even though I have only been working with java for 4 years not as a career...I'm thinking about posting the example code here...except here they divide it into several small classes which is the java way small modules...

    I sincerely apologize for my ignorant response

  10. #30
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for the post and the correction. Sorry if I was hard on you, but I fear for the newbies who don't know any better. Cheers and happy coding!

  11. #31
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    Primal pop here is a sample that works that should help you ubderstand the logical problems you are having

    Java Code:
                  
    
     import java.awt.*;
     import iava.awt.geom.*;
     import javax. swing.*;
    
     public class FillTest
    
    {
           public static void main(String[] args)
           {
        FillFrame frame = new FillFrame();
           frame. setDefaultCloseOperation( JFrame.EXIT_ON_CL05E) ;
               frame.setVisible(true);
            }
       }
    
      /*
      A frame that contains a panel with drawings
       */
     class FillFrame extends JFrame
       {
    	Public FillFrame()	
    	{			
    		setTitle("FillTest");	
    	setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    				
    	// add panel to frame			
    				
    		FillPanel panel = new FillPanel();	
    		add(panel)	;	
    	}			
    				
    	public static final int	DEFAULT_WIDTH = 400;
    	public static final int	DEFAULT_HEIGHT = 400;
    	}			
    				
    	/*			
    	A	panel that	displays filled rectangles and ellipses	
       */
     class FillPanel extends JPanel
       {
             public void paintComponent(Craphics g)
             {
                   super. paintComponent(g);
                   Graphics2D g2 = (Craphics2D) g;
    
                   // draw a rectangle
    
                   double leftX = 100;
                   double topY = 100;
                   double width = 200;                
                      double height = 150;
    
                   Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
                   g2.setPaint(Color.RED);
                   g2.fill(rect);
    
    // draw the enclosed ellipse
    
    Ellipse2D ellipse = new Ellipse2D.Double();
    ellipse.setFrame(rect);
    g2.setPaint(new Color(0,128,128)); //dull blue green
    g2.fill(elipse);
    }
    }

    they use graphics2d here which may be easier in swing

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Problem In Swing Components
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2008, 11:23 PM
  2. Swing Components Placing
    By ne2000 in forum Eclipse
    Replies: 2
    Last Post: 06-23-2008, 09:00 AM
  3. How to print Swing components
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. Tab order on swing components
    By ashvin@projectdemo.biz in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2008, 11:06 AM
  5. HTML on Swing Components
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-27-2007, 10:51 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
  •