Java 在用线程按下按钮时使两个轮盘旋转

问题描述 投票:0回答:2

这是名为 MainWheel 的主脚本 该脚本有六个类 MainWheel - 主脚本 - 创建两个轮子的列表 SelectionWheel - 调用轮子 Tick - 由轮子调用 Tick1 - 由 wheel1 调用 Wheel - 由 SelectionWheel 调用并创建 wheal Wheel1 - 由 SelectionWheel 调用并创建 wheal1

public static void main(String[] args) throws Exception {
   
    int width = 1000, height = 1000;
   
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    ArrayList<String> list = new ArrayList<String>();
    list.add("Real Madrid");
    list.add("Barcelona");
    list.add("PSG");
    list.add("Juventus");
    list.add("Milan");
    list.add("Benfica");
    list.add("Sporting");
    list.add("Bayern");
    list.add("Man City");
    list.add("Chelsea");
    list.add("Liverpol");
    list.add("Borussia Dortmond");
    list.add("Arsenal");
    list.add("Atético de Madrid");
    list.add("Napoli");
    list.add("Porto");
   
   
    SelectionWheel wheel= new SelectionWheel(list);
    wheel.hasBorders(true, false);
    wheel.setBounds(10, 30, 350, 350);
   
    JButton rodar = new JButton("Rodar");
    rodar.setBounds(375, 10, 100, 40);
    rodar.setBackground(Color.black);
    rodar.setForeground(Color.yellow);
   
   
    JLabel titulo = new JLabel("Equipa 1");
    titulo.setBounds(150, 10, 100, 20);
    frame.add(titulo);
    frame.add(wheel);
    frame.add(rodar);
    frame.setSize(width, height);
    frame.setLayout(null);
    frame.setVisible(true);
   
    JFrame frame1 = new JFrame();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    ArrayList<String> list1 = new ArrayList<String>();

    list1.add("Leverkusen");
    list1.add("Marseille");
    list1.add("Ajax");
    list1.add("Rangers");
    list1.add("Club Brugge");
    list1.add("Inter");
    list1.add("Frankfurt");
    list1.add("Dinamo Zagreb");
    list1.add("Salzburg");
    list1.add("Leipzig");
    list1.add("Shaktar");
    list1.add("Celtic");
    list1.add("Sevilha");
    list1.add("Copenhaga");
    list1.add("PSV");
    list1.add("Dynamo Kiev");
    list1.add("Zenit");
    list1.add("Monaco");
   
    SelectionWheel wheel1 = new SelectionWheel(list1);
    wheel1.hasBorders(true, false);
    wheel1.setBounds(500, 30, 350, 350);
    JLabel titulo1 = new JLabel("Equipa 2");
    titulo1.setBounds(650, 10, 100, 20);
    frame.add(titulo1);
    frame.add(wheel1);
    frame.setSize(width, height);
    frame.setLayout(null);
    frame.setVisible(true);
   
    JButton spinButton = new JButton("Spin");
    spinButton.setBounds(375, 70, 100, 40);
    spinButton.setBackground(Color.black);
    spinButton.setForeground(Color.yellow);
    
    //Wheel minhaThread = new Wheel(list);
    //minhaThread.Wheel();
    
    //roleta2 minhaThread1 = new roleta2();

    
    spinButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //minhaThread.start();
        }
    });

    frame.add(spinButton);
    
    
    
    while(true) {
        // wait for action
        while(true)
        {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(wheel.isSpinning())
                break;
            if(wheel.isSpinning())
                break;
        }
       
        while(true) {
            // wait for action
            while(true)
            {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(wheel1.isSpinning())
                    break;
                if(wheel1.isSpinning())
                    break;
            }
           
        // while spinning
        while(wheel1.isSpinning())
        {
            //lblsel.setText(wheel.getSelectedString());
            //lblang.setText(Double.toString(wheel.getRotationAngle()));
            //lblsp.setText(Double.toString(wheel.getSpinSpeed()));
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //lblsp.setText(Double.toString(wheel.getSpinSpeed()));
        // show selection
        JOptionPane.showMessageDialog(frame, "Selection 1: " + wheel.getSelectedString());
       
        while(wheel1.isSpinning())
        {
            //lblsel1.setText(wheel1.getSelectedString1());
            //lblang1.setText(Double.toString(wheel1.getRotationAngle1()));
            //lblsp1.setText(Double.toString(wheel1.getSpinSpeed1()));
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //lblsp1.setText(Double.toString(wheel1.getSpinSpeed1()));
        // show selection
        JOptionPane.showMessageDialog(frame1, "Selection: 2" + wheel1.getSelectedString());
       
    }
}

如果单击“spinButton”使两个轮盘同时开始旋转,有人可以帮助我吗?作为回应,我从脚本中添加了其他类。

java multithreading eclipse button java-threads
2个回答
0
投票

这是 SelectionWheel 类

public class SelectionWheel extends JPanel {

Wheel _wheel;
Wheel _wheel1;
Tick _tick;
Tick _tick1;

@Override
public void setBounds(int x, int y, int width, int height) {
/*
* Adjust the bounds of the wheel and tick based on tick width.
*/
super.setBounds(x, y, width, height);
_wheel.setBounds(0, 0, width - _tick.getTickWidth(), height);
_tick.setBounds(width - _tick.getTickWidth(), 0, _tick.getTickWidth(), height);

super.setBounds(x, y, width, height);
_wheel1.setBounds(0, 0, width - _tick1.getTickWidth(), height);
_tick1.setBounds(width - _tick1.getTickWidth(), 0, _tick1.getTickWidth(), height);
}

public void hasBorders(boolean borders, boolean borders1) {
/*
* Check if the wheel borders are on.
*/
_wheel.hasBorders(borders);
_wheel1.hasBorders(borders1);
}

public int getRadius() {
/*
* Get radius of the wheel.
*/
return _wheel.getRadius();
}

public int getRadius1() {
/*
* Get radius of the wheel.
*/
return _wheel1.getRadius();
}

public double getRotationAngle() {
/*
* Get current rotation angle of the wheel.
*/
return _wheel.getRotationAngle();
}

public double getRotationAngle1() {
/*
* Get current rotation angle of the wheel.
*/
return _wheel1.getRotationAngle();
}

public void setRotationAngle(double rotationAngle, double rotationAngle1) {
/*
* Set current rotation angle of the wheel.
*/
_wheel.setRotationAngle(rotationAngle);
}

public void setRotationAngle1(double rotationAngle1) {
/*
* Set current rotation angle of the wheel.
*/
_wheel1.setRotationAngle(rotationAngle1);
}

public Font getWheelFont() {
/*
* Get current font of the wheel.
*/
return _wheel.getFont();
}

public Font getWheelFont1() {
/*
* Get current font of the wheel.
*/
return _wheel1.getFont();
}

public void setWheelFont(Font font) {
/*
* Set current font of the wheel.
*/
super.setFont(font);
_wheel.setFont(font);
}

public void setWheelFont1(Font font1) {
/*
* Set current font of the wheel.
*/
super.setFont(font1);
_wheel1.setFont(font1);
}

public ArrayList<String> getListOfStrings() {
/*
* Get the list of strings for the wheel.
*/
return _wheel.getListOfStrings();
}

public ArrayList<String> getListOfStrings1() {
/*
* Get the list of strings for the wheel.
*/
return _wheel1.getListOfStrings();
}

public void setListOfStrings(ArrayList<String> list) throws Exception {
/*
* Set the list of strings for the wheel.
*/
_wheel.setListOfStrings(list);
}

public void setListOfStrings1(ArrayList<String> list1) throws Exception {
/*
* Set the list of strings for the wheel.
*/
_wheel1.setListOfStrings(list1);
}

public double getSpinSpeed() {
/*
* Get current spin speed of the wheel.
*/
return _wheel.getSpinSpeed();
}

public double getSpinSpeed1() {
/*
* Get current spin speed of the wheel.
*/
return _wheel1.getSpinSpeed();
}

public double getMaxSpinSpeed() {
/*
* Get current spin speed limit of the wheel.
*/
return _wheel.getMaxSpinSpeed();
}

public double getMaxSpinSpeed1() {
/*
* Get current spin speed limit of the wheel.
*/
return _wheel1.getMaxSpinSpeed();
}

public void setMaxSpinSpeed(double speed) {
/*
* Set current spin speed limit of the wheel.
*/
_wheel.setMaxSpinSpeed(speed);
}

public void setMaxSpinSpeed1(double speed) {
/*
* Set current spin speed limit of the wheel.
*/
_wheel1.setMaxSpinSpeed(speed);
}

public double getSpinDeceleration() {
return _wheel.getSpinDeceleration();
}

public double getSpinDeceleration1() {
return _wheel1.getSpinDeceleration();
}

public void setSpinDeceleration(double deceleration) throws Exception {
_wheel.setSpinDeceleration(deceleration);
}

public void setSpinDeceleration1(double deceleration) throws Exception {
_wheel1.setSpinDeceleration(deceleration);
}

public ArrayList<Color> getColorScheme() {
/*
* Get color scheme of the wheel.
*/
return _wheel.getColorScheme();
}

public ArrayList<Color> getColorScheme1() {
/*
* Get color scheme of the wheel.
*/
return _wheel1.getColorScheme();
}

public void setColorScheme(ArrayList<Color> colors) {
/*
* Set color scheme of the wheel.
*/
_wheel.setColorScheme(colors);
}

public void setColorScheme1(ArrayList<Color> colors1) {
/*
* Set color scheme of the wheel.
*/
_wheel1.setColorScheme(colors1);
}

public void addColor(Color color) {
/*
* Add new color to the color scheme of the wheel.
*/
_wheel.addColor(color);
}

public void addColor1(Color color) {
/*
* Add new color to the color scheme of the wheel.
*/
_wheel1.addColor(color);
}

public String getSelectedString() {
/*
* Get current string selection for the wheel.
*/
return _wheel.getSelectedString();
}

public String getSelectedString1() {
/*
* Get current string selection for the wheel.
*/
return _wheel1.getSelectedString();
}

public boolean isSpinning() {
/*
* Check if wheel is spinning.
*/
return _wheel.isSpinning();
}

public boolean isSpinning1() {
/*
* Check if wheel is spinning.
*/
return _wheel1.isSpinning();
}

public void setShape(Wheel.Shape shape) {
/*
* Set shape of the wheel.
*/
_wheel.setShape(shape);
}

public void setShape1(Wheel.Shape shape) {
/*
* Set shape of the wheel.
*/
_wheel1.setShape(shape);
}

public double getTickWidth() {
/*
* Get tick width.
*/
return _tick.getTickWidth();
}

public double getTickWidth1() {
/*
* Get tick width.
*/
return _tick1.getTickWidth();
}

public void setTickWidth(int width) {
/*
* Set tick width. Resets the bounds of both tick and wheel.
*/
_tick.setTickWidth(width);
this.setBounds(this.getX(), this.getY(), this.getWidth(), this.getHeight());
}

public void setTickWidth1(int width) {
/*
* Set tick width. Resets the bounds of both tick and wheel.
*/
_tick1.setTickWidth(width);
this.setBounds(this.getX(), this.getY(), this.getWidth(), this.getHeight());
}

public double getTickHeight() {
/*
* Get tick height.
*/
return _tick.getTickHeight();
}

public double getTickHeight1() {
/*
* Get tick height.
*/
return _tick1.getTickHeight();
}

public void setTickHeight(int height) {
/*
* Set tick height.
*/
_tick.setTickHeight(height);
}

public void setTickHeight1(int height) {
/*
* Set tick height.
*/
_tick1.setTickHeight(height);
}

public Polygon getTickPolygon() {
/*
* Get tick polygon.
*/
return _tick.getPolygon();
}

public Polygon getTickPolygon1() {
/*
* Get tick polygon.
*/
return _tick1.getPolygon();
}

public void setTickPolygon(Polygon polygon) {
/*
* Set tick polygon.
*/
_tick.setPolygon(polygon);
}

public void setTickPolygon1(Polygon polygon) {
/*
* Set tick polygon.
*/
_tick1.setPolygon(polygon);
}

public SelectionWheel(ArrayList<String> listOfStrings) throws Exception {
_wheel = new Wheel(listOfStrings);
_wheel.setLayout(null);
_tick = new Tick();
_tick.setLayout(null);
this.setLayout(null);
this.add(_wheel);
this.add(_tick);

_wheel1 = new Wheel(listOfStrings);
_wheel1.setLayout(null);
_tick1 = new Tick();
_tick1.setLayout(null);
this.setLayout(null);
this.add(_wheel1);
this.add(_tick1);
}


public void spinStartAsync(double speed, int direction, double deceleration) throws Exception {
_wheel.spinStartAsync(speed, direction, deceleration);
}

public void spinStartAsync1(double speed, int direction, double deceleration) throws Exception {
_wheel1.spinStartAsync(speed, direction, deceleration);
}

public void spinStop() {
/*
* Stop spinning.
*/
_wheel.spinStop();
}
public void spinStop1() {
/*
* Stop spinning.
*/
_wheel1.spinStop();
}
}

0
投票

这是滴答声 @SuppressWarnings("串行") 公共类 Tick 扩展 JPanel {

private Polygon _polygon_orig = null;
private Polygon _polygon = null;

private int _tickWidth = 20;
private int _tickHeight = 20;

public int getTickWidth() {
/*
* Get tick width.
*/
return _tickWidth;
}

public void setTickWidth(int width) {
/*
* Set tick width.
*/
_tickWidth = width;
}

public int getTickHeight() {
/*
* Get tick height.
*/
return _tickHeight;
}

public void setTickHeight(int height) {
/*
* Set tick height.
*/
_tickHeight = height;
}

public Polygon getPolygon() {
/*
* Get polygon shape of the tick.
*/
return _polygon;
}

public void setPolygon(Polygon polygon) {
/*
* Set polygon shape of the tick.
*/
_polygon_orig = polygon;
_polygon = polygon;
adjustPolygon();
this.repaint();
}

public Tick() {
super();
this.repaint();
}

private void adjustPolygon()
{
/*
* Adjust the size and position of the custom polygon shape of the tick.
*/
int i;
// calculate width/height of the polygon
int xmax = Integer.MIN_VALUE, xmin = Integer.MAX_VALUE;
int ymax = xmax, ymin = xmin;
for(i = 0; i < _polygon.xpoints.length; i++)
{
if(_polygon.xpoints[i]>xmax) xmax = _polygon.xpoints[i];
if(_polygon.xpoints[i]<xmin) xmin = _polygon.xpoints[i];
}
for(i = 0; i < _polygon.ypoints.length; i++)
{
if(_polygon.ypoints[i]>ymax) ymax = _polygon.ypoints[i];
if(_polygon.ypoints[i]<ymin) ymin = _polygon.ypoints[i];
}
int width = xmax - xmin;
// scale polygon
double factor = (double)this.getWidth() / width;
for(i = 0; i < _polygon.xpoints.length; i++)
{
_polygon.xpoints[i] *= factor;
_polygon.ypoints[i] *= factor;
}
// calculate center of polygon
int centerX = 0, centerY = 0;
for(i = 0; i < _polygon.xpoints.length; i++)
{
centerX += _polygon.xpoints[i];
}
centerX /= _polygon.xpoints.length;
for(i = 0; i < _polygon.ypoints.length; i++)
{
centerY += _polygon.ypoints[i];
}
centerY /= _polygon.ypoints.length;
// translate polygon to center of the panel
_polygon.translate(this.getWidth() / 2 - centerX, this.getHeight() / 2 - centerY);
}

private Polygon getTriangle() {
/*
* Get triangle polygon - default shape of the tick.
*/
Polygon polygon = new Polygon();
polygon.addPoint(0, this.getHeight() / 2);
polygon.addPoint(this.getWidth(), (int)(this.getHeight() / 2 - this.getWidth() * Math.tan(Math.toRadians(30))));
polygon.addPoint(this.getWidth(), (int)(this.getHeight() / 2 + this.getWidth() * Math.tan(Math.toRadians(30))));
return polygon;
}

@Override
public void paintComponent(Graphics g)
{
/*
* Paintcomponent.
* If custom polygon is not set, use default triangle.
*/
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON
);
g2d.addRenderingHints(rh);

if(_polygon_orig == null)
_polygon = getTriangle();
else
adjustPolygon();
g2d.fillPolygon(_polygon);
}
}
© www.soinside.com 2019 - 2024. All rights reserved.