• 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();
    }
    }
    }
    }

  • 相关阅读:
    chrome下不支持select里面的option单击事件!
    实现自适应宽度圆角按钮的方法
    jQuery分析(取DOM元素)
    字符串转换成JSON
    限制数量不可为0,且不大于1000
    计算用字符串表示的整数四则运算的值
    计算用字符串表示的个位数四则运算的值(栈)
    螺旋数组
    Joseph问题(循环链表)
    C/C++计算阶乘n!末尾所含0的个数
  • 原文地址:https://www.cnblogs.com/ZC962464/p/5470476.html
Copyright © 2020-2023  润新知