我实现了一个时钟,其中秒针和分针扩展了相同的组件JClockHand。我有这样的执行, 但这不是我想要的。我想知道是否有任何可能的方法来设置Graphics z索引重叠不同的图形?提前谢谢。
sechand.png,https://app.box.com/s/pwwxxl9g0mparsg5fxghxwvtjjd58b25
minhand.png,https://app.box.com/s/mgwgx4odjexgt7qhyxr1tbcp68lkpy6x
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ClockFrame extends JFrame {
public ClockFrame(){
JPanel panel = new JPanel();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(panel);
setSize(1000, 1000);
panel.setSize(getWidth(), getHeight());
panel.setLayout(new GridLayout());
setVisible(true);
setResizable(false);
panel.setBackground(Color.BLACK);
SecondHand sHand=new SecondHand("./res/icon/sechand.png");
panel.add(sHand);
MinuteHand mHand=new MinuteHand("./res/icon/minhand.png");
panel.add(mHand);
pack();
setVisible(true);
}
//start here
public class JClockHand extends JComponent{
protected Dimension preferredSize=new Dimension(1000,1000);
protected BufferedImage secImage;
public void setIconPath(String iconPath){
try {
secImage = ImageIO.read(new File(iconPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void rotate(Graphics2D g2d,BufferedImage img,double angleRad){
double middle = preferredSize.getWidth() / 2d;
g2d.translate(middle, middle);
AffineTransform at = AffineTransform.getTranslateInstance(middle, middle);
at.rotate(angleRad);
g2d.setTransform(at);
g2d.drawImage(img, 0, 0, this);
}
public Dimension getPreferredSize() {
return preferredSize;
}
}
class SecondHand extends JClockHand{
private Timer timer;
private Calendar currentTime;
public SecondHand(String iconPath){
currentTime = Calendar.getInstance();
setIconPath(iconPath);
timer = new Timer(50, new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentTime.setTime(new Date(System.currentTimeMillis()));
repaint();
}
});
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
double angle = (currentTime.get(Calendar.SECOND)*2*Math.PI)/60d;
rotate(g2d,secImage,angle);
g2d.dispose();
}
}
public class MinuteHand extends JClockHand{
private Timer timer;
private Calendar currentTime;
public MinuteHand(String iconPath) {
currentTime = Calendar.getInstance();
setIconPath(iconPath);
timer = new Timer(50, new ActionListener() {
public void actionPerformed(ActionEvent e) {
currentTime.setTime(new Date(System.currentTimeMillis()));
repaint();
}
});
timer.start();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
double angle = (currentTime.get(Calendar.MINUTE)*2*Math.PI)/60d;
rotate(g2d,secImage,angle);
g2d.dispose();
}
}
public static void main(String[] a) {
ClockFrame c = new ClockFrame();
}
}
答案 0 :(得分:1)
使用JLayeredPane。喜欢这个
public ClockFrame(){
int frameWidth = 1000;
int frameHeight = 1000;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(frameWidth, frameHeight));
setLayout(new BorderLayout());
JLayeredPane pane = new JLayeredPane();
pane.setOpaque(true);
pane.setBackground(Color.BLACK);
add(pane);
SecondHand sHand=new SecondHand("sechand.png");
MinuteHand mHand=new MinuteHand("minhand.png");
mHand.setBounds(0, 0, frameWidth, frameHeight);
sHand.setBounds(0, 0, frameWidth, frameHeight);
pane.add(mHand, 1);
pane.add(sHand, 2);
pack();
setResizable(false);
setVisible(true);
}
您必须手动设置JLayeredPane组件的位置和大小,因为JLayeredPane不是LayoutManager