G
Gast2
Gast
Gibt es eine einfach Möglichkeit einen JButton rund zu machen????
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
package gui.elements.draft;
import global.Skin;
import gui.ActionInterface;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import javax.swing.JButton;
public abstract class PPButton extends JButton implements ActionInterface, FocusListener
{
private String text = "";
private boolean mouseover = false;
private boolean focused = false;
private Image buttonFocusedImage = Skin.getButtonFocusedImage();
private Image buttonFocusedMouseoverImage = Skin.getButtonFocusedMouseoverImage();
private Image buttonUnfocusedImage = Skin.getButtonUnfocusedImage();
private Image buttonUnfocusedMouseoverImage = Skin.getButtonUnfocusedMouseoverImage();
private Image buttonDisabledImage = Skin.getButtonDisabledImage();
private Font buttonFocusedFont = Skin.getButtonFocusedFont();
private Font buttonFocusedMouseoverFont = Skin.getButtonFocusedMouseoverFont();
private Font buttonUnfocusedFont = Skin.getButtonUnfocusedFont();
private Font buttonUnfocusedMouseoverFont = Skin.getButtonUnfocusedMouseoverFont();
private Font buttonDisabledFont = Skin.getButtonDisabledFont();
private Color buttonFocusedFontColor = Skin.getButtonFocusedFontColor();
private Color buttonFocusedMouseoverFontColor = Skin.getButtonFocusedMouseoverFontColor();
private Color buttonUnfocusedFontColor = Skin.getButtonUnfocusedFontColor();
private Color buttonUnfocusedMouseoverFontColor = Skin.getButtonFocusedMouseoverFontColor();
private Color buttonDisabledFontColor = Skin.getButtonDisabledFontColor();
public PPButton(String text)
{
this.text = text;
addFocusListener(this);
setOpaque(false);
setFocusPainted(false);
setBorderPainted(false);
}
public void setFocused(boolean focused)
{
this.focused = focused;
repaint();
}
public void setMouseover(boolean mouseover)
{
this.mouseover = mouseover;
repaint();
}
public void focusGained(FocusEvent e)
{
setFocused(true);
}
public void focusLost(FocusEvent e)
{
setFocused(false);
}
@Override
public void setEnabled(boolean b)
{
setFocusable(b);
super.setEnabled(b);
}
protected void paintComponent(Graphics g)
{
if (isEnabled())
{
if (focused)
{
if (mouseover)
{
g.drawImage(buttonFocusedMouseoverImage, 0, 0, getWidth(), getHeight(), this);
g.setColor(buttonFocusedMouseoverFontColor);
g.setFont(buttonFocusedMouseoverFont);
}
else
{
g.drawImage(buttonFocusedImage, 0, 0, getWidth(), getHeight(), this);
g.setColor(buttonFocusedFontColor);
g.setFont(buttonFocusedFont);
}
}
else
{
if (mouseover)
{
g.drawImage(buttonUnfocusedMouseoverImage, 0, 0, getWidth(), getHeight(), this);
g.setColor(buttonUnfocusedMouseoverFontColor);
g.setFont(buttonUnfocusedMouseoverFont);
}
else
{
g.drawImage(buttonUnfocusedImage, 0, 0, getWidth(), getHeight(), this);
g.setColor(buttonUnfocusedFontColor);
g.setFont(buttonUnfocusedFont);
}
}
}
else
{
g.drawImage(buttonDisabledImage, 0, 0, getWidth(), getHeight(), this);
g.setColor(buttonDisabledFontColor);
g.setFont(buttonDisabledFont);
}
FontMetrics metrics = g.getFontMetrics();
Dimension d = getSize();
int x = (d.width - metrics.stringWidth(text)) / 2;
int y = (d.height - metrics.getHeight()) / 2 + metrics.getAscent();
g.drawString(text, x, y);
}
public PPFrame getPPFrame()
{
ActionInterface actionInterface = getActionInterfaceParent();
while (true)
{
if (actionInterface == null)
{
return null;
}
else if (actionInterface instanceof PPFrame)
{
return (PPFrame) actionInterface;
}
else
{
actionInterface = getActionInterfaceParent((Container) actionInterface);
}
}
}
public ActionInterface getActionInterfaceParent()
{
return getActionInterfaceParent(this);
}
public ActionInterface getActionInterfaceParent(Container container)
{
Container parent = container.getParent();
while (true)
{
if (parent == null)
{
return null;
}
else if (parent instanceof ActionInterface)
{
return (ActionInterface) parent;
}
else
{
parent = parent.getParent();
}
}
}
public void keyPressedAction(KeyEvent keyEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.keyPressedAction(keyEvent);
}
}
public void keyReleasedAction(KeyEvent keyEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.keyReleasedAction(keyEvent);
}
}
public void keyTypedAction(KeyEvent keyEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.keyTypedAction(keyEvent);
}
}
public void mouseClickedAction(MouseEvent mouseEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mouseClickedAction(mouseEvent);
}
}
public void mouseDraggedAction(MouseEvent mouseEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mouseClickedAction(mouseEvent);
}
}
public void mouseEnteredAction(MouseEvent mouseEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mouseEnteredAction(mouseEvent);
}
}
public void mouseExitedAction(MouseEvent mouseEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mouseExitedAction(mouseEvent);
}
}
public void mousePressedAction(MouseEvent mouseEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mousePressedAction(mouseEvent);
}
}
public void mouseReleasedAction(MouseEvent mouseEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mouseReleasedAction(mouseEvent);
}
}
public void mouseWheelAction(MouseWheelEvent mouseWheelEvent)
{
ActionInterface actionInterface = getActionInterfaceParent();
if (actionInterface != null)
{
actionInterface.mouseWheelAction(mouseWheelEvent);
}
}
}
setFocusPainted(false);
setBorderPainted(false);
protected void paintComponent(Graphics g)
{
if (isEnabled())
{
if (focused)
{
....
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JButton;
import javax.swing.JFrame;
public class RoundButton extends JButton {
private Shape shape;
private boolean rollover;
public RoundButton(String label) {
super(label);
setContentAreaFilled(false);
}
protected void paintComponent(Graphics g) {
if (shape instanceof RoundRectangle2D) {
shape = new RoundRectangle2D.Double(0, 0, getSize().width - 1,
getSize().height - 1, 50, 50);
}
if (shape instanceof Rectangle2D) {
shape = new Rectangle2D.Double(0, 0, getSize().width,
getSize().height);
}
Graphics2D g2 = (Graphics2D) g;
if (getModel().isArmed()) {
g2.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g2.setPaint(new GradientPaint(0.0F, 0.0F, Color.white, getSize().width,
getSize().height, g2.getColor(), false)); //hier wird des Farbverlauf gezeichnet
g2.fill(shape);
super.paintComponent(g);
}
protected void paintBorder(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
shape.getBounds().width = getBounds().width + 1;
shape.getBounds().height = getBounds().height + 1;
if(model.isRollover())
{
g2.setColor(Color.CYAN);
}
else
{
g2.setColor(getForeground());
}
g2.draw(shape);
}
public static void main(String[] args) {
JButton button = new RoundButton("Jackpot");
button.setBackground(Color.MAGENTA);
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.yellow);
frame.getContentPane().add(button);
frame.getContentPane().add(new JButton("jackpot"));
frame.getContentPane().setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(150, 70);
frame.setVisible(true);
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.java2d.pipe.SpanShapeRenderer.renderPath(Unknown Source)
at sun.java2d.pipe.SpanShapeRenderer.fill(Unknown Source)
at sun.java2d.pipe.ValidatePipe.fill(Unknown Source)
at sun.java2d.SunGraphics2D.fill(Unknown Source)
at RoundButton.paintComponent(RoundButton.java:46)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
yo hab die .jar bei freundin auf pc geguckt, dein button sieht sch...recklich aus :wink: ich denke mal solche Buttons sollte man eher in einem guten Grafikprogramm erstellen als .png sieht besser aus als selberzeichnen mit der 2DGraphics Klasse, denke zumindest ich...@pelle bei mir gehts ohne probleme www.java-forum.org/de/userfiles/user10343/button.jar musst das fenster noch ein bischen größer ziehen