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]

1Các pro cứu em với Empty Các pro cứu em với Thu Feb 28, 2013 2:01 pm

vnsvnpt


Hột vịt tươi
Hột vịt tươi
Xin chào các ACE. Em nói thật là em đang học tại chức mà tại chức thì các ACE hiểu trình độ có hạn.Mong các ACE cứu em với.
Em có đoạn code bai tập lập trình hệ thống nhúng bằng java. đoạn code mô phỏng quả bóng chuyển động theo quy tắc bàn bi a. Giờ thầy yêu cầu là mỗi lần quả bóng chạm thành( biên ) thì đổi màu.các chuyên gia cứu em với. Em xin chân thành cảm ơn. Tuần sau là em thi lại lần hai rồi mong ACE giúp em sớm với nha

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;

public class HomNay extends MIDlet{

public void startApp(){
Display display = Display.getDisplay (this);
display.setCurrent(new CircleCanvas(display));
}

public void pauseApp(){}

public void destroyApp(boolean unconditional){}
}

class CircleCanvas extends Canvas{
Display display;
int R =10, x=0, y=0, width = 0, height = 0, dx = 10, dy = 10;
int red = 0, green = 0, blue= 0;

Random r;

CircleCanvas(Display display){
this.display = display;
width = getWidth();
height = getHeight();
r = new Random();

x = r.nextInt(width);
if (x < R)
x = R;

if (x > width - R)
x = width - R;

y = r.nextInt(height);
if (y < R)
y = R;

if (y > height - R)
y = height - R;

red = r.nextInt(256);
green = r.nextInt(256);
blue = r.nextInt(256);
}

public void paint(Graphics g){
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);

g.setColor(255, 0, 0);
g.drawRect(0, 0, width-1, height-1);

g.setColor(red, green, blue);
g.fillArc(x, y, R, R, 0, 360);

try{
Thread.sleep(50);
}catch (InterruptedException e){}

x += dx;
if ((x >= width - R)||(x <= 0))
dx = -dx;

y +=dy;
if ((y >= height - R)||(y <= 0))
dy = -dy;

repaint();

}

protected void keyPressed(int keyCode) {
if (keyCode == -1)
{
dy -=5;
}

if (keyCode == -2)
{
dy +=5;
}

if (keyCode == -3)
{
dx =-5;
x+=dx;
}

if (keyCode == -4)
{
dx =5;
x+=dx;
}

if (keyCode == -5)
{
dy = 0;
}

if (keyCode == 50)
{
R -=5;
}

if (keyCode == 56)
{
R +=5;
}
}
}

2Các pro cứu em với Empty Re: Các pro cứu em với Sat Mar 09, 2013 9:53 pm

Admin


Admin
Admin
Sr bạn, do mình bận nhiều việc vào tuần rồi nên không lên page xem.
Bạn có thể tham khảo bài có sẵn của mình. Mình đã làm 1 bài quả bóng dội 45 độ, và cách set màu.
Ở đây là code mình đã chỉnh! Chúc bạn vui!
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 cnt=0;
    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"};
    String cl[] = {"red","green","yellow","black","gray","blue"};
    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;
                        cnt++;
                        a.setColor(cl[cnt%6]);
                }
                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);
    }
}

https://javasupport.forumvi.com

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