java绘制音乐播放按钮,用于基本音乐播放器的Java Swing绘图PlayButton

Maybe my case it's a simple confusion of ideas. How do draw a button like this using Shape?

CVARP.jpg

I don't mind the rounded corners, heres my aproach for a round corner button.

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Shape;

import java.awt.geom.RoundRectangle2D;

import javax.swing.AbstractButton;

import javax.swing.ButtonModel;

import javax.swing.JComponent;

import javax.swing.plaf.basic.BasicButtonUI;

public class PlayButtonUI extends BasicButtonUI{

protected Shape shape;

@Override

protected void installDefaults(AbstractButton b) {

super.installDefaults(b);

b.setOpaque(false);//removes that annoying default background

}

@Override public void paint(Graphics g, JComponent c) {

Graphics2D g2 = (Graphics2D)g;

AbstractButton b = (AbstractButton) c;

ButtonModel model = b.getModel();

drawButtonShape(b);

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);//smoth borders

if(model.isArmed()) {

g2.setColor(Color.RED);//color when button is pressed

}else{

g2.setColor(Color.GREEN);//default button color

}

g2.fill(shape);//aplying color

super.paint(g2, c);

}

private void drawButtonShape(JComponent c) {

//button shape is drawn here, 16 are the border radius

shape = new RoundRectangle2D.Float(0, 0, c.getWidth()-1, c.getHeight()-1,16, 16);

}

}

I don't really know how to draw anything at all, this class was a result from a chaotic example that i found somewhere, and then simplified by myself until it just worked, i left some comments for the important lines.

I've been looking for a while, and found this example in oracle docs.

https://docs.oracle.com/javase/tutorial/2d/geometry/arbitrary.html

I don't really know how to convert Graphics2D to Shape, please tell me if i'm taking the wrong way.

解决方案

So, I've spent the better of the day banging my head against this problem, trying to do a whole bunch of trig magic ... I can't even do simple card tricks :P

Then I realised, there are other tricks I could do...

oedlh.png

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Shape;

import java.awt.geom.GeneralPath;

import javax.swing.AbstractButton;

import javax.swing.ButtonModel;

import javax.swing.JButton;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

import javax.swing.plaf.basic.BasicButtonUI;

public class Test {

public static void main(String[] args) {

new Test();

}

public Test() {

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run() {

JFrame frame = new JFrame();

JButton btn = new JButton();

btn.setUI(new PlayButtonUI());

frame.add(btn);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

});

}

public class PlayButtonUI extends BasicButtonUI {

@Override

public Dimension getPreferredSize(JComponent c) {

return new Dimension(200, 200);

}

@Override

public void paint(Graphics g, JComponent c) {

Graphics2D g2 = (Graphics2D) g;

AbstractButton b = (AbstractButton) c;

ButtonModel model = b.getModel();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//smoth borders

if (model.isArmed()) {

g2.setColor(Color.BLACK);//color when button is pressed

} else {

g2.setColor(Color.GRAY);//default button color

}

float thinkness = Math.min(c.getWidth(), c.getHeight()) * 0.1f;

Shape shape = shapeFor(c, thinkness);

g2.setStroke(new BasicStroke(thinkness, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

g2.fill(shape);//aplying color

g2.draw(shape);

super.paint(g2, c);

}

private Shape shapeFor(JComponent c, float thickness) {

GeneralPath gp = new GeneralPath();

double width = c.getWidth();

double height = c.getHeight();

double vPos = height / 2.0;

double hPos = width - thickness;

gp.moveTo(0.0 + thickness, 0.0 + thickness);

gp.lineTo(hPos, vPos);

gp.lineTo(0.0 + thickness, height - thickness);

gp.closePath();

return gp;

}

}

}

So, this is a slight "cheat". What this actually does is uses the properties of the Stroke to generate rounded edges, rather than trying to use curveTo or compound shapes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的Java Swing音乐播放器示例。以下是基本的代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.IOException; import javax.sound.sampled.*; public class MusicPlayer extends JFrame implements ActionListener { private JButton playButton, pauseButton, stopButton; private Clip clip; private File file; public MusicPlayer() { setTitle("Music Player"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(); playButton = new JButton("Play"); playButton.addActionListener(this); buttonPanel.add(playButton); pauseButton = new JButton("Pause"); pauseButton.addActionListener(this); buttonPanel.add(pauseButton); stopButton = new JButton("Stop"); stopButton.addActionListener(this); buttonPanel.add(stopButton); add(buttonPanel, BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == playButton) { if (clip == null || !clip.isActive()) { try { file = new File("path-to-your-music-file"); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file); clip = AudioSystem.getClip(); clip.open(audioInputStream); clip.start(); } catch (LineUnavailableException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch (UnsupportedAudioFileException ex) { ex.printStackTrace(); } } } else if (e.getSource() == pauseButton) { if (clip != null && clip.isActive()) { clip.stop(); } } else if (e.getSource() == stopButton) { if (clip != null) { clip.stop(); clip.setFramePosition(0); } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new MusicPlayer(); } }); } } ``` 在上面的代码中,我们创建了一个名为MusicPlayer的Swing窗口。窗口包含三个按钮:播放、暂停和停止。当用户单击播放按钮时,我们使用Java Sound API中的Clip类打开音频文件并播放它。当用户单击暂停按钮时,我们停止正在播放的音频文件。当用户单击停止按钮时,我们停止正在播放的音频文件并将其位置重置为0。 请注意,您需要将“path-to-your-music-file”替换为您自己的音乐文件路径。 希望这个示例对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值