This program does a simple ball animation.
Một chương trình đơn giản hiển thị các quả bóng có chữ cái nhảy vòng.
Đây là một phần trong bài tập của một bạn đã nhờ mình support trước đây thấy cũng hay hay nên share lại cho các bạn tham khảo.
Main Application: NayBong.java
package naybong;
import java.awt.Dimension;
import javax.swing.*;
public class NayBong extends JPanel {
/**
* @param args the command line arguments
*/
public NayBong() {
add(new NayBongPanel());
}
public static void main(String[] args) {
JFrame win = new JFrame("Nay Bong Demo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setContentPane(new NayBongPanel());
win.getContentPane().setPreferredSize(new Dimension(400,600));
win.pack();
win.setVisible(true);
}
}
import java.awt.Dimension;
import javax.swing.*;
public class NayBong extends JPanel {
/**
* @param args the command line arguments
*/
public NayBong() {
add(new NayBongPanel());
}
public static void main(String[] args) {
JFrame win = new JFrame("Nay Bong Demo");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setContentPane(new NayBongPanel());
win.getContentPane().setPreferredSize(new Dimension(400,600));
win.pack();
win.setVisible(true);
}
}
Class Application: NayBongPanel.java
package naybong;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class NayBongPanel extends JPanel {
NayBongTrongBox m_bb;
public NayBongPanel() {
m_bb = new NayBongTrongBox();
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
startButton.addActionListener(new StartAction());
stopButton.addActionListener(new StopAction());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.PAGE_END);
this.add(m_bb , BorderLayout.CENTER);
}
class StartAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(true);
}
}
class StopAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(false);
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class NayBongPanel extends JPanel {
NayBongTrongBox m_bb;
public NayBongPanel() {
m_bb = new NayBongTrongBox();
JButton startButton = new JButton("Start");
JButton stopButton = new JButton("Stop");
startButton.addActionListener(new StartAction());
stopButton.addActionListener(new StopAction());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
this.setLayout(new BorderLayout());
this.add(buttonPanel, BorderLayout.PAGE_END);
this.add(m_bb , BorderLayout.CENTER);
}
class StartAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(true);
}
}
class StopAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
m_bb.setAnimation(false);
}
}
}
Class Application: NayBongTrongBox.java
package naybong;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NayBongTrongBox extends JPanel {
private TraiBong m_ball_01 = new TraiBong(0, 0, 5, 7, "A");
private TraiBong m_ball_02 = new TraiBong(30, 0, 6, 8, "B");
private TraiBong m_ball_03 = new TraiBong(60, 0, 9, 10, "C");
private int m_interval = 35;
private Timer m_timer;
public NayBongTrongBox() {
setPreferredSize(new Dimension(200, 80));
setBorder(BorderFactory.createLineBorder(Color.BLACK));
m_timer = new Timer(m_interval, new TimerAction());
}
public void setAnimation(boolean turnOnOff) {
if (turnOnOff) {
m_timer.start();
} else {
m_timer.stop();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
m_ball_01.draw(g);
m_ball_02.draw(g);
m_ball_03.draw(g);
}
class TimerAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
m_ball_01.setBounds(getWidth(), getHeight());
m_ball_01.move();
m_ball_02.setBounds(getWidth(), getHeight());
m_ball_02.move();
m_ball_03.setBounds(getWidth(), getHeight());
m_ball_03.move();
repaint();
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NayBongTrongBox extends JPanel {
private TraiBong m_ball_01 = new TraiBong(0, 0, 5, 7, "A");
private TraiBong m_ball_02 = new TraiBong(30, 0, 6, 8, "B");
private TraiBong m_ball_03 = new TraiBong(60, 0, 9, 10, "C");
private int m_interval = 35;
private Timer m_timer;
public NayBongTrongBox() {
setPreferredSize(new Dimension(200, 80));
setBorder(BorderFactory.createLineBorder(Color.BLACK));
m_timer = new Timer(m_interval, new TimerAction());
}
public void setAnimation(boolean turnOnOff) {
if (turnOnOff) {
m_timer.start();
} else {
m_timer.stop();
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
m_ball_01.draw(g);
m_ball_02.draw(g);
m_ball_03.draw(g);
}
class TimerAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
m_ball_01.setBounds(getWidth(), getHeight());
m_ball_01.move();
m_ball_02.setBounds(getWidth(), getHeight());
m_ball_02.move();
m_ball_03.setBounds(getWidth(), getHeight());
m_ball_03.move();
repaint();
}
}
}
Class Application: TraiBong.java
package naybong;
import java.awt.*;
public class TraiBong {
final static int DUONGKINH = 21;
private int m_x;
private int m_y;
private int m_vantocX;
private int m_vantocY;
private int m_rightBound;
private int m_bottomBound;
private String m_text;
public TraiBong(int x, int y, int vantocX, int vantocY, String text) {
m_x = x;
m_y = y;
m_vantocX = vantocX;
m_vantocY = vantocY;
m_text = text;
}
public void setBounds(int width, int height) {
m_rightBound = width - DUONGKINH;
m_bottomBound = height - DUONGKINH;
}
public void move() {
m_x += m_vantocX;
m_y += m_vantocY;
if (m_x < 0) {
m_x = 0;
m_vantocX = -m_vantocX;
} else if (m_x > m_rightBound) {
m_x = m_rightBound;
m_vantocX = -m_vantocX;
}
if (m_y < 0) {
m_y = 0;
m_vantocY = -m_vantocY;
} else if (m_y > m_bottomBound) {
m_y = m_bottomBound;
m_vantocY = -m_vantocY;
}
}
public void draw(Graphics g) {
g.setColor(Color.black);
g.fillOval(m_x, m_y, DUONGKINH, DUONGKINH);
g.setColor(Color.white);
g.drawString(m_text, m_x - DUONGKINH / 2 + 17, m_y - DUONGKINH / 2 + 25);
}
public int getDiameter() { return DUONGKINH;}
public int getX() { return m_x;}
public int getY() { return m_y;}
public String getText() { return m_text;}
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}
import java.awt.*;
public class TraiBong {
final static int DUONGKINH = 21;
private int m_x;
private int m_y;
private int m_vantocX;
private int m_vantocY;
private int m_rightBound;
private int m_bottomBound;
private String m_text;
public TraiBong(int x, int y, int vantocX, int vantocY, String text) {
m_x = x;
m_y = y;
m_vantocX = vantocX;
m_vantocY = vantocY;
m_text = text;
}
public void setBounds(int width, int height) {
m_rightBound = width - DUONGKINH;
m_bottomBound = height - DUONGKINH;
}
public void move() {
m_x += m_vantocX;
m_y += m_vantocY;
if (m_x < 0) {
m_x = 0;
m_vantocX = -m_vantocX;
} else if (m_x > m_rightBound) {
m_x = m_rightBound;
m_vantocX = -m_vantocX;
}
if (m_y < 0) {
m_y = 0;
m_vantocY = -m_vantocY;
} else if (m_y > m_bottomBound) {
m_y = m_bottomBound;
m_vantocY = -m_vantocY;
}
}
public void draw(Graphics g) {
g.setColor(Color.black);
g.fillOval(m_x, m_y, DUONGKINH, DUONGKINH);
g.setColor(Color.white);
g.drawString(m_text, m_x - DUONGKINH / 2 + 17, m_y - DUONGKINH / 2 + 25);
}
public int getDiameter() { return DUONGKINH;}
public int getX() { return m_x;}
public int getY() { return m_y;}
public String getText() { return m_text;}
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}
Kết quả - Result
public void move() {
ReplyDeletem_x += m_vantocX;
m_y += m_vantocY;
if (m_x < 0) {
m_x = 0;
m_vantocX = -m_vantocX;
} else if (m_x > m_rightBound) {
m_x = m_rightBound;
m_vantocX = -m_vantocX;
}
if (m_y < 0) {
m_y = 0;
m_vantocY = -m_vantocY;
} else if (m_y > m_bottomBound) {
m_y = m_bottomBound;
m_vantocY = -m_vantocY;
}
}
lỗi ko chạy
Câu báo lỗi là gì vậy? bạn copy nguyên câu báo lỗi cho mình xem sao nha.
Delete