Results 1 to 11 of 11
  1. #1
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default how to add a circle or rectangle in a jtextarea

    hi im new here

    and i have try to add by myself a circle on a jtextarea which doesnt work atm

    im new to graphics2d but not to java...

    the code i have atm doesnt work out when i try to add my circle into a jtextarea but its works when i add it to the jframe
    what am i doing wrong ?

    Here is the Main class

    Java Code:
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    
    public class ShowCircle extends JFrame
    {
    	
          private Circle circle;
          private JTextArea jTA;
    	
          public ShowCircle()
          {
                 jTA = new JTextArea(6, 20);
    	
                 circle = new Circle(60,150,50,30);
                 jTA.add(circle);
                 //this.add(circle); works
        	getContentPane().add(jTA,BorderLayout.WEST);
                 this.setSize(100,100);
                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 this.setVisible(true);
          }
        
    	
          public static void main(String[] args)
          {
                 ShowCircle sc = new ShowCircle();
          }
    }
    and here is the Circle class

    Java Code:
    import java.awt.Graphics;
    import java.awt.geom.Ellipse2D;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class Circle extends JPanel
    {
    	
    	int x,y,width,height;
    	
    	
    	
    	public Circle()
    	{
    		x = 10;
    		y = 50;
    		width = 100;
    		height = 50;
    	}
    	
    	public Circle(int x,int y,int width, int height)
    	{
    		this.x = x;
    		this.y = y;
    		this.width  = width;
    		this.height = height;
    	
    		
    		  
    	}
    	
    	
    	public void paint(Graphics g)
    	{
    
    		Graphics2D g2 = (Graphics2D)g;
    
    		g.drawOval(x,y,width,height);
    	}
    }



    help me out plz im block

    ty in advance
    Last edited by nicnicnic; 10-14-2009 at 10:09 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    JTextAreas are for displaying text.

  3. #3
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    ok but what if i do it with jtextpane will it work ?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Textpane? Rather draw on JPanels instead?
    PS: It helps to go through the swing tutorial before trying to do 2d graphics.

  5. #5
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    Mod action: post with inappropriate language deleted
    Last edited by Fubarable; 10-17-2009 at 04:11 PM.

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

    Default

    Quote Originally Posted by nicnicnic View Post
    hi im new here
    welcome!

    and i have try to add by myself a circle on a jtextarea which doesnt work atm
    You can have any JComponent show graphics by overriding that component's paintComponent method and drawing in it. Have you given this a try?

  7. #7
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default

    ty fubarable for the tip im gonna try it:)

    i didnt know about the tip you just give me but im gonna try:)

    finally a constructive post !

  8. #8
    nicnicnic is offline Member
    Join Date
    Oct 2009
    Posts
    13
    Rep Power
    0

    Default nicnicnic

    ty fubarable its works !


    Java Code:
    textarea = new JTextArea(20, 40)
      {
    	    @Override
    	    protected void paintComponent(Graphics g)
    	    {
    	      super.paintComponent(g);
    	  
    	        g.drawOval(150, 50, 100, 100);
    	      
    	    }   
      };[

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    finally a constructive post !
    The suggestion to read the Swing 2D tutorial was a constructive post. If you actually do take the time to look at it you will see the demo code given is exactly the same as the code you ended up using. So don't be so quick to judge the advice you have been given.

    I also suggest you watch you language if you want future help. I'm actually surprised you got any help after those comments.
    Last edited by camickr; 10-15-2009 at 05:21 AM.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Abuse reported on post #5.

    db

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

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Abuse reported on post #5.

    db
    Ah, thanks. I did not see the language in post #5, and had I, my post above would have been quite different. The post will be deleted and the poster warned. Any similar infarction can have him banned from this forum.

Similar Threads

  1. Replies: 1
    Last Post: 07-15-2009, 02:30 PM
  2. drawing a dashed circle
    By Alarmmy in forum SWT / JFace
    Replies: 0
    Last Post: 07-13-2009, 09:46 AM
  3. How to write numbers around a circle
    By pheonix in forum New To Java
    Replies: 8
    Last Post: 06-11-2009, 10:20 AM
  4. Generate numbers around a circle?
    By pheonix in forum New To Java
    Replies: 4
    Last Post: 06-05-2009, 05:08 PM
  5. In which circle is the Point lying?
    By nidhirastogi in forum New To Java
    Replies: 1
    Last Post: 07-02-2008, 11:12 PM

Posting Permissions

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