Ver Mensaje Individual
  #2 (permalink)  
Antiguo 23/04/2012, 02:18
alexg88
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: Colocar un circulo degradado en una ventana degradada

Tienes que meter todo el código en el código paint de uno de los dos paneles.

Código Java:
Ver original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package Proyects;
  7. import java.awt.Color;
  8. import javax.swing.JPanel;
  9. import java.awt.GradientPaint;
  10. import java.awt.Graphics;
  11. import java.awt.Graphics2D;
  12. /**
  13.  *
  14.  * @author Usuario
  15.  */
  16. class degradadoHorizontal extends JPanel {
  17.  
  18.     public degradadoHorizontal() {
  19.     }
  20.  
  21.     public void paint(Graphics g){
  22.         Graphics2D g2d=(Graphics2D)g;
  23.         //Creo el degradado horizontal con las
  24.         //coordenadas (0,0) al (anchura del componente, 0)
  25.         GradientPaint horizontalGradient = new GradientPaint (0,0, Color.RED, getWidth(),0,Color.BLUE);
  26.         g2d.setPaint(horizontalGradient);
  27.         g2d.fillRect(0,0, getWidth(), getHeight());
  28.  
  29.         //Creo el degradado horizontal con las
  30.         //coordenadas (0,0) al (anchura del componente, 0)
  31.         // Gradiente de color de azul a verde
  32.         GradientPaint gradiente = new GradientPaint(75,75,Color.blue, 100,100,Color.green);
  33.         // Se fija el gradiente
  34.         g2d.setPaint( gradiente );
  35.         // Circulo a rellenar
  36.         Ellipse2D circulo = new Ellipse2D.Float(10,10,150,150);
  37.         g2d.setPaint(gradiente);
  38.         g2d.fill(circulo);
  39.     }
  40.  
  41. }

Main:

Código Java:
Ver original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package Proyects;
  7. import java.awt.GridLayout;
  8. import javax.swing.JFrame;
  9.  
  10. /**
  11.  *
  12.  * @author Usuario
  13.  */
  14. public class Main extends JFrame{
  15.  
  16.     private static void alumnos(){
  17.         //procedo a crear y preparar mi ventana
  18.  
  19.         JFrame ventana= new JFrame("Mis Degrados");
  20.         ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         ventana.getContentPane().add (new degradadoHorizontal());
  22.         ventana.setVisible(true);
  23.         ventana.setSize(400,400);
  24.     }
  25.         /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) {
  29.         // TODO code application logic here
  30.         javax.swing.SwingUtilities.invokeLater(new Runnable(){
  31.          public void run(){
  32.              alumnos();
  33.          }
  34.          });
  35.     }
  36. }