Question

In: Computer Science

Create a simple Swing application that animates a square diagonally across its window once. You may...

  1. Create a simple Swing application that animates a square diagonally across its window once. You may set the size of the window and assume that it doesn’t change. Using Java swing using timers

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// MovingSquare.java

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.Timer;

public class MovingSquare extends JFrame {

      //declaring important attributes

      private int x, y; //current position of square

      private int size; //size of square

      private Color color; //square color

      private Timer timer; //timer for running animation

      public MovingSquare() {

            //will exit on close button click

            setDefaultCloseOperation(EXIT_ON_CLOSE);

            //using 500x500 size

            setSize(500, 500);

            //assigning values to instance variables

            x = 0;

            y = 0;

            size = 80;

            color = Color.BLUE;

            //initializing a timer that runs every 50 ms

            timer = new Timer(50, new ActionListener() {

                  @Override

                  public void actionPerformed(ActionEvent e) {

                        //updating x and y values by 5

                        x+=5;

                        y+=5;

                        //if the square goes out of bounds, stopping the timer

                        if ((x + size) >= getWidth() || (y + size) >= getHeight()) {

                              timer.stop();

                        }

                        //repainting to make things visible

                        repaint();

                  }

            });

            //starting timer

            timer.start();

            //making window visible

            setVisible(true);

      }

      @Override

      public void paint(Graphics g) {

            super.paint(g);

            //using the specified color

            g.setColor(color);

            //drawing a filled square

            g.fillRect(x, y, size, size);

      }

     

      public static void main(String[] args) {

            //initializing GUI

            new MovingSquare();

      }

}

/*OUTPUT*/



Related Solutions

Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
Create an application to simulate a bank ATM and you must combine graphical interfaces using Swing...
Create an application to simulate a bank ATM and you must combine graphical interfaces using Swing or AWT and relational databases. We will have a Bank Accounts database. Each account will have an account number, the name of the owner and the account balance. The program must allow the following options: a) Create an account: request the account information and save it in the database. b) Enter the ATM: it will allow you to request the account number and will...
Create a simple C++ application that will exhibit concurrencyconcepts. Your application should create two threads...
Create a simple C++ application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0. For your created code, provide a detailed analysis of appropriate concepts that could impact your application. Specifically, address:Performance issues with concurrencyVulnerabilities exhibited with use of stringsSecurity of the data types exhibited.
Create a Java application that creates a window in which multiple bouncing balls can be animated....
Create a Java application that creates a window in which multiple bouncing balls can be animated. have attached a text file of the source found on that site. You can paste the entire application into an Eclipse source file named BounceThread.java and it will compile and run. Optionally, you may prefer to put some or all of the classes into separate files to organize your work. Your task is to add the following capability to the application: (5%)    Increase the...
Chi Square Assignment Foundational Knowledge: 1. When is chi-square used? Critical Thinking and Application: 1. Create...
Chi Square Assignment Foundational Knowledge: 1. When is chi-square used? Critical Thinking and Application: 1. Create null and research hypothesis statements and formulas for the following scenario: Sam wants to know out of all majors, which has the most students? H0: H1: 2. Using the numbers provided below, calculate a one-sample Chi-Square and interpret the outcome at the .05 level. (where O = observed; E = expected) [In this example, we are expecting there to be the same number of...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple...
Database Application Development Project/Assignment Milestone 1 (part 1) Objective: In this assignment, you create a simple HR application using the C++ programming language and Oracle server. This assignment helps students learn a basic understanding of application development using C++ programming and an Oracle database Submission: This Milestone is a new project that simply uses what was learned in the SETUP. Your submission will be a single text-based .cpp file including your C++ program for the Database Application project/assignment. The file...
Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
Creation of Program Application (Development Task 1) This program should create two output files. You may...
Creation of Program Application (Development Task 1) This program should create two output files. You may use .txt files. Create the following files: Deposists.txt Withdrawls.txt 1. The program application should have the following inputs: Amount for Deposits (Only Accept Positive Amounts) Amount for Withdrawals (Only Accept Positive Amounts). The subsequent program will subtract the positive amount. 2. The program application should have the following outputs: Deposists.txt Withdrawals.txt Total of Deposits and Withdrawals back to the console
Once you create a financial model, you need to address the issue of uncertainty. Compare and...
Once you create a financial model, you need to address the issue of uncertainty. Compare and contrast two methods for including and assessing risk in a financial model. What are the key variables that should be included in a model that can increase risk and how?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT