Toggle Theme Editor
Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Charcoal

làm hiệu ứng tuyết rơi bằng java

Discussion in 'Xây dựng ứng dụng server-client desktop' started by thao, 14/1/15.

  1. thao

    thao New Member

    Chào mọi người!!! có bạn nào ở đây biết làm hiệu ứng tuyết rơi bằng java không? hướng dẫn cho t với. t sắp phải nộp rồi mà tìm hiểu mãi chẳng thấy có hiệu quả gì cả, hiện tại t đã làm dk 1 châm rơi từ trên xuống và h muốn nó có nhiều hơn và rơi ở các điểm ngẫu nhiên và có mầu sắc ngẫu nhiên thì làm thế nào nhỉ???.cảm ơn mọi người!!!
     
  2. Joe

    Joe Thành viên VIP

    Just because of you as a newbie
    PHP:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class 
    ItSnows extends JFrame {
            public 
    ItSnows( ) {
                    
    setTitle("It's snowing...");
                    
    Snowing snow = new Snowing();
                    
    snow.setBounds(55,400400);
                    
    add("Center"snow);
                    
    setBounds(30050400400);
                    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
    setVisible(true);
                    
    snow.go();
            }
          
            public class 
    Snowing extends JPanel {
                    private 
    double angle;
                    public 
    Snowing( ) { }

                    public 
    void go() {
                            (new 
    Falling()).start();
                    }
                    private class 
    Falling extends Thread {
                            public 
    Falling() { }
                            public 
    void run() {
                                    try {
                                            
    Random ran = new Random();
                                            while (
    true) {
                                                    
    ax.add(Math.abs(ran.nextInt(400)));
                                                    
    ay.add(Math.abs(ran.nextInt(400)));
                                                    
    repaint();
                                                    
    Thread.sleep(50);
                                            }
                                    } catch (
    Exception e) { }
                            }
                    }
                    private 
    ArrayList<Integerax = new ArrayList<Integer>(100);
                    private 
    ArrayList<Integeray = new ArrayList<Integer>(100);
                    protected 
    void paintComponent(Graphics g) {
                            
    super.paintComponent(g);
                            
    g.setColor(Color.black);
                            
    g.fillRect(00400400);
                            
    g.setColor(Color.white);
                            for (
    int i 0mx ax.size(); mx; ++i)
                                
    g.fillOval(ax.get(i), ay.get(i), 55);
                    }
            }
            public static 
    void main(String... a) {
                    new 
    ItSnows();
            }
    }
     
    Last edited: 15/1/15
  3. thao

    thao New Member

    Cảm ơn bạn Joe nhiều ạ nhưng nó không đúng đề tài mà. nó rơi từ trên xuống chứ không phải xuất hiện chấm trên màn hình như thế ạ.
     
  4. Joe

    Joe Thành viên VIP

    Oh...That's much easier...
    PHP:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    public class 
    ItSnows extends JFrame {
            public 
    ItSnows( ) {
                    
    setTitle("It's snowing...");
                    
    Snowing snow = new Snowing();
                    
    snow.setBounds(55,400400);
                    
    add("Center"snow);
                    
    setBounds(30050400400);
                    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
    setVisible(true);
                    
    snow.go();
            }
           
            public class 
    Snowing extends JPanel {
                    private 
    double angle;
                    public 
    Snowing( ) { }

                    public 
    void go() {
                            (new 
    Falling()).start();
                    }
                    private class 
    Falling extends Thread {
                            public 
    Falling() { }
                            public 
    void run() {
                                    try {
                                            
    Random ran = new Random();
                                            while (
    true) {
                                                    
    ran.nextInt(400);
                                                    for (
    ran.nextInt(400); 400+= 5) {
                                                            
    repaint();
                                                            
    Thread.sleep(20);
                                                    }
                                            }
                                    } catch (
    Exception e) { }
                            }
                    }
                    private 
    int xy;
                    protected 
    void paintComponent(Graphics g) {
                            
    super.paintComponent(g);
                            
    g.setColor(Color.black);
                            
    g.fillRect(00400400);
                            
    g.setColor(Color.white);
                            
    g.fillOval(xy55);
                    }
            }
            public static 
    void main(String... a) {
                    new 
    ItSnows();
            }
    }
     
  5. thao

    thao New Member

    Làm nó rơi nhiều hơn trong cùng 1 lúc thì sao ạ???
     
  6. Joe

    Joe Thành viên VIP

    Sorry, my dear. I show you the way to your goal, to get there you have to "walk" yourself. I don't carry anyone on my back or spoonfeed anybody like a "babe".

    Anyway, some hints for you:
    1) it requires a complex structure to organize a random number of snowflakes to fall slowly down ,
    2) Animation is an art of deceiving if you're able to create a scene that produces some optical illusion so that the viewers "see" the snowflakes floating down. With the 1st version and some modification you can achieve the illusion of "It snows and the snowflakes are floating". This version here is a little game with all the trimmings ;)
    PHP:
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class 
    ItSnows extends JFrame implements ActionListener {
            
    JComboBox<Stringcbdn;
            
    JButton go;
            
    Snowing snow;
            public 
    ItSnows( ) {
                    
    setTitle("It's snowing...");
                    
    snow = new Snowing();
                    
    snow.setBounds(00,410410);
                    
    cb = new JComboBox<String>("40!60!80!100!120!140!160!180!200".split("!"));
                    
    dn = new JComboBox<String>("Day!Night".split("!"));
                    
    dn.addActionListener(this);
                    
    cb.addActionListener(this);
                    
    JPanel jp = new JPanel();
                    
    JLabel jl = new JLabel("FallingSpeed mSec.");
                    
    go = new JButton("GO");
                    
    go.addActionListener(this);
                    
    jp.add(go);
                    
    jp.add(jl);
                    
    jp.add(cb);
                    
    jp.add(dn);
                    
    add("Center"snow);
                    
    add("South"jp);
                    
    setBounds(30050410420);
                    
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
    setVisible(true);
            }
            private 
    int speedday;
            private 
    boolean done false;
            public 
    void actionPerformed(ActionEvent e) {
                    
    day dn.getSelectedIndex();
                    
    speed Integer.parseInt((String)cb.getSelectedItem());
                    if (!
    donesnow.go();
                    
    go.setEnabled(false);
                    
    done true;

            }
            public class 
    Snowing extends JPanel {
                    private 
    double angle;
                    public 
    Snowing( ) { }

                    public 
    void go() {
                            
    ax = new ArrayList<Integer>(50);
                            
    ay = new ArrayList<Integer>(50);
                            (new 
    Falling()).start();
                    }
                    private class 
    Falling extends Thread {
                            public 
    Falling() { }
                            public 
    void run() {
                                    try {
                                        
    Random ran = new Random();
                                        while (
    true) {
                                            
    mx ran.nextInt(400);
                                            if (
    mx 10mx 10;
                                            for (
    int i 0mx; ++i) {
                                                    
    ax.add(ran.nextInt(400));
                                                    
    ay.add(ran.nextInt(400));
                                            }
                                            
    repaint();
                                            
    Thread.sleep(speed);
                                            
    ax.clear();
                                            
    ay.clear();
                                       }
                                    } catch (
    Exception e) { }
                            }
                    }
                    private 
    int mx;
                    private 
    ArrayList<Integeraxay;
                    protected 
    void paintComponent(Graphics g) {
                            
    super.paintComponent(g);
                            
    g.setColor(day == 0Color.gray:Color.black);
                            
    g.fillRect(00410410);
                            
    g.setColor(Color.white);
                            for (
    int i 0mx; ++i)
                                    
    g.fillOval(ax.get(i), ay.get(i), 1010);
                    }
            }
            public static 
    void main(String... a) {
                    try {   
    UIManager.setLookAndFeel
                            
    ("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                    } catch (
    Exception ex) { }
                    new 
    ItSnows();
            }
    }
     
    Last edited: 18/1/15
  7. thao

    thao New Member

    Cảm ơn bạn Joe nhiều nhiều ạ!!! với những gợi ý của bạn mình sẽ cố gắng hoàn thành bài!!!!:x
     

Chia sẻ trang này

Loading...