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

    Default why isnt my background color displaying correctly ?

    what i wanna do with my program is adding circle and rectangle into my jtextpane

    that is working

    but at the moment, it doesnt show the white background color for my jtextpane

    whats wrong ?

    Main class

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    import javax.swing.JMenuBar;
    import javax.swing.JMenu;
    import javax.swing.JMenuItem;
    import javax.swing.JScrollPane;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Mains extends JFrame 
    {
    	private JTabbedPane             jtp        	= new JTabbedPane();
    	private Transpa                 tp[]        = new Transpa[5];
    	   
    	private int                     i = 0;
    
    	
        public static void main( String[] argv ) 
        {
        	Mains  m = new Mains();     
        }
    
        
        public Mains() 
        {
    
        	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();
                }
            });
                   
            file.add(newItemFile);    
            menuBar.add(file);   
            
    
            setSize(990,735);
            pack();
            setVisible(true);    
       }
      
        
        
       public void createDocument()
       {   
    	    tp[i] = new Transpa();  
    	    
    
            JScrollPane editorScroll = new JScrollPane(tp[i]);
         
     		jtp.add("doc"+(i+1), editorScroll);	
     	    getContentPane().add(jtp,BorderLayout.CENTER);
     	    i++;
       }  
    }

    Transpa class
    Java Code:
    import javax.swing.JTextPane;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    import java.util.Random; 
    
    
    public class Transpa extends JTextPane
    {
    	
    	public Transpa()
    	{
    		this.setBackground(Color.WHITE);
    		this.setOpaque(false);
    	}
    		
    		
    	public void paintComponent(Graphics 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,50,50);
    	    g.drawOval(50,50,30,40);
    	    super.paintComponent(g);
    	}
    }
    Thankz for the help
    Last edited by nicnicnic; 10-29-2009 at 11:31 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    How do you expect to see the background color after this:
    Java Code:
    this.setOpaque(false);
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    ok but if i dont add that line

    this.setOpaque(false);


    it wont show the circle and rectangle

    so what im doing wrong ?

    im new to painting 2d

    and im trying to find the solution

    ty for the reply
    Last edited by nicnicnic; 10-29-2009 at 11:32 AM.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I'd try to put super.paintComponent(g); as the first line of your overridden method. Just a guess.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    Java Code:
    import javax.swing.JTextPane;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    import java.util.Random; 
    
    
    public class Transpa extends JTextPane
    {
    	
    	public Transpa()
    	{
    		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,50,50);
    	    g.drawOval(50,50,30,40);
    	}
    }
    is it that how your supposed to do it ?

    i try that way but it didnt help


    but ty for the reply again and trying to help me out


    its so a weird " bug " if you can say it that way

    p.s. maybe i need to put the repaint(); function into it ? i will test it

    The repaint() didnt workout well too.
    Last edited by nicnicnic; 10-29-2009 at 11:42 AM.

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

    Default

    import javax.swing.JTextPane;

    import java.awt.Color;
    import java.awt.Graphics;

    import java.util.Random;


    public class Transpa extends JTextPane
    {

    public Transpa()
    {
    //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();
    }
    }



    thats work :D

    ty for the help PhHein

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I don't know what you expect but this looks OK to me:
    Java Code:
    public class Transpa extends JTextPane
    {
    	
    	public Transpa()
    	{
    		this.setBackground(Color.WHITE);
    		this.setOpaque(true);
    	}
    		
    		
    	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,50,50);
    	    g.drawOval(50,50,30,40);
    	   
    	}
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by nicnicnic View Post
    thats work :D

    ty for the help PhHein
    Ah, so you got it :)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

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

    Default

    ya:) thank you alot:)

    im not new to java but to java2d yes so sometimes im doing noob things hehe

    btw if you wanna have a laugh

    copy and paste this code and try it lol i laugh hard the first time i did this

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    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;
    public class Mains extends JFrame 
    {
    	private JTabbedPane             jtp        	= new JTabbedPane();
    	private Transpa                 tp[]        = new Transpa[5];
    	   
    	private int                     i = 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();
                }
            });              
            file.add(newItemFile);    
            menuBar.add(file);   
            
    
            setSize(990,735);
            pack();
            setVisible(true);    
       }
      
        
        
       public void createDocument()
       {   
    	    tp[i] = new Transpa();  
            JScrollPane editorScroll = new JScrollPane(tp[i]);
     		jtp.add("doc"+(i+1), editorScroll);	
     	    getContentPane().add(jtp,BorderLayout.CENTER);
     	    i++;
       }  
    }

    Java Code:
    import javax.swing.JTextPane;
    
    import java.awt.Color;
    import java.awt.Graphics;
    
    import java.util.Random; 
    
    
    public class Transpa extends JTextPane
    {
    	
    	public Transpa()
    	{
    		//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();
    	}
    }

    LOLLLLLLLLLL have fun:)

Similar Threads

  1. my code doesn't change background color
    By javanoobita in forum New To Java
    Replies: 5
    Last Post: 02-22-2009, 05:30 AM
  2. Replies: 6
    Last Post: 01-02-2009, 10:36 PM
  3. Replies: 13
    Last Post: 10-21-2008, 03:32 PM
  4. JButton onClick change color background
    By behrk2 in forum AWT / Swing
    Replies: 6
    Last Post: 07-09-2008, 05:54 PM
  5. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 11: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
  •