3D-Effekt

Status
Nicht offen für weitere Antworten.

foobar

Top Contributor
Hi,
ich brauche ein Widget das aussieht wie eine Leuchtdiode. Wie bekomme ich denn diesen 3D-Effekt hin, damit das Widget nicht wie ein Farbklecks aussieht sondern optisch etwas ansprechend ist. Ausserdem ist der Rand etwas ausgefranst. Wie bekommt man das weg?
Im Moment erbe ich einfach von JComponent und habe in der paintComponent-Methode mit drawOval einen Kreis gezeichnet.

Viele Grüße
foobar
 

tux2323

Mitglied
Mein Vorschlag
led.jpg

LED als Bild
Code:
public class Led extends Button implements ActionListener{
	
	private static final long serialVersionUID = 1L;

	private final static Image redLed = Toolkit.getDefaultToolkit().getImage( "icon/LEDRed.jpg" );
	
	private final static Image greenLed = Toolkit.getDefaultToolkit().getImage( "icon/LEDGreen.jpg" );;
	
	private Image image;
	
	private boolean isActive = false;
	
	public Led(){
		init();
	}

	private void init() {
		image = redLed;
		setPreferredSize(new Dimension(34,100));
		addActionListener(this);
		repaint();
	}

	private void action() {
		if(isActive) image = greenLed;
		else image = redLed;
		repaint();
	}

	public void paint(Graphics g) {
		Dimension d = getSize();
		g.setColor(Color.WHITE);
		g.fillRect( 0, 0, d.width, d.height );
		int x = ( d.width - 34) / 2;
		int y = ( d.height - 100 ) / 2;
		g.drawImage( image,x,y,this);
	}
	
	public void setActive(boolean b){
		isActive = b;
		action();
	}
	
	public boolean isActive(){
		return isActive;
	}

	public void actionPerformed(ActionEvent e) {
		setActive(!isActive);
	}
	
}
Beipiel Programm ...
Code:
public class MainFrame extends JFrame {
	
	public MainFrame(){
		super();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().add(new Led(),BorderLayout.NORTH);
		getContentPane().add(new Led(),BorderLayout.CENTER);
		getContentPane().add(new Led(),BorderLayout.SOUTH);
		pack();
	}
	
	public static void main(String[] args) {
		MainFrame frame = new MainFrame();
		frame.setVisible(true);
	}

}
 

foobar

Top Contributor
So ich habe jetzt eine Lösung gefunden:
Code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class IlluminatingDiode extends JComponent
{
        private final static Color COLOR_ACTIVE    = Color.GREEN;
        private final static Color COLOR_INACTIVE = Color.RED;
        private final static int SIZE                       = 20;
        private boolean active                             = false;
        
        public IlluminatingDiode()
        {
            Dimension dim = new Dimension(SIZE+1 , SIZE+1);
            setSize( dim );
            setPreferredSize( dim );
            setMinimumSize( dim );
        }
        
        public void setActive(boolean active)
        {
            this.active = active;
            repaint();
        }
        
        @Override
        protected void paintComponent(Graphics g)
        {
            Color currentColor = active ? COLOR_ACTIVE : COLOR_INACTIVE;
            Shape circle         = new Ellipse2D.Float(0,0,SIZE, SIZE);
            
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setColor( currentColor );
            
            g2.setPaint(new GradientPaint(0,0,Color.WHITE, (SIZE / 2),(SIZE / 2), currentColor));
            g2.fill( circle );
            
            g2.setStroke(new BasicStroke(0.5f));
            g2.setColor(Color.BLACK);
            g2.draw( circle );
        }

        public boolean isActive()
        {
            return active;
        }
        
        public static void main(String[] args)
        {
            JFrame f =  new JFrame();
            f.add(new IlluminatingDiode());
            f.setLocationRelativeTo( null );
            f.setSize(200, 200);
            f.setVisible( true );
        }
}

Hat noch jemand Verbesserungsvorschläge?

Viele Grüße
foobar
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben