Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JInternalFrame中的电影_Java_Swing - Fatal编程技术网

Java 在JInternalFrame中的电影

Java 在JInternalFrame中的电影,java,swing,Java,Swing,我正在写一个显示火车运行图的应用程序。使用库JGrpahx在JInternalFrame上添加组件计数,地图本身显示为图形。以下是此框架的代码: public static mxGraph graph = new mxGraph(); public static Object parent = graph.getDefaultParent(); private static boolean shown = false; public TrainMapView() { super("T

我正在写一个显示火车运行图的应用程序。使用库
JGrpahx
JInternalFrame
上添加组件计数,地图本身显示为图形。以下是此框架的代码:

public static mxGraph graph = new mxGraph();
public static Object parent = graph.getDefaultParent();
private static boolean shown = false;


public TrainMapView() {
    super("Train Map",
            true, //resizable
            true, //closable
            true, //maximizable
            true);//iconifiable

    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    graphComponent.setEnabled(false);
    getContentPane().add(graphComponent);

    TrainMoveAnimation trainMoveAnimation = new TrainMoveAnimation();
    trainMoveAnimation.start();

   // getContentPane().add(trainMoveAnimation);

    setSize(500, 500);
    setVisible(true); //necessary as of 1.3
    shown=true;


}

public static boolean isShown() {
    return shown;
}

public Object addVertex(String name, int x, int y) {
    return graph.insertVertex(parent, null, name, x, y, 20, 20, "shape=and");
}

public void addEdge(int price, Object obj1, Object obj2) {
    graph.insertEdge(parent, null, ""+price, obj1, obj2);
    graph.insertEdge(parent, null, "", obj2, obj1);
}

@Override
public void doDefaultCloseAction() {
    shown=false;
    super.doDefaultCloseAction();
}

public TrainMapView update(){
    graph = new mxGraph();
    parent = graph.getDefaultParent();
    TrainMapView mapView = new TrainMapView();
    List<Object> vertexes = new ArrayList<>(TrainMap.getAllStations().size());
    for (Station station: TrainMap.getAllStations()){
        vertexes.add(addVertex(station.getTitle(),station.getX(),station.getY()));
        System.out.println(station.getTitle());
    }
    for (int i = 0; i < 256; i++) {
        for (int j = i+1; j < 256; j++) {
            if (TrainMap.getMatrix()[i][j]>0){
                addEdge(TrainMap.getMatrix()[i][j],vertexes.get(i),vertexes.get(j));
                //addEdge(vertexes.get(j),vertexes.get(i));
            }
        }
    }
    return mapView;
}

这是什么
mxGraphComponent
类?它不能来自专业库,因为它违反Java命名约定。我猜它扩展了JComponent或JPanel,如果是这样,为什么不重写它的paintComponent方法并在其中绘制动画?@HovercraftFullOfEels谢谢你的建议,它帮助了我。但只有当我重写
paint
方法而不重写
paintComponent
时,运动才会启动,它会影响什么吗?另外,检查这里,该库的所有类都以mx开头,我也很惊讶。根据我对API的阅读,mxGraphComponent扩展了JScrollPane,并在其中包含一个
mxGraphComponent.mxGraphControl
对象,即它的视图,正是后一个类从JComponent扩展而来,应该重写它的paintComponent方法。@hoverCraftfullOfels是的,我可以重写paintComponent方法,但由于这种认识,只有在重写了paint方法之后,它才起作用。
public class TrainMoveAnimation extends JPanel{

    public static final String NAME = "Title Game";
    public static final int WIDTH = 490;
    public static final int HEIGHT = 450;
    private Image image;
    private int x;
    private int y;


    public TrainMoveAnimation() {
        loadImage();
    }

    private void loadImage() {
        try {
            image = ImageIO.read(new File("F://train.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(image, x, y, null);
    }

    public static void main(String[] args) {
        TrainMoveAnimation game = new TrainMoveAnimation();
        game.start();

        game.setSize(WIDTH, HEIGHT);
        JFrame frame = new JFrame(NAME);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH, HEIGHT);
        frame.add(game);
        frame.setVisible(true);


    }

    public void start(){
        Timer timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                move();
            }
        });
        timer.start();
    }

    private void move(){
        x+=5;
        repaint();
    }
}