Модераторы: LSD, AntonSaburov
  

Поиск:

Ответ в темуСоздание новой темы Создание опроса
> Кнопки произвольной формы 
:(
    Опции темы
LSD
Дата 2.11.2004, 13:22 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Leprechaun Software Developer
****


Профиль
Группа: Модератор
Сообщений: 15718
Регистрация: 24.3.2004
Где: Dublin

Репутация: 210
Всего: 538



Есть в Java2D интерфейс Shape который описывает любые геометрические примитивы.
А вот и готовый пример как можно сделать самую "извращенную кнопку".

Код

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class JSwingButton extends JButton
{
 private Paint rolloverHighlight, armedHighlight, borderHighlight, borderShadow;
 private Shape shape, upperBorder, lowerBorder;
 private Stroke borderStroke;

 public JSwingButton(String label, Paint rolloverHighlight, Paint armedHighlight, Paint borderHighlight, Paint borderShadow, Stroke borderStroke)
 {
   super(label);
   setContentAreaFilled(false);
   setRolloverEnabled(true);
   setFocusable(false);
   setRolloverHighlight(rolloverHighlight);
   setArmedHighlight(armedHighlight);
   setBorderHighlight(borderHighlight);
   setBorderShadow(borderShadow);
   setBorderStroke(borderStroke);
   setShape(new Ellipse2D.Double(0.0, 0.0, 100.0, 100.0));
   setUpperBorder(new Arc2D.Double(0.0, 0.0, 100.0, 100.0, 000.0, 180.0, Arc2D.OPEN));
   setLowerBorder(new Arc2D.Double(0.0, 0.0, 100.0, 100.0, 180.0, 180.0, Arc2D.OPEN));
   addComponentListener(new ResizeListener());
 }

 public JSwingButton(String label)
 {
   this(label, null, null, null, null, null);
 }

 public JSwingButton()
 {
   this("", null, null, null, null, null);
 }

 public void setRolloverHighlight(Paint rolloverHighlight)
 {
   Paint oldHighLight = this.rolloverHighlight;
   this.rolloverHighlight = rolloverHighlight;
   if(this.rolloverHighlight == null)
     this.rolloverHighlight = new Color(255, 255, 255, 64);
   firePropertyChange("rolloverHighlight", oldHighLight, this.rolloverHighlight);
 }

 public Paint getRolloverHighlight()
 {
   return rolloverHighlight;
 }

 public void setArmedHighlight(Paint armedHighlight)
 {
   Paint oldHighLight = this.armedHighlight;
   this.armedHighlight = armedHighlight;
   if(this.armedHighlight == null)
     this.armedHighlight = new Color(0, 0, 0, 64);
   firePropertyChange("armedHighlight", oldHighLight, this.armedHighlight);
 }

 public Paint getArmedHighlight()
 {
   return armedHighlight;
 }

 public Shape getShape()
 {
   return shape;
 }

 public void setShape(Shape shape)
 {
   if(shape != null && !shape.equals(this.shape))
   {
     Shape oldShape = this.shape;
     this.shape = shape;
     firePropertyChange("shape", oldShape, shape);
   }
 }


 protected void resizeShapes()
 {
   Dimension size = getSize(new Dimension());
   Rectangle shapeBounds = getBorderStroke().createStrokedShape(shape).getBounds();
   if(shapeBounds.x != 0 || shapeBounds.y != 0)
   {
     AffineTransform transform = AffineTransform.getTranslateInstance( -shapeBounds.x, -shapeBounds.y);
     shape = transform.createTransformedShape(shape);
     upperBorder = transform.createTransformedShape(upperBorder);
     lowerBorder = transform.createTransformedShape(lowerBorder);
   }

   if(shapeBounds.width != size.width || shapeBounds.height != size.height)
   {
     double sx = (double) size.width / (double) shapeBounds.width;
     double sy = (double) size.height / (double) shapeBounds.height;
     AffineTransform transform = AffineTransform.getScaleInstance(sx, sy);
     shape = transform.createTransformedShape(shape);
     upperBorder = transform.createTransformedShape(upperBorder);
     lowerBorder = transform.createTransformedShape(lowerBorder);
   }
 }

 public Shape getUpperBorder()
 {
   return upperBorder;
 }

 public void setUpperBorder(Shape upperBorder)
 {
   Shape oldUpperBorder = this.lowerBorder;
   this.upperBorder = upperBorder;
   firePropertyChange("upperBorder", oldUpperBorder, upperBorder);
 }

 public Shape getLowerBorder()
 {
   return lowerBorder;
 }

 public void setLowerBorder(Shape lowerBorder)
 {
   Shape oldLowerBorder = this.lowerBorder;
   this.lowerBorder = lowerBorder;
   firePropertyChange("lowerBorder", oldLowerBorder, lowerBorder);
 }

 public void setBorderHighlight(Paint borderHighlight)
 {
   Paint oldBorderHighlight = this.borderHighlight;
   this.borderHighlight = borderHighlight;
   if(this.borderHighlight == null)
     this.borderHighlight = UIManager.getColor("Button.light");
   firePropertyChange("borderHighlight", oldBorderHighlight, this.borderHighlight);
 }

 public Paint getBorderHighlight()
 {
   return borderHighlight;
 }

 public void setBorderShadow(Paint borderShadow)
 {
   Paint oldBorderShadow = this.borderShadow;
   this.borderShadow = borderShadow;
   if(this.borderShadow == null)
     this.borderShadow = UIManager.getColor("Button.shadow");
   firePropertyChange("borderShadow", oldBorderShadow, this.borderShadow);
 }

 public Paint getBorderShadow()
 {
   return borderShadow;
 }

 public Stroke getBorderStroke()
 {
   return borderStroke;
 }

 public void setBorderStroke(Stroke borderStroke)
 {
   Stroke oldBorderStroke = this.borderStroke;
   this.borderStroke = borderStroke;
   if(this.borderStroke == null)
     this.borderStroke = new BasicStroke();
   firePropertyChange("lowerBorder", oldBorderStroke, this.borderStroke);
 }

 protected void paintComponent(Graphics g)
 {
   Graphics2D g2 = (Graphics2D) g;
   if(getModel().isArmed())
   {
     g2.setPaint(getArmedHighlight());
     g2.fill(shape);
   }
   else if(getModel().isRollover())
   {
     g2.setPaint(getRolloverHighlight());
     g2.fill(shape);
   }

   super.paintComponent(g);
 }

 protected void paintBorder(Graphics g)
 {
   Graphics2D g2 = (Graphics2D) g;
   g2.setStroke(getBorderStroke());
   if(getModel().isRollover())
   {
     if(getUpperBorder() != null && getLowerBorder() != null)
     {
       g2.setPaint(getBorderHighlight());
       g2.draw(getUpperBorder());
       g2.setPaint(getBorderShadow());
       g2.draw(getLowerBorder());
     }
     else
     {
       g2.setPaint(getBorderShadow());
       g2.draw(getShape());
     }
   }
 }

 public boolean contains(int x, int y)
 {
   return shape.contains(x, y);
 }

 public static void main(String[] args)
 {
   try
   {
     UIManager.setLookAndFeel(UIManager.getInstalledLookAndFeels()[0].getClassName());
   }
   catch(Exception ex)
   {}

   JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   Container cont = frame.getContentPane();
   cont.setLayout(new GridLayout(1, 0));

   JSwingButton button = new JSwingButton("Button 1");
   cont.add(button);
   button = new JSwingButton("Button 2");
   Dimension d = new Dimension(250, 150);
   button.setPreferredSize(d);
   button.setShape(new RoundRectangle2D.Double(0, 0, d.width, d.height, 100, 100));
   button.setUpperBorder(null);
   button.setLowerBorder(null);
   button.setArmedHighlight(new GradientPaint(0, 0, Color.RED.darker(), d.width, d.height, Color.YELLOW.darker()));
   button.setRolloverHighlight(new GradientPaint(0, 0, Color.RED, d.width, d.height, Color.YELLOW));
   button.setBorderShadow(new GradientPaint(0, 0, Color.GREEN, d.width, d.height, Color.BLUE));
   //button.setBorderStroke(new BasicStroke(5.0f));
   button.setBorderStroke(new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f, new float[] {30.0f, 10.0f, 10.0f, 10.0f}, 0.0f));
   cont.add(button);

   frame.pack();
   frame.setLocationRelativeTo(null);
   frame.show();
 }

 private class ResizeListener implements ComponentListener
 {
   public void componentResized(ComponentEvent e)
   {
     resizeShapes();
   }

   public void componentMoved(ComponentEvent e)
   {}

   public void componentShown(ComponentEvent e)
   {}

   public void componentHidden(ComponentEvent e)
   {}
 }
}

