ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
nope.
My problem: I'm writing a hangman program as a learning program. It's really complicated. grr.
I have multiple panels... I tried to get the background colour to be orange.. it flashed orange on repaint, and went grey.
never mind. problem I'll be able to work out as a challenge.
What i want is like: red buttons. red combo boxes.
hi there
according to me the foll peice of code should work
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class OneButton extends Applet {
public void init(){ Button testBut = new Button("BUTTON");
testBut.setBackground(Color.blue);
testBut.setForeground(Color.red);
add(testBut); } }
i haven't tested it yet
still better u culd use
create an image icon object say penguin
now just use
new Button(penguin)
this should wrk
fine
what you do is subclass the components you want to change the color of. You can also do cool things like make them circles or whatever, add gradients, etc. Here is the code I used for custom text fields (this is assuming you are using Swing/JFC.. IE JButton, JTextArea, etc.). It makes them orange, and with round corners.
Code:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
public class KTextField extends JTextField {
private Paint main = new Color(0xff,0xa5,0x00); // orange
private Paint hilite = new Color(0xff,0xff,0xff); // white
private Paint lolite = new Color(0xa0,0x64,0x00); // dark orange
public KTextField(int cols) {
super(cols);
init();
}
public KTextField(int cols, Paint color) {
super(cols);
this.main = color;
init();
}
private void init() {
setOpaque(false); // if true it paints a regular btn on top of what we do
setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
}
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
int width = getWidth();
int height = getHeight();
g2d.setColor(lolite);
g.fillRoundRect(0, 0, width-1, height-4, height, height-4);
g2d.setColor(hilite);
g.fillRoundRect(1, 4, width, height-4, height, height-4);
g2d.setPaint(main);
g.fillRoundRect(1, 2, width-2, height-4, height, height-4);
super.paintComponent(g);
}
}
This formula can be applied to most swing components, but the more complex ones will give you trouble. Another thing that's worth looking into is creating a custom MetalTheme (which I also have example code for) which globally replaces colors in the default Swing Theme.
In order to set colors for swing components globally without subclassing each one, there's a class javax.swing.UIManager , which all swing components use for their icons, fonts and colors (in the Metal theme).
And make sure you set all of the colors you want to change BEFORE you create the objects (a static initializer of the main class is a good place) or else you'll have to call
which slows things down. Also, the previous example I posted does use a fair bit of CPU if you have a lot of KTextField's on screen, because when you call setOpaque(false); you force Swing to traverse the entire component hierarchy to fill in every pixel you don't (it's a speed hack that you have to set it true or false at all). If you don't want round buttons or text fields, then I'd suggest setting UIManager defaults to just change the color of stuff.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.