簡體   English   中英

使用Java擺動從一個點移動到另一個

[英]Moving from point to point using java swings

我為對象創建了一個代碼,可以從一個點移動到另一個點。 現在我需要一件事的幫助。 條件是程序應該提示用戶輸入問題,如果用戶正確回答了該問題,則該點應該移動,否則該點應該不移動,並且應該顯示下一個問題,直到所有10個問題都結束為止。 如果用戶到達另一端,則他贏了,否則他輸了。 這是點到點移動的代碼。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {

        new Test();

    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

            }
        });
    }

    public class TestPane extends JPanel {

        private Long startTime;
        private long playTime = 2000;

        private Point startPoint, endPoint;
        private Point pointOnTimeLine;
        private double pointInTime; // For rendering...

        public TestPane() {

            startPoint = new Point(0, 95);
            endPoint = new Point(190, 95);
            pointOnTimeLine = new Point(startPoint);
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {


                    if (startTime == null) {

                        startTime = System.currentTimeMillis();
                    }
                    long now = System.currentTimeMillis();
                    long diff = now - startTime;
                    if (diff >= playTime) {
                        diff = playTime;
                        ((Timer) e.getSource()).stop();
                    }
                    double i = (double) diff / (double) playTime;
                    pointInTime = i;

                    pointOnTimeLine.x = (int) (startPoint.x + ((endPoint.x - startPoint.x) * i));
                    pointOnTimeLine.y = (int) (startPoint.y + ((endPoint.y - startPoint.y) * i));


                    repaint();
                }
            });

            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.fill(new Ellipse2D.Double(startPoint.x, startPoint.y, 10, 10));
            g2d.fill(new Ellipse2D.Double(endPoint.x, endPoint.y, 10, 10));
            g2d.setColor(Color.GREEN);
            g2d.fill(new Ellipse2D.Double(pointOnTimeLine.x, pointOnTimeLine.y, 10, 10));
            g2d.dispose();
        }

    }

}

這是我用來獲取用戶輸入的代碼

import javax.swing.*;
public class input1
{
  public static void main(String args[])
  {
    String str1 = JOptionPane.showInputDialog("france has how many varieties of cheese ? a: 100 b: 200 c: 250");
if(str1.equals("c"))
{
JOptionPane.showMessageDialog( null, "good job ", "answer", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog( null, "sorry try again ", "answer", JOptionPane.INFORMATION_MESSAGE);
}
}
}

請幫幫我。 提前致謝

import javax.swing.*;
import java.awt.*;

public class td {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Train Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 400);
        frame.setLocationRelativeTo(null);
        frame.add(new TrainCanvas());
        frame.setVisible(true);
    }

}

class TrainCanvas extends JComponent {

    private int lastX = 0;

    public TrainCanvas() {
        Thread animationThread = new Thread(new Runnable() {
            public void run() {
while(true)
{
String str1 = JOptionPane.showInputDialog("france has how many varieties of cheese ? a: 100 b: 200 c: 250");
if(str1.equals("c"))
{       

                    repaint();
                    try {Thread.sleep(100);} catch (Exception ex) {}  
String str2 = JOptionPane.showInputDialog("where did noodles originate ? a: India b: Italy c: China");
if(str2.equals("b"))
{       

                    repaint();
                    try {Thread.sleep(100);} catch (Exception ex) {}     
String str3 = JOptionPane.showInputDialog("which place is famous for good chocolate ? a: switzerland b: Germany c: China");
if(str3.equals("a"))
{       

                    repaint();
                    try {Thread.sleep(100);} catch (Exception ex) {}   
String str4 = JOptionPane.showInputDialog("what is the staple food of India ? a: rice b: bread c: roti");
if(str4.equals("a"))
{       

                    repaint();
                    try {Thread.sleep(100);} catch (Exception ex) {}   
String str5 = JOptionPane.showInputDialog("where did ice cream originate ? a: greenland b: china c: korea");
if(str5.equals("b"))
{       

                    repaint();
                    try {Thread.sleep(100);} catch (Exception ex) {}
JOptionPane.showMessageDialog( null, "GOOD JOB!!!   LEVEL COMPLETE ", "answer", JOptionPane.INFORMATION_MESSAGE); 
break;
}
else
{
JOptionPane.showMessageDialog( null, "GAME OVER  (restarting...) ", "answer", JOptionPane.INFORMATION_MESSAGE);

} 
} 
else
{
JOptionPane.showMessageDialog( null, "GAME OVER  (restarting...) ", "answer", JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog( null, "GAME OVER  (restarting...) ", "answer", JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog( null, "GAME OVER  (restarting...)", "answer", JOptionPane.INFORMATION_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog( null, "GAME OVER  (restarting...)", "answer", JOptionPane.INFORMATION_MESSAGE);
}


            }
}
        });

 animationThread.start();

 }

    public void paintComponent(Graphics g) {
        Graphics2D gg = (Graphics2D) g;

        int w = getWidth();
        int h = getHeight();

        int trainW = 100;
        int trainH = 10;
        int trainSpeed = 30;

        int x = lastX + trainSpeed;

        gg.setColor(Color.BLACK);
        gg.fillRect(x, h/2 + trainH, trainW, trainH);

      lastX = x;
    }

}

還是謝謝你的幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM