使用java swings从一个点移动到另一个点

时间:2015-11-28 15:26:05

标签: java graphics awt swingx

我已经创建了一个对象的代码,可以从一个点移动到另一个点。现在我需要帮助一件事。条件是程序应该提示用户提出问题,如果用户正确地回答了问题,那么点应该移动,否则点不应该移动,它应该显示下一个问题,这将持续到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);
}
}
}

请帮帮我。提前谢谢

1 个答案:

答案 0 :(得分:1)

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;
    }

}

感谢你的帮助......