martes, 30 de julio de 2013

JButtonBall añade y aprende como hacer un JButton tipo esfera en java


Hace poco vimos como crear unJButton translucido o transparente, aquí vamos a utilizar una lógica similar primero necesito quitar los efectos por defecto que tiene el JButton ya sabemos que utilizando los métodos .
  • setFocusable(false);
  • setContentAreaFilled(false);
  • setBorderPainted(false);
nos permite quitar esos valores, entonces vamos utilizar la claseGraphics2D nuevamente y hago uso defillOval este método me permite dibujar un circulo donde añado o dibujo un gradiente con la claseGradientPaint que sera el fondo del JButton y otro circulo que estará situado en la parte superior este circulo sera el efecto de reflejo de la esfera también pintado con unGradientPaint,ahora lo nuevo aquí es el efecto de luz inferior de la esfera es una clase llamadaRadialGradientPaint la cual puede formar un gradiente pero de tipo radial es decir de tipo esférico sin la necesitada de recurrir alfillOval.

RadialGradientPaint p = new RadialGradientPaint(new Point2D.Double( getWidth() / 2.0, getHeight() * 1.5), getWidth() / 2.3f, new Point2D.Double(getWidth() / 2.0, getHeight() * 1.75 + 6), new float[] { 0.0f, 0.8f }, new Color[] { destelloinicial,destellofinal}, RadialGradientPaint.CycleMethod.NO_CYCLE, RadialGradientPaint.ColorSpaceType.SRGB, AffineTransform.getScaleInstance(1.0, 0.5));


esteRadialGradientPaint me recibe dos valores que la clasePoint2D.Double me proporciona estos valores serán la forma y la posición que yo necesito para situar el Radial ,luego me permite añadir valoresflotantes que seran valores de transparencia del Radial seguido de dos valores de tipoColorpara formar el gradiente.

Graphics2D g2d = (Graphics2D) g.create(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); int h = getHeight(); int height = getHeight(); ButtonModel modelo = getModel(); GradientPaint reflejo_superior = null; GradientPaint fondo_boton = null; /********************************/ reflejo_superior = new GradientPaint(0, 0, new Color(255, 255, 255),0, height / 2, new Color(120, 137, 145, 20)); /********************************/ fondo_boton = new GradientPaint(0, h / 2, getBackgroundTop(), 0, h, getBackgroundBotton()); setForeground(Color.WHITE); if(modelo.isPressed()) { /**SI EL BUTON FUE PRESIONADO*/ fondo_boton = new GradientPaint(0, h / 2, getBackgroundTop(), 0, h, getBackgroundBotton()); }else if(modelo.isRollover()) { /*****SI PASA POR ENCIMA*****/ fondo_boton = new GradientPaint(0, h / 2, getBackgroundTopHover(), 0, h, getBackgroundBottonHover()); } /*************************/ g2d.setPaint(fondo_boton); g2d.fillOval(0, 0, getWidth(), getHeight()); /**********REFLEJO SUPERIOR*************/ g2d.setPaint(reflejo_superior); g2d.fillOval(getWidth() / 3, 6, getWidth() / 3, getHeight() / 6); /*********DESTELLO*****************/ RadialGradientPaint p = new RadialGradientPaint(new Point2D.Double(getWidth() / 2.0, getHeight() * 1.5), getWidth() / 2.3f, new Point2D.Double(getWidth() / 2.0, getHeight() * 1.75 + 6), new float[] { 0.0f, 0.8f }, new Color[] { destelloinicial,destellofinal}, RadialGradientPaint.CycleMethod.NO_CYCLE, RadialGradientPaint.ColorSpaceType.SRGB, AffineTransform.getScaleInstance(1.0, 0.5)); g2d.setPaint(p); g2d.fillOval(0, 0, getWidth() - 1, getHeight() - 1);


les dejo el código fuente completo con métodos para cambiar el color de las esferas.


DESCARGAR


,

No hay comentarios:

Publicar un comentario