Hỗ trợ lập trình Java
Hãy đăng nhập để tham gia thảo luận và chia sẻ!
Hỗ trợ lập trình Java
Hãy đăng nhập để tham gia thảo luận và chia sẻ!
Hỗ trợ lập trình Java

Diễn đàn hỏi đáp Java, hướng dẫn bài tập Java, Giúp đỡ về Java


You are not connected. Please login or register

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

1Bài tập quả bóng chuyển động Empty Bài tập quả bóng chuyển động Tue Nov 06, 2012 1:38 pm

java_study


Hột vịt tươi
Hột vịt tươi
Chào các bạn,
Mình mới học java nên chưa hiểu lắm về timer.
Bạn có thể demo cho mình về cách sử dụng timer bằng chương trình quả bỏng chuyển động trong frame không, khi nó chạm vào bìa của frame thì nó sẽ dội lệc đi một góc nào đó.
Cảm ơn các bạn

Admin


Admin
Admin
Tuy chưa tối ưu được code, nhưng mong là có thể giúp được bạn! Nó sẽ hoạt động theo frame của bạn, bạn có thể co dãn frame, và quả bóng sẽ hoạt động theo frame tại thời điểm đó

Code:


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Dell
 */
public class funnyball {
    JFrame frame;
    ball a;
    Timer timer;
    int x,y,incx,incy,lx,ly,state, flag=0;
    JPanel control,mau;
    JLabel lb,tenmau;
    String[] Color = {"Đỏ","Xanh lá","Vàng","Đen","Nâu","Xanh biển"};
    JComboBox cb;
    JButton Start;
    funnyball(){
        frame = new JFrame();
        frame.setLayout(new BorderLayout());
        frame.setTitle("Funny Ball");
        frame.setSize(400, 300);
        http://frame.setPreferredSize(new Dimension(300, 300));
        frame.setLocation(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        control = new JPanel(new GridLayout(1, 2));
        tenmau = new JLabel("Chọn màu: ");
        lb = new JLabel();
        lx=0;ly=9;
        x=0;
        y=0;
        state=0;
        lb.setText("Ball location: ("+(x+25)+":"+(y+25)+")");
        Start = new JButton("Start");
        Start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (flag ==0){
                    flag=1;
                    Start.setText("Pause");
                }
                else{
                    flag=0;
                    Start.setText("Start");
                }
            }
        });
        a = new ball();
        cb = new JComboBox(Color);
        mau = new JPanel(new FlowLayout());
        cb.setPreferredSize(new Dimension(70, 20));
        cb.setSelectedIndex(0);
        cb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox t = (JComboBox) e.getSource();
                int pos = t.getSelectedIndex();
                String c="";
                if (pos==0)
                    c="red";
                else
                    if (pos==1){
                        c="green";
                    }
                    else
                        if (pos==2){
                            c="yellow";
                        }
                        else
                            if (pos==3){
                                c="black";
                            }
                            else
                                if (pos==4){
                                    c="gray";
                                }
                                else
                                    if (pos==5){
                                        c="blue";
                                    }
                a.setColor(c);
            }
        });
        frame.add(a,BorderLayout.CENTER);
        mau.add(tenmau);
        mau.add(cb);
        lb.setHorizontalTextPosition(SwingConstants.CENTER);
        control.add(mau);
        control.add(Start);
        http://control.add(lb);
        control.setPreferredSize(new Dimension(frame.getWidth(), 50));
        frame.add(control,BorderLayout.SOUTH);
        frame.add(lb,BorderLayout.NORTH);
        frame.setVisible(true);
        delay();
    }
    public final  void delay(){
        timer = new Timer();
        timer.schedule(new Task( ), 5);
    }
    class Task extends TimerTask{

        @Override
        public void run() {
         
            lx = frame.getWidth();
            ly = frame.getHeight();
            if (flag==1){
                if (x+ 68  >= lx || y+160>=ly || x<0 || y<0){
                    state++;
                    if (state==4)
                        state=0;
                }
                if (state==0){
                    incx=1;
                    incy=1;
                }
                else
                    if (state==1){
                        incx=1;
                        incy=-1;
                    }
                    else
                        if (state==2){
                            incx=-1;
                            incy=-1;
                        }
                        else
                            if (state==3){
                                incx=-1;
                                incy=1;
                            }

                x=x+incx;
                y=y+incy;
            }
                lb.setText("Ball location: ("+(x+25)+":"+(y+25)+")");
                a.setL(x, y,lx,ly);
                a.repaint();
                delay();
           
        }
    }
    public static void main(String[] args) {
        funnyball obj = new funnyball();
    }
}


class ball extends JPanel{
    int x,y,w,h;
    String color="red";
    public void setL(int x,int y,int w,int h){
        this.x=x;
        this.y=y;
        this.w=w;
        this.h=h;
    }
    public void setColor(String color){
        this.color = color;
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.green);
        g.drawRect(0, 0, w-17, h-110);
        if (color.equals("red")){
            g.setColor(Color.red);
        }
        else
            if (color.equals("green")){
                g.setColor(Color.green);
            }
            else
                if (color.equals("yellow")){
                    g.setColor(Color.yellow);
                }
                else
                    if (color.equals("black")){
                        g.setColor(Color.black);
                    }
                    else
                        if (color.equals("gray")){
                            g.setColor(Color.gray);
                        }
                        else
                            if (color.equals("blue")){
                                g.setColor(Color.blue);
                            }
                   
        g.fillRoundRect(x, y, 50, 50, 50, 50);
        g.setColor(Color.PINK);
        g.drawOval(x+13, y, 25, 50);
        g.drawOval(x, y+13, 50, 25);
    }
}
}



Được sửa bởi Admin ngày Tue Nov 06, 2012 10:03 pm; sửa lần 3.

https://javasupport.forumvi.com

3Bài tập quả bóng chuyển động Empty Hic. Góp vui !!! Tue Nov 06, 2012 6:07 pm

Võ Nguyên Khoa


Vịt con
Vịt con
Code bị xóa vì quá gà !!

http://mrkhoa.web44.net

Sponsored content


Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết