晨时婧蜜

导航

java第六次作业

package com.lzw;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class Demo extends JFrame {

private final class ButtonActionListener implements
ActionListener {
private final ImagePanel imagePanel;
private Thread imageThread;


private ButtonActionListener(ImagePanel imagePanel) {
this.imagePanel = imagePanel;
}

public void actionPerformed(final ActionEvent e) {

if (imageThread == null || !imageThread.isAlive()) {
imageThread = new Thread(imagePanel);
imageThread.start();
} else if (!imageThread.isAlive()) {
imageThread.start();
}
}
}


public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Demo frame = new Demo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


public Demo() {
super();
setTitle("抽奖大转盘");
setResizable(false);
setBounds(100, 100, 700, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final ImagePanel imagePanel = new ImagePanel();

getContentPane().add(imagePanel, BorderLayout.CENTER);
final JButton button = new JButton();
button.setCursor(Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR));
button.setPressedIcon(new ImageIcon(getClass()
.getResource("bt2.png")));
button.setFocusPainted(false);
button.setBorderPainted(false);
button.addActionListener(new ButtonActionListener(
imagePanel));

button.setIcon(new ImageIcon(getClass().getResource(
"bt.png")));
button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorder(null);
button.setBounds(277, 202, 139, 209);
imagePanel.add(button);
}
}

 

package com.lzw;

import java.awt.*;
import java.net.URL;

import javax.swing.JPanel;

/**
*
*/
public class ImagePanel extends JPanel implements Runnable {
private static Image image;
private static Image rotateIcon;
private double angle = 0;
private static Toolkit tk = Toolkit.getDefaultToolkit();

public ImagePanel() {
URL bgUrl = getClass().getResource("bg.jpg"); 
URL rotateUrl = getClass().getResource("rotate.png");

image = tk.createImage(bgUrl);
rotateIcon = tk.createImage(rotateUrl);
setOpaque(false);
setLayout(null);
}

protected void paintComponent(Graphics g) {
int width = getWidth();
int height = getHeight();
if (image != null) {
g.drawImage(image, 0, 0, width, height, this);
}
Graphics2D g1 = (Graphics2D) g.create();

g1.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
if (rotateIcon != null) {
int iconWidth = rotateIcon.getWidth(this);
int iconHeight = rotateIcon.getHeight(this);

g1.rotate(Math.toRadians(angle), width / 2,
height / 2);
g1.drawImage(rotateIcon, width / 2 - iconWidth / 2,
height / 2 - iconHeight / 2, this);
}
}

int count = 0;
int temp = 0;
int randNum = 0;

@Override
public void run() {
double stAngle = Math.random() * 360;
angle = stAngle;
while (angle < stAngle + 1200) {
angle += 6;
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
double sleepTime = 10;
while (sleepTime < 90) {
angle += 6;
repaint();
try {
Thread.sleep((int) (sleepTime += 0.5));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

 

posted on 2016-05-08 13:38  晨时婧蜜  阅读(154)  评论(0编辑  收藏  举报