Okrągły button

0

Postanowiłem zrobić okrągły guzik. Poszperałem w necie i znalazłem stonkę http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html na której jest pokazane jak zrobić okrągłe JTextField.

Przerobiłem kod po swojemu:

public class Pionek extends  JButton
{	
	int NrPole;
	
	Pionek()
	{ 	super("tidum");
		NrPole	= 0;
		this.setOpaque(false);

	}
	
	protected void paintComponent(Graphics g) 
	{	int width = getWidth();
		int height = getHeight();

		g.setColor( new Color (255, 255, 255, 0) );
		g.fillOval(25, 25, 500, 500);		
		g.setColor( this.getBackground() );
		g.fillOval(25, 25, 50, 50);

		super.paintComponent(g);
            }
} 

i nie działa;(
po ustawieniu opaque na false w ogóle nie są wyzwalane funkcje rysowania, dlaczego?

0

super.paintComponent() powinno być pierwsze

0

Ale program nie wiem dlaczego w ogóle nie wykonuje funkcji paint i paintComponent.

0

Jestem nowicjuszem:
public class Klasa extends JButton{

Klasa()
{         super("tidum");
        this.setOpaque(false);

}

public void paintComponent(Graphics g)
{
	super.paintComponent(g);
    g.setColor( new Color (255, 255, 255) );
    g.fillOval(25, 25, 500, 500);                
    g.setColor(Color.RED); //Jak chcesz uzyc BG, ktorego nie zdefiniowales??
    g.fillOval(25, 25, 50, 50);
}

}

0

Nie mam konta, nie moge edytowac :P, czyli ze nie rysuje sie po podales jakies dziwny parametr:
(255,255,255,0) Skad masz to 0??? Jakis nowy kolor, przeciez to jest RGB.

0

Kamyker Czwarty parametr konstruktora obiektu Color to the alpha component. Przy wyłowaniu konstruktora z 3 parametrami np: new Color ( 100, 100, 100 ) The Alpha component przyjmuje wartość 255.

0

Czyli mam rozumiec ze kolor 255,255,255,0 jest niewidzialny? W stu % przezroczysty?

0

A może zrób raczej to na zasadzie UI. Dam przykład oczywiście mocno uproszczony:

class RoundBorder implements Border {
    private final static Color BORDER_COLOR = Color.BLACK;
    private final static float BORDER_WIDTH = 2f;
    private final static int MARGIN = 5;

    @Override
    public Insets getBorderInsets(Component c) {
        int margin = (int) Math.ceil(BORDER_WIDTH) + MARGIN;
        return new Insets(margin,margin, margin, margin);
    }

    @Override
    public boolean isBorderOpaque() {
        return true;
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        Graphics2D g2 = (Graphics2D) g;
        Stroke orignalStroke = g2.getStroke();
        Object oldAntialiasing = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        try {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            this.doPaintBorder(c, g2, x, y, width, height);
        } finally {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntialiasing);
            g2.setStroke(orignalStroke);
        }

    }

    protected void doPaintBorder(Component c, Graphics2D g, int x, int y, int width, int height) {
        g.setStroke(new BasicStroke(BORDER_WIDTH));
        g.setColor(BORDER_COLOR);
        g.drawOval(x + 1, y + 1, width - 3, height - 3);
    }

}

class RoundButtonUI extends MetalButtonUI {
    private final static Color PRESSED_BACKGROUND = Color.ORANGE;
    private final static Color FOCUS_COLOR = Color.RED;
    private final static Color BACKGROUND_COLOR = Color.WHITE;

    public static ComponentUI createUI(JComponent component) {
        return new RoundButtonUI();
    }

    @Override
    public void installDefaults(AbstractButton b) {
        super.installDefaults(b);
        b.setBorder(new RoundBorder());
        b.setBackground(BACKGROUND_COLOR);
    }

    @Override
    public void update(Graphics g, JComponent c) {
        AbstractButton button = (AbstractButton) c;
        Graphics2D g2 = (Graphics2D) g;
        if (button.isContentAreaFilled() && button.isEnabled()) {
            this.paintBackground(g2, button);
            this.paint(g2, c);
        } else {
            super.update(g, c);
        }
    }

    @Override
    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
            Rectangle textRect, Rectangle iconRect) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(FOCUS_COLOR);
        Stroke oldStroke = g2.getStroke();
        Object oldAntialiasing = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        try {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setStroke(new BasicStroke(2f));
            g2.drawOval(2, 2, b.getWidth() - 5, b.getHeight() - 5);
        } finally {
            g2.setStroke(oldStroke);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntialiasing);
        }

    }

    @Override
    protected void paintButtonPressed(Graphics g, AbstractButton button) {
        g.setColor(PRESSED_BACKGROUND);
        g.fillOval(0, 0, button.getWidth() - 1, button.getHeight() - 1);
    }

    protected void paintBackground(Graphics2D g, AbstractButton button) {
        g.setPaint(button.getBackground());
        g.fillOval(0, 0, button.getWidth() - 1, button.getHeight() - 1);
    }

    @Override
    public Dimension getPreferredSize(JComponent c) {
        // dla wymuszenia kształtu koła
        Dimension preferred = super.getPreferredSize(c);
        int size = Math.max(preferred.width, preferred.height);
        return new Dimension(size, size);
    }

    @Override
    public Dimension getMinimumSize(JComponent c) {
     // dla wymuszenia kształtu koła
        Dimension minimum = super.getMinimumSize(c);
        int size = Math.max(minimum.width, minimum.height);
        return new Dimension(size, size);
    }
}

public class Main extends JFrame {
    private static final long serialVersionUID = -8450091917337048608L;
    private JButton button1;
    private JButton button2;

    private Main() {
        this.initComponents();
        this.initDefaults();
        this.createLayout();
    }

    private void initComponents() {
        this.button1 = new JButton("TEST 1");
        this.button1.setUI(new RoundButtonUI());

        this.button2 = new JButton("TEST 2");
        this.button2.setUI(new RoundButtonUI());
    }

    private void initDefaults() {
        this.setTitle("Test");
        this.setSize(160, 100);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void createLayout() {
        this.getContentPane().setLayout(new FlowLayout());
        this.getContentPane().add(this.button1);
        this.getContentPane().add(this.button2);
    }

    private void showFrameInCenter() {
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Main main = new Main();
                main.showFrameInCenter();
            }
        });
    }
}
0

Tak na szybko by pokazać ze działa.
Robi button w formie czarnego owalu.

 
package pl.asseco.amms.instalatorperyf.main;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JButton;

public class Pionek extends JButton {
    
    private Color circleColor = Color.BLACK;

    public Pionek() {
        super("test");
        this.setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {        
        //pobranie wysokosci i szerokosci dodanego buttona
        Dimension originalSize = super.getPreferredSize();        
        //lewy gorny rog
        int x = 0; 
        int y = 0;  
        int wysokoscOwalu = originalSize.height;
        int szerokoscOwalu = originalSize.width;
        g.setColor(circleColor);
        g.fillOval(x, y, szerokoscOwalu, wysokoscOwalu);        
    }

}

1 użytkowników online, w tym zalogowanych: 0, gości: 1