如何移动形状的JFrame [英] How to move JFrame shape

查看:187
本文介绍了如何移动形状的JFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个简单的游戏。本场比赛是关于形状。形状会移动,我们将鼠标赶上。我已经创建了一个椭圆形,椭圆形给出图形大小。但我不能反复移动这个形状。我想我需要使用定时器类。我一直在努力2小时自己,但我没有做呢。

在code;

 进口java.awt.Color中;
进口java.awt.Container中;
进口java.awt.Dimension中;
进口java.awt.Graphics;
进口java.awt.event.ActionEvent中;
进口java.awt.event.ActionListener;
进口java.awt.event.MouseEvent中;
进口java.util.Timer中;
进口java.util.TimerTask中;进口javax.swing.JFrame中;
进口javax.swing.JPanel中;公共类myshapestry扩展JFrame中实现的ActionListener {JFrame的帧=新的JFrame(Deneme);
集装箱升;
私有静态诠释ballX = 150;
私有静态诠释BALLY = 150;
myshapestry(){
     L = this.getContentPane();
     l.setLayout(NULL);
     MyPanel面板=新MyPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(NULL);
        frame.add(面板);
        frame.setVisible(真);
        frame.setSize(GET preferredSize());`` }
 公共尺寸的get preferredSize(){
       返回新的Dimension(500,600);
   }
   公共静态无效的主要(字符串ARGS []){
       myshapestry TR =新myshapestry();
       tr.setTitle(形状游戏);
   }   私有静态类MyPanel继承JPanel {
       保护无效paintComponent(图形G){
           super.paintComponent方法(G);
           g.fillOval(ballX,BALLY,50,70);    }       公共无效的actionPerformed(ActionEvent的五){
          ballX = ballX + 5;
          BALLY BALLY = + 10;
          重绘();
      }       }   }

我是想在myshapestry code座,这些code;

 定时器定时器=新定时器(100,myshapestry);
t.start();


解决方案

添加像这样

  javax.swing.Timer中的定时器=新javax.swing.Timer中(100,面板);
timer.start();

每个100毫秒的定时调用的actionPerformed()的方法,你的 MyPanel

I'm trying to develop a simple game. The games is about the shapes. Shapes will move and we'll catch by mouse. I have already created a oval and given size of oval graphic. But I cannot move this shape repeatedly. I think I need to use timer class. I have been trying 2 hours myself but I didnt do yet.

The code;

    import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;


import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;



public class myshapestry extends JFrame implements ActionListener  {

JFrame frame=new JFrame("Deneme");
Container l ;
private static int ballX=150;
private static  int ballY=150;
myshapestry() {
     l=this.getContentPane();
     l.setLayout(null);
     MyPanel panel=new MyPanel();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.setVisible(true);
        frame.setSize(getPreferredSize());``

 }
 public Dimension getPreferredSize() {
       return new  Dimension(500,600);
   }
   public static void main (String args[]){
       myshapestry tr=new myshapestry();
       tr.setTitle("Game of Shapes");


   } 

   private static class MyPanel extends JPanel {
       protected void paintComponent(Graphics g){
           super.paintComponent(g);
           g.fillOval(ballX, ballY,50 , 70);

    }

       public void actionPerformed(ActionEvent e){
          ballX = ballX + 5;
          ballY = ballY + 10;
          repaint();
      }

       }

   }

I was trying these code in the myshapestry code block;

Timer timer=new Timer(100,myshapestry);
t.start();

解决方案

Add something like this

javax.swing.Timer timer=new javax.swing.Timer(100, panel) ;
timer.start();

Each 100msec the timer invokes actionPerformed() method of your MyPanel class

这篇关于如何移动形状的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