TODAY I WILL BE SHOWING YOU HOW TO MAKE AN ANIMATION USING JAVA.
- import javax.swing.*;
- import java.awt.*;
- final public class Test1 {
- JFrame frame;
- DrawPanel drawPanel;
- private int A = 9;
- private int B = 5;
- boolean Up = false;
- boolean Down = true;
- public static void main(String[] args) {
- new Test1().go();
- }
- private DrawPanel DrawPanel;
- private void go() {
- frame = new JFrame("SHAPE ANIMATION");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- DrawPanel = new DrawPanel();
- frame.getContentPane().add(BorderLayout.CENTER, DrawPanel);
- frame.setVisible(true);
- frame.setResizable(false);
- frame.setSize(200, 200);
- frame.setLocation(175, 55);b
- moveANIME();
- }
- class DrawPanel extends JPanel {
- public void paintComponent(Graphics g) {
- g.setColor(Color.BLUE.darker());
- g.fillRect(0, 0, this.getWidth(), this.getHeight());
- g.setColor(Color.blue.darker());
- g.fillOval(3, 5, this.getWidth()-6, this.getHeight()-6);
- g.setColor(Color.RED.brighter());
- g.fillRect(10, 6, this.getWidth()-12, this.getHeight()-12);
- g.setColor(Color.black.brighter());
- g.fillOval(B, 10, 9, 7);
- g.setColor(Color.black.brighter());
- g.fillOval(11, B, 9, 7);
- g.setColor(Color.black.brighter());
- g.fillOval(80, B, 9, 7);
- g.setColor(Color.black.brighter());
- g.fillOval(B, B, 9, 7);
- }
- }
- private void moveANIME() {
- while(true){
- if(B >= 260){
- Up = true;
- Down = false;
- }
- if(B <= 7){
- Up = false;
- Down = true;
- }
- if(Up){
- B--;
- }
- if(Down){
- B++;
- }
- try{
- Thread.sleep(12);
- } catch (Exception exc){}
- frame.repaint();
- }
- }
- }

HOW TO MAKE AN ANIMATION IN JAVA USING NETBEANS by Mohammed Ibrahim Mirza Beig is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at https://www.wired2tech.org/2017/12/how-to-make-animation-in-java-using.html?m=1.
Comments
Post a Comment