Páginas: [1]
|
 |
|
Autor
|
Tema: Java (Leído 1656 veces)
|
calitb
Desconectado
Mensajes: 2
|
 |
Java
« en: 25 Noviembre 2005, 17:10 » |
|
necesito que alguien me diga como puedo poner botones con formas redondas en JFrame, y como le puedo poner una imagen de fondo a un objeto de la clase Container porque hasta ahora solo he podido poner colores de fondo.
|
|
|
|
|
En línea
|
|
|
|
Neostream
Desconectado
Mensajes: 113
Neostream&ImagenCreativa
|
 |
Re: Java
« Respuesta #1 en: 26 Noviembre 2005, 17:25 » |
|
Saludos bueno la pregunta es muy buena yo tambien tengo la misma duda =o seria bueno si alguien sabe nos diera una respuesta saludos 
|
|
|
|
|
En línea
|
solo se q nada se 
|
|
|
AnKeR
Desconectado
Mensajes: 123
printk("\n%s\n","Kernel Panic");
|
 |
Re: Java
« Respuesta #2 en: 27 Noviembre 2005, 01:46 » |
|
Bueno, me ha resultado curiosa la idea y he encontrado esto: import java.awt.*; import java.awt.geom.*; import javax.swing.*;
public class RoundButton extends JButton {//Somos un boton :D 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); } } Es un poco cutre pero los colores que son bastante pobres... Se ve bastante feo... pero bueno es un boton redondo. Me parece wapa la idea de crear un nuevo boton  Salu2o3
|
|
|
|
|
En línea
|
|
|
|
AnKeR
Desconectado
Mensajes: 123
printk("\n%s\n","Kernel Panic");
|
 |
Re: Java
« Respuesta #3 en: 27 Noviembre 2005, 01:54 » |
|
mmm este esta mucho mas currado: import java.awt.AWTEvent; import java.awt.AWTEventMulticaster; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionEvent;
/** * A circular button. * */
public class CircularButton extends Component { /** * The label on the button. * */
private String _strLabel;
/** * Constructs a circular button with no label. * */
public CircularButton() { this(""); }
/** * Constructs a circular button with the specified label. * */
public CircularButton(String strLabel) { enableEvents(AWTEvent.MOUSE_EVENT_MASK); enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); enableEvents(AWTEvent.FOCUS_EVENT_MASK);
_strLabel = strLabel; }
/** * Gets the label. * */
public String getLabel() { return _strLabel; } /** * Sets the label. * */
public void setLabel(String strLabel) { _strLabel = strLabel;
invalidate();
repaint(); } /** * Is the button pressed? * */
private boolean _boolPressed = false;
/** * Does the button have the input focus? * */
private boolean _boolFocus = false;
/** * Paint the circular button. * */
public void paint(Graphics g) { Dimension dim = getSize();
int n = (dim.width < dim.height ? dim.width : dim.height) - 1; // draw the interior
if(_boolPressed) { g.setColor(getBackground().darker().darker()); } else { g.setColor(getBackground()); }
g.fillArc(6, 6, n - 12, n - 12, 0, 360); // draw the perimeter
g.setColor(getBackground().darker().darker().darker().darker());
g.drawArc(6, 6, n - 12, n - 12, 0, 360); // draw the ring
g.drawArc(0, 0, n, n, 0, 360);
// fill the ring
if (_boolFocus == true) { g.drawArc(1, 1, n - 2, n - 2, 0, 360); g.drawArc(2, 2, n - 4, n - 4, 0, 360); g.drawArc(3, 3, n - 6, n - 6, 0, 360); g.drawArc(0, 0, n - 1, n - 1, 0, 360); g.drawArc(1, 1, n - 1, n - 1, 0, 360);
g.drawArc(1, 1, n - 3, n - 3, 0, 360); g.drawArc(2, 2, n - 3, n - 3, 0, 360);
g.drawArc(2, 2, n - 5, n - 5, 0, 360); g.drawArc(3, 3, n - 5, n - 5, 0, 360); }
// draw the label
Font font = getFont();
if (font != null) { FontMetrics fontmetrics = getFontMetrics(font);
g.setColor(getForeground());
g.drawString(_strLabel, n/2 - fontmetrics.stringWidth(_strLabel)/2, n/2 + fontmetrics.getMaxDescent()); } } /** * The circular button's minimum size. * */
private static Dimension _dimMinimumSize = new Dimension(100, 100);
/** * Gets the minimum size. * */
public Dimension getMinimumSize() { return new Dimension(_dimMinimumSize); }
/** * Gets the preferred size (based on the label). * */
public Dimension getPreferredSize() { Font font = getFont();
if (font == null) { return new Dimension(_dimMinimumSize); } else { FontMetrics fontmetrics = getFontMetrics(font);
int n = Math.max(fontmetrics.stringWidth(_strLabel) + 35, fontmetrics.getHeight() + 35);
return new Dimension(n, n); } } /** * The chain of action listeners. * */
private ActionListener _al = null;
/** * Adds an action listener. * */
public void addActionListener(ActionListener al) { _al = AWTEventMulticaster.add(_al, al); } /** * Removes an action listener. * */
public void removeActionListener(ActionListener al) { _al = AWTEventMulticaster.remove(_al, al); }
/** * Determines whether or not the circular button contains * the point and the specified coordinates. */
public boolean contains(int x, int y) { Dimension dim = getSize();
int nX = dim.width/2; int nY = dim.height/2;
return ((nX-x)*(nX-x)+(nY-y)*(nY-y)) <= (nX*nY); } /** * The currently active button. * */
private Component _component = null;
/** * Process focus events. * */
public void processFocusEvent(FocusEvent e) { switch (e.getID()) { case FocusEvent.FOCUS_GAINED:
if (_boolFocus == false) { _boolFocus = true;
repaint(); }
break;
case FocusEvent.FOCUS_LOST:
if (_boolFocus == true) { _boolFocus = false;
repaint(); }
break; }
super.processFocusEvent(e); }
/** * Process mouse events. * */
public void processMouseEvent(MouseEvent e) { switch (e.getID()) { case MouseEvent.MOUSE_PRESSED:
requestFocus();
_component = e.getComponent();
_boolPressed = true;
repaint();
break;
case MouseEvent.MOUSE_RELEASED:
_component = null;
if (_boolPressed == true) { if (_al != null) { _al.actionPerformed ( new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, _strLabel, e.getModifiers() ) ); }
_boolPressed = false; repaint(); }
break;
case MouseEvent.MOUSE_ENTERED: if (_component == e.getComponent() && _boolPressed == false) { _boolPressed = true;
repaint(); }
break;
case MouseEvent.MOUSE_EXITED:
if (_boolPressed == true) { _boolPressed = false;
repaint(); }
break; }
super.processMouseEvent(e); }
/** * Process mouse motion events. * */
public void processMouseMotionEvent(MouseEvent e) { switch (e.getID()) { case MouseEvent.MOUSE_DRAGGED:
if (_component == e.getComponent() && !contains(e.getX(), e.getY()) && _boolPressed == true) { _boolPressed = false;
repaint(); } else if (_component == e.getComponent() && contains(e.getX(), e.getY()) && _boolPressed == false) { _boolPressed = true;
repaint(); }
break; }
super.processMouseMotionEvent(e); } }
Hereda de un Componente en vez de un boton, se tiene que currar mas movidillas pero queda mejor. Salu2o3
|
|
|
|
|
En línea
|
|
|
|
Neostream
Desconectado
Mensajes: 113
Neostream&ImagenCreativa
|
 |
Re: Java
« Respuesta #4 en: 28 Noviembre 2005, 18:10 » |
|
 Gracias =D gracias por responder a por cierto Anker ese codigo fuente no tiene main como lo ejecuto LOL XD  salu2
|
|
|
|
|
En línea
|
solo se q nada se 
|
|
|
|
|
ShawnShadow
Desconectado
Mensajes: 16
|
 |
Re: Java
« Respuesta #6 en: 05 Diciembre 2005, 06:24 » |
|
Para poner una imagen de fondo utiliza el método paintComponent
Salu2.
|
|
|
|
|
En línea
|
|
|
|
|
|
|
Páginas: [1]
|
|
|
|