-
Form backcolor and picture ?
Hey;
I need to change the form background color and picture , the first "from proprietes" changes but it doesn't dispaly any results , and the second -putting a picture- doesn't exists in proprietes so how to put it and how to change colores ? thanks !
p.s. I'm using netbeans 5.5 .
-
Use the method setBackground() of the container.
-
what is the color constants that I could assign ? and what about pictures ?
Thanks .
-
You can assign any if the static colors in the Color class. You can also create your own color mixes by using the rgb constructor for a Color object, like;
public final static Color myColor=new Color(155,100,120);
If you want to use pictures you will have to override the
paintComponent(Graphics g);
method of the JComponent class and use the methods defined for the Graphics class to draw your picture.
eschew obfuscation
-
Thanks sjalle for reply.
But it doesn't changes the backcolor , I think I must make some thing n the form like enabling graphics or any thing similar, but I odn't know how.. as said before I use netbeans and it has in the form propieties the backcolor member and changing it doesn't make effects too .
thanks for any help here .
-
A general approach
I am not familiar w. netbeans so I cannot offer any help there. However, customizing (e.g.) the JPanel class is very simple:
Code:
package devx6;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
* Setting background color using three textfield for rgb values
* and a button for activating the specified color
*/
public class CustomPanel extends JPanel implements ActionListener {
JButton btn=new JButton("Set Color"); // sets color
JTextField tFR=new JTextField(); // red value field
JTextField tFG=new JTextField(); // green value field
JTextField tFB=new JTextField(); // blue value field
Color bgColor=Color.blue; // blue at startup
public CustomPanel() {
super.setLayout(null); // no layout manager
// use absolute positioning for components
btn.setBounds(30,30,100,22);
tFR.setBounds(30,54,30,22);
tFG.setBounds(64,54,30,22);
tFB.setBounds(97,54,30,22);
add(btn);
add(tFR);
add(tFG);
add(tFB);
// hook up the button
btn.addActionListener(this);
}
/**
* handle button click
* (does not check for negative values..)
* @param e
*/
public void actionPerformed(ActionEvent e) {
try {
int red = Integer.parseInt(tFR.getText().trim());
int green = Integer.parseInt(tFG.getText().trim());
int blue = Integer.parseInt(tFB.getText().trim());
if (red > 255 || green > 255 || blue > 255) {
JOptionPane.showMessageDialog(this,"RGB values are in range 0->255",
"User error",
JOptionPane.ERROR_MESSAGE);
return;
}
// color values ok, set new color and repaint
bgColor=new Color(red,green,blue);
repaint();
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this,"Invalid numeric entry",
"Uesr error",
JOptionPane.ERROR_MESSAGE);
}
}
// this is the CLUE ! Overrides the paintComponent method
// of the JComponent class (and also the descendant
// JPanel class)
public void paintComponent(Graphics g) {
Color c=g.getColor(); // save default color
g.setColor(bgColor);
g.fillRect(0,0,getWidth(),getHeight()); // fill entire panel w. the color
g.setColor(c); // restore default color
}
/**
* main for testing
* @param args
*/
public static void main(String[] args) {
CustomPanel cp = new CustomPanel();
JFrame f=new JFrame();
f.getContentPane().setLayout(new GridLayout());
f.getContentPane().add(cp);
f.setBounds(10,10, 400,400);
f.setVisible(true);
}
}
Last edited by sjalle; 02-01-2007 at 09:00 PM.
eschew obfuscation
-
Thanks sijale for the code, but still don't want to paint any color .
I think as said before it should be something to enable, like graphicale interface = true , or jform graphical = true or some thing similar ...
Anybody use netbeanes tell me what should I do ??
I foud this originally in the IDE generated code :
setBackground(new java.awt.Color(255, 102, 51)); //that because I have changed the default value from the form propietes .
and the rest isn't any thing starnge , all is about creating components and setting their boundry and location and all those routines ..
btw, sijale what do u use like IDE ?
thanks.
-
I use JBuilder, the ultimate tool for java development 
In JBuilder, if I want to make a panel, say, blue, I switch to design mode, select the panel and selects the "foreground" option from the left side of the IDE. All that happends when I do this is that JBuilder inserts one line of code in the GUI-setup part of my code.
eschew obfuscation
-
mmm, before I'm was using the Jbuilder 4 (2 or 3 years ago) , but I think the Netbeans 5.5 that I use now is better .
anyway thanks sjalle very much for help; I found the solution :
import java.awt.Color;
import java.awt.Container;
//---
Container c = this.getContentPane();
c.setBackground(Color.orange);
now I need to know how to paint the background with a picture ?
Thanks .
-
well solved :
//main :
BufferedImage image=null;
//for my frame :
String filename=PICTURE_PATH;
image = ImageIO.read(new File(filename)); //image is :
setContentPane(getJLabel());
pack();
repaint();
//get jlabel function :
private JLabel getJLabel() {
if (image == null) return null;
ImageIcon icon = new ImageIcon(image);
return new JLabel(icon);
}
-
#9: I'm using JBuilder 8.
#10: This will work if you are using a single image throughout the execution time of the application. If the application used several images that were displayed according to some programmatic state/condition you would have to implement double buffering to avoid flickering.
PS:For the case of an applet you would have to use a MediaTracker to ensure complete image load before displaying it. I always use a MediaTracker, even for applications running locally.
eschew obfuscation
-
Well maybe the difference of versions u r right, but at least it's free and open source 
I don't matter about the best tool now, I have just asked 2 weeks ago about the best tool and the only response by nspil was the netbeans, first time I couldn't just starting creating a gui with it -auto generated code- but now I make it as I playing puzzle , but I'm now just creating a small jar tool to work under any platform to transfere desired data to it , this tool will work with my application [c++/vb] based , but I'm really willing to transform it to be comletly java based , when I got time I'll plain to do that and I'll see again in the best tool to make my work based on it ..
About the picture I just wanna paint the background with a small logo , not big work , but I think if I should put many pictures I wont use the pack(); and resize my frame to match the images total width and height .
Thanks sijale.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|