Results 1 to 9 of 9
- 10-29-2009, 08:36 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
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
Thankz for the helpJava 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); } }Last edited by nicnicnic; 10-29-2009 at 10:31 AM.
- 10-29-2009, 08:38 AM #2
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
- 10-29-2009, 10:23 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
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 replyLast edited by nicnicnic; 10-29-2009 at 10:32 AM.
- 10-29-2009, 10:24 AM #4
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
- 10-29-2009, 10:36 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
is it that how your supposed to do it ?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); } }
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 10:42 AM.
- 10-29-2009, 10:49 AM #6
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
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
- 10-29-2009, 10:49 AM #7
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
- 10-29-2009, 10:51 AM #8
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 10-29-2009, 10:54 AM #9
Member
- Join Date
- Oct 2009
- Posts
- 13
- Rep Power
- 0
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
-
my code doesn't change background color
By javanoobita in forum New To JavaReplies: 5Last Post: 02-22-2009, 04:30 AM -
Making your own font/foreground-background color?
By Durnus in forum Java 2DReplies: 6Last Post: 01-02-2009, 09:36 PM -
[SOLVED] how to cahnge the background color of the frame?
By pranav13 in forum AWT / SwingReplies: 13Last Post: 10-21-2008, 02:32 PM -
JButton onClick change color background
By behrk2 in forum AWT / SwingReplies: 6Last Post: 07-09-2008, 04:54 PM -
window background color?
By javan00b in forum New To JavaReplies: 3Last Post: 01-29-2008, 10:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks