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

    Default how to paint a circle or rectangle in a specify tab of the jtabbedpane ?

    hi i have an application with 5 jtextpanes which is an array

    i would like to know how to be able to draw a blue circle
    in the 1st jtextpane tp[0]

    in the 2nd jtextpane i would like to draw a red circle... tp[1]....

    i have a code that can help us to resolve this problem

    but right now with that, im painting all the same shapes
    in all the tabs



    ty for the help

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextPane;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JScrollPane;
    import javax.swing.UIManager;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Color;
    import java.*;
    import javax.*;
    import java.awt.Graphics;
    public class Mains extends JFrame 
    {
    	private JTabbedPane             jtp        	= new JTabbedPane();
    	//private Transpa                 tp[]        = new Transpa[5];
    	private JTextPane               tp[]        = new JTextPane[5];
    	private JTextPane               textPane;
    	
    	private int                     i = 0;
    	private int  					index = 0;
    	
        public static void main( String[] argv ) 
        {
        	Mains  m = new Mains(); 
        }
    
        
        public Mains() 
        {
        	super("Editor Text Center");
        	createDocument();
    
               
            JMenuBar menuBar = new JMenuBar();
            setJMenuBar (menuBar);
    
            JMenu file = new JMenu ("File");
            JMenuItem newItemFile = new JMenuItem ("New");
            newItemFile.addActionListener (new ActionListener() 
            {
                public void actionPerformed (ActionEvent e) 
                {
                	createDocument();
                }
            });  
            JMenuItem ItemFile = new JMenuItem ("test");
            ItemFile.addActionListener (new ActionListener() 
            {
                public void actionPerformed (ActionEvent e) 
                {
                	int index = jtp.getSelectedIndex();
                	JTextPane jTPane = tp[index];
                 	System.out.println(jTPane.getText());
     
                }
            }); 
            file.add(newItemFile);
            file.add(ItemFile);   
            menuBar.add(file);   
            
    
            setSize(990,735);
            pack();
            setVisible(true);    
       }
      
     
        
       public void createDocument()
       {   
            tp[i] = new JTextPane(); 
            
            JTextPane t = tp[i];
            t.setText("a"+(i+1));
            JScrollPane editorScroll = new JScrollPane(tp[i]);
     		jtp.add("doc"+(i+1), editorScroll);	
     	    getContentPane().add(jtp,BorderLayout.CENTER);
     	    i++;
       }  
    }

    Java Code:
    import javax.swing.JTabbedPane;
    import javax.swing.JTextPane;
    import java.awt.Color;
    import java.awt.Graphics;
    
    import java.util.Random; 
    
    
    public class Transpa extends JTextPane
    {
    	JTabbedPane jtp;
    	
    	public Transpa(JTabbedPane jtp)
    	{
    		this.jtp = jtp;
    		//this.setOpaque(false);
    		this.setBackground(Color.WHITE);
    	}
    		
    	@Override	
    	public void paintComponent(Graphics g)
    	{
    		
    		super.paintComponent(g);
    		Random r = new Random();
    		Color  c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
    		g.setColor(c);
    		
             
    		
    	    //g.fillRect(0,0,getWidth(),getHeight());
    		g.fillRect(0,0,50,50);
    	    g.drawOval(50,50,30,40);
    	    super.repaint();
    	}
    }

    some code or advice would be very appreciate it

    do i have to do a for to check all the tabs and inside the for do if (tp[i]==... ) ? in the paint component ?
    Last edited by nicnicnic; 11-22-2009 at 07:40 AM.

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

    Default

    First of all custom painting is done by extending a JPanel (or JComponent), not a JTextPane.

    If you want different color circles, then you need to pass in the Color as a parameter when you create the custom JPanel.

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

    Default

    If he has a strong reason to paint in the JTextPane, then I don't readily see a contraindication for doing this, but would gladly entertain any thoughts of yours on this, Rob.

    Something like so (if you wanted random colors)
    Java Code:
    class Transpa extends JTextPane {
       JTabbedPane jtp;
       private Random random = new Random();
       private Color c = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    
       public Transpa(JTabbedPane jtp) {
          this.jtp = jtp;
          this.setBackground(Color.WHITE);
       }
    
       @Override
       public void paintComponent(Graphics g) {
    
          super.paintComponent(g);
          g.setColor(c);
    
          g.fillRect(0, 0, 50, 50);
          g.drawOval(50, 50, 30, 40);
          //super.repaint();  ?????
       }
    }

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

    Default

    If he has a strong reason to paint in the JTextPane, then I don't readily see a contraindication for doing this,
    If you really did need to do custom painting of random shapes on top of text in the text pane, then you would do it the way you suggested.

Similar Threads

  1. problem with adding circle and rectangle
    By nicnicnic in forum Java 2D
    Replies: 3
    Last Post: 11-16-2009, 07:13 PM
  2. how to add a circle or rectangle in a jtextarea
    By nicnicnic in forum Java 2D
    Replies: 10
    Last Post: 10-17-2009, 05:09 PM
  3. drawing a dashed circle
    By Alarmmy in forum SWT / JFace
    Replies: 0
    Last Post: 07-13-2009, 10:46 AM
  4. How to write numbers around a circle
    By pheonix in forum New To Java
    Replies: 8
    Last Post: 06-11-2009, 11:20 AM
  5. In which circle is the Point lying?
    By nidhirastogi in forum New To Java
    Replies: 1
    Last Post: 07-03-2008, 12:12 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
  •