Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/09/2015, 09:42
wagego
 
Fecha de Ingreso: agosto-2015
Mensajes: 24
Antigüedad: 8 años, 8 meses
Puntos: 0
Jbutton propio

Gente, necesito su ayuda.
Necesito crear un boton propio, con un dibujo adentro.
El boton tiene que tener 3 estados.
Tiene que iniciarse como un boton cuadrado gris, luego si hago click dibujarse una flecha <- adentro .

La cosa es que voy a tener un jtextfield y a la derecha el boton mio creado. Al hacer click me ponga el <- y el cursor para escribir se va a posicionar sobre el jtextfield. Al ingresar lo que necesito en el jtextfield y luego de hacer unas verificaciones, si esta ok, en el boton se tiene que dibujar un check ( ✓ ).

Por el momento, hice una clase que extiende de JButton el cual en paintComponent me llama a cualquiera de los 2 metodos ( drawArrow 0 drawOk ).

Dejo el código para que vean lo que hice.

Código:
public class JIconButton extends JButton implements ActionListener{
	
    private boolean mouseInside;
    private Color btnColor;
    private Color symbolCol;
    private final int ARR_SIZE = 20;
    Graphics g;
    
    public JIconButton(Color col, Color symbolLbk)
    {
        super();
    	this.btnColor = col;
    	this.symbolCol = symbolLbk;
        setBorder(new LineBorder(Color.LIGHT_GRAY, 0, true));
        setContentAreaFilled(false);
        setFocusPainted(false);
        setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
       Graphics2D g2 = (Graphics2D) g.create();
       g2.setPaint(new GradientPaint(new Point(0, 0), btnColor, new Point(0,getHeight()), btnColor));
       g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
       g2.setPaint(Color.BLACK);

      //drawArrow(g, 50, 30, 10, 30);
       //drawOk(g, 60, 30, 10, 30); (ACA ES DONDE ME DIBUJA UN BOTON CON FLECHA O OK

       g2.dispose();
       // super.paintComponent(g);
    }
    
    void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
        Graphics2D g = (Graphics2D) g1.create();

        double dx = x2 - x1;
        double dy = y2 - y1;
        double angle = Math.atan2(dy, dx);
        int len = (int) Math.sqrt(dx*dx + dy*dy);
        AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
        at.concatenate(AffineTransform.getRotateInstance(angle));
        g.transform(at);
        g.setColor(symbolCol);
        // Draw horizontal arrow starting in (0, 0)
        g.setStroke(new BasicStroke(6));
        g.draw(new Line2D.Float(0, 0, len-ARR_SIZE, 0));
        //g.drawLine(0, 0, len, 0);
        g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
                      new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);

    }

  void drawOk(Graphics g1, int x1, int y1, int x2, int y2) {
        Graphics2D g = (Graphics2D) g1.create();
       
        g.setColor(symbolCol);
        // Draw horizontal arrow starting in (0, 0)
        g.setStroke(new BasicStroke(6));
        g.draw(new Line2D.Float(10,40,20,50));
        g.draw(new Line2D.Float(50,10,20,50));
        //g.drawLine(0, 0, len, 0);
    }
Esa clase me dibuja lo siguiente
( https://www.dropbox.com/s/pf3o0u275kfk3tu/btnflecha.jpg?dl=0 )


o el ok
( https://www.dropbox.com/s/pp7a8cuo3eutc4g/botonok.jpg?dl=0 )


Bueno, lo que necesito yo, es hacer la prueba, desde un main común.
main....
JFrame jf = new JFrame();
JPanel jp = new JPanel();
Color botonLbk;
botonLbk = new Color(119,136,153);
Color symbolLbk;
symbolLbk= new Color(16,220,36);
JIconButton jb;
jb = new JIconButton(botonLbk,symbolLbk);
jp.add(jb);

jf.add(jp);
jf.setSize(new Dimension(150,150));
jf.setVisible(true);


Bueno, ahí tendria mi boton creado, ¿como hago yo para cuando hago click sobre el mismo me cambie el estado al boton??? osea, que me ponga la flecha o el ok???

Gracias.