Skip to main content

changing nimbus look n feel background

6 replies [Last post]
falazar
Offline
Joined: 2009-03-11

for the nimbus look n feel:

Does anyone know how to change the DesktopPane[Enabled].backgroundPainter object? It controls the blue background image on the desktop panel. The colors are changed on it now, but we would like to put a specific icon there instead.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Peter Zhelezniakov

swing@javadesktop.org wrote:
> Yeah thats what I was trying, but I am unsure how to create the painter object, and couldnt find the imports needed.
>
> I was trying something like this:
>
>
>
> public class BgPainter implements Painter {
> private Image cache;
> public void paint(Graphics g, Rectangle rect) {
> cache = Image.createImage(rect.getSize().getWidth(), rect.getSize().getWidth());
> cache.getGraphics().setColor(0x0000ff);
> cache.getGraphics().drawLine(10, 10, 20, 20);
> cache.getGraphics().fillRect(0, 0, 10, 9);
> cache.getGraphics().fillLinearGradient(0xff0000, 0x00ff00, 50, 0, 40, 10, true);
> g.drawImage(cache, 2, 2);
> }
> }
>
>
> // testing
> import java.awt.Graphics;
> import java.awt.Image;
> import java.awt.Rectangle;
> //import javax.swing.Painter;
>
>
> But not sure where the painter import is coming from, or if the other awt ones are correct.

Painter is currently com.sun.java.swing.Painter, AWT imports are correct
indeed.

--
Peter | x33066 | location: SPB04 | timezone: GMT+03

swebb
Offline
Joined: 2010-03-10

If anyone is interested I tweaked the background using this technique. Basically I didn't want the hit of drawing the fancy vector based background as it was killing the processor on a low spec atom based pc. A simple fill is fine.

Enjoy

Steve

public class MyDesktopPane extends JDesktopPane {

@Override
public void updateUI() {
if ("Nimbus".equals(UIManager.getLookAndFeel().getName())) {
UIDefaults map = new UIDefaults();
Painter painter = new Painter() {

Color color = null;

@Override
public void paint(Graphics2D g, JComponent c, int w, int h) {
g.setColor(color == null ? UIManager.getDefaults().getColor("desktop") : color);
g.fillRect(0, 0, w, h);
}
};
map.put("DesktopPane[Enabled].backgroundPainter", painter);
putClientProperty("Nimbus.Overrides", map);
}
super.updateUI();
}
}

Peter Zhelezniakov

falazar wrote:
> for the nimbus look n feel:
>
> Does anyone know how to change the DesktopPane[Enabled].backgroundPainter object? It controls the blue background image on the desktop panel. The colors are changed on it now, but we would like to put a specific icon there instead.

Try
UIManager.put("DesktopPane[Enabled].backgroundPainter", yourPainter);

This should change the painter app-wide. It's also possible to customize
one particular desktoppane component only, see Jasper's blog [1] for
details.

[1] http://www.jasperpotts.com/blog/2008/08/skinning-a-slider-with-nimbus/

--
Peter

falazar
Offline
Joined: 2009-03-11

Yeah thats what I was trying, but I am unsure how to create the painter object, and couldnt find the imports needed.

I was trying something like this:

public class BgPainter implements Painter {
private Image cache;
public void paint(Graphics g, Rectangle rect) {
cache = Image.createImage(rect.getSize().getWidth(), rect.getSize().getWidth());
cache.getGraphics().setColor(0x0000ff);
cache.getGraphics().drawLine(10, 10, 20, 20);
cache.getGraphics().fillRect(0, 0, 10, 9);
cache.getGraphics().fillLinearGradient(0xff0000, 0x00ff00, 50, 0, 40, 10, true);
g.drawImage(cache, 2, 2);
}
}

// testing
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
//import javax.swing.Painter;

But not sure where the painter import is coming from, or if the other awt ones are correct.

can we use a small icon image also?

James

davidvidal
Offline
Joined: 2012-05-26

I know this post is old. But today I did it:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.Painter;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

/**
*
* @author dvidal
*/
public class DesktopMain extends JDesktopPane{

private Image cache;
private Color bgColor;

public DesktopMain() {
bgColor = UIManager.getDefaults().getColor("Desktop");
cache= new ImageIcon(getClass().getResource("/images/logopoliedro.png")).getImage();
}

@Override
public void updateUI() {
if("Nimbus".equalsIgnoreCase(UIManager.getLookAndFeel().getName())){
UIDefaults map = new UIDefaults();
Painter painter = new Painter() {

@Override
public void paint(Graphics2D g, Object object, int width, int height) {
g.drawImage(cache, 0, 0, bgColor==null?UIManager.getColor("Desktop"):bgColor, null);
}
};
map.put("DesktopPane[Enabled].backgroundPainter", painter);
putClientProperty("Nimbus.Overrides", map);
}
super.updateUI();
}

}

And it worked fine for me, a customized JDesktopPane background image with the Nimbus Look And Feel. By the way, I'm using Netbeans on Oracle JDK and JRE 7 with Linux Mint x64.
I hope it helps somebody.

Jasper Potts

import com.sun.java.swing.Painter

Hope that helps

Jasper

On 16 Mar 2009, at 14:05, swing@javadesktop.org wrote:

> Yeah thats what I was trying, but I am unsure how to create the
> painter object, and couldnt find the imports needed.
>
> I was trying something like this:
>
>
>
> public class BgPainter implements Painter {
> private Image cache;
> public void paint(Graphics g, Rectangle rect) {
> cache = Image.createImage(rect.getSize().getWidth(),
> rect.getSize().getWidth());
> cache.getGraphics().setColor(0x0000ff);
> cache.getGraphics().drawLine(10, 10, 20, 20);
> cache.getGraphics().fillRect(0, 0, 10, 9);
> cache.getGraphics().fillLinearGradient(0xff0000, 0x00ff00, 50, 0,
> 40, 10, true);
> g.drawImage(cache, 2, 2);
> }
> }
>
>
> // testing
> import java.awt.Graphics;
> import java.awt.Image;
> import java.awt.Rectangle;
> //import javax.swing.Painter;
>
>
> But not sure where the painter import is coming from, or if the
> other awt ones are correct.
>
> can we use a small icon image also?
>
> James
> [Message sent by forum member 'falazar' (falazar)]
>
> http://forums.java.net/jive/thread.jspa?messageID=337192

Jasper Potts

Sun Microsystems UK
Blog www.jasperpotts.com
[att1.html]
[6b_top.gif]