Originariamente inviato da falcoG
Vorrei sapere se qualcuno di voi ha idea di come si possa fare in Java a creare oggetti con sfondi sfumati. Ad esempio sfumature con due colori e cose del genere. Ad esempio applicabili ad oggetti come Label o anche a Pannelli interi.
Sì, certo che è possibile. Basta sfruttare le API di Java 2D, ovvero principalmente la classe Graphics2D più tutto il resto tra cui shape, le tecniche di stroking, filling e altro.
Ecco un esempio di pannello con dei gradienti:
codice:
import java.awt.*;
import javax.swing.*;
public class GradientTestFrame extends JFrame
{
public GradientTestFrame ()
{
super ("Gradient Test");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (300, 300);
GradientPanel panel = new GradientPanel ();
getContentPane ().add (panel);
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable ()
{
public void run ()
{
GradientTestFrame f = new GradientTestFrame ();
f.setVisible (true);
}
});
}
}
class GradientPanel extends JPanel
{
public void paintComponent (Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
int w = getWidth ();
int h = getHeight ();
GradientPaint gp1 = new GradientPaint (0, 0, Color.RED, w/2, 0, Color.GREEN);
GradientPaint gp2 = new GradientPaint (w/2, 0, Color.GREEN, w, 0, Color.BLUE);
g2d.setPaint (gp1);
g2d.fill (new Rectangle (0, 0, w/2, h));
g2d.setPaint (gp2);
g2d.fill (new Rectangle (w/2, 0, w, h));
}
}