Выглядит это так:
[IMG]http://forum.vingrad.ru/index.php?act=module&module=vingradfaq&target=download_file&articleid=128&attachid=1[/IMG]


А вот еще один простой пример круглой кнопки

Код

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class RoundButton extends JButton 
{
    public RoundButton(String label) 
    {
        super(label);

        // These statements enlarge the button so that it 
        // becomes a circle rather than an oval.
        Dimension size = getPreferredSize();
        size.width = size.height = Math.max(size.width, size.height);
        setPreferredSize(size);

        // This call causes the JButton not to paint 
        // the background.
        // This allows us to paint a round background.
        setContentAreaFilled(false);
    }

    // Paint the round background and label.
    protected void paintComponent(Graphics g) 
    {
        if (getModel().isArmed()) {
            // You might want to make the highlight color 
            // a property of the RoundButton class.
            g.setColor(Color.lightGray);
        } 
        else {
            g.setColor(getBackground());
        }
        g.fillOval(0, 0, getSize().width-1, 
        getSize().height-1);

        // This call will paint the label and the 
        // focus rectangle.
        super.paintComponent(g);
    }

    // Paint the border of the button using a simple stroke.
    protected void paintBorder(Graphics g) 
    {
        g.setColor(getForeground());
        g.drawOval(0, 0, getSize().width-1, 
        getSize().height-1);
    }

    // Hit detection.
    Shape shape;
    public boolean contains(int x, int y) 
    {
        // If the button has changed size, 
        // make a new shape object.
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
        }
        return shape.contains(x, y);
    }

    // Test routine.
    public static void main(String[] args) 
    {
        // Create a button with the label "Jackpot".
        JButton button = new RoundButton("Jackpot");
        button.setBackground(Color.green);

        // Create a frame in which to show the button.
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(Color.yellow);
        frame.getContentPane().add(button);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(150, 150);
        frame.setVisible(true);
    }
}

Выглядит это так:
[IMG]http://forum.vingrad.ru/index.php?act=module&module=vingradfaq&target=download_file&articleid=128&attachid=2[/IMG]


--------------------
Disclaimer: this post contains explicit depictions of personal opinion. So, if it sounds sarcastic, don't take it seriously. If it sounds dangerous, do not try this at home or at all. And if it offends you, just don't read it.
PM MAIL WWW   Вверх
  
Ответ в темуСоздание новой темы Создание опроса
Правила форума "Java"
LSD   AntonSaburov
powerOn   tux
javastic
  • Прежде, чем задать вопрос, прочтите это!
  • Книги по Java собираются здесь.
  • Документация и ресурсы по Java находятся здесь.
  • Используйте теги [code=java][/code] для подсветки кода. Используйтe чекбокс "транслит", если у Вас нет русских шрифтов.
  • Помечайте свой вопрос как решённый, если на него получен ответ. Ссылка "Пометить как решённый" находится над первым постом.
  • Действия модераторов можно обсудить здесь.
  • FAQ раздела лежит здесь.

Если Вам помогли, и атмосфера форума Вам понравилась, то заходите к нам чаще! С уважением, LSD, AntonSaburov, powerOn, tux, javastic.

 
0 Пользователей читают эту тему (0 Гостей и 0 Скрытых Пользователей)
0 Пользователей:
« Предыдущая тема | Java: Общие вопросы | Следующая тема »


 




[ Время генерации скрипта: 0.0442 ]   [ Использовано запросов: 23 ]   [ GZIP включён ]


Реклама на сайте     Информационное спонсорство

 
По вопросам размещения рекламы пишите на vladimir(sobaka)vingrad.ru
Отказ от ответственности     Powered by Invision Power Board(R) 1.3 © 2003  IPS, Inc.