Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2009, 07:35 AM
Member
 
Join Date: Oct 2009
Posts: 13
Rep Power: 0
nicnicnic is on a distinguished road
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

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++;
   }  
}

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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2009, 07:56 AM
Senior Member
 
Join Date: Jul 2009
Posts: 231
Rep Power: 1
camickr is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2009, 08:41 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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)
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();  ?????
   }
}
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2009, 09:08 AM
Senior Member
 
Join Date: Jul 2009
Posts: 231
Rep Power: 1
camickr is on a distinguished road
Default
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with adding circle and rectangle nicnicnic Java 2D 3 11-16-2009 07:13 PM
how to add a circle or rectangle in a jtextarea nicnicnic Java 2D 10 10-17-2009 05:09 PM
drawing a dashed circle Alarmmy SWT / JFace 0 07-13-2009 10:46 AM
How to write numbers around a circle pheonix New To Java 8 06-11-2009 11:20 AM
In which circle is the Point lying? nidhirastogi New To Java 1 07-03-2008 12:12 AM


All times are GMT +2. The time now is 07:03 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org