Giter VIP home page Giter VIP logo

legomania's Introduction

Lego Mania

Part 1

Create a Swing GUI application that displays the following graphics of red and blue lego blocks. The tower is always built from bottom row to top row, left to right in each row.

Alt text

Write a class named Legos1 extending the JFrame class with the following specifications.

a. Declare the integer type instance variables startX, startY, legoWidth, and legoHeight. The first two values represent the top-left coordinates of the first red block in the bottom row. The next two values are the width and height of each block.

b. Declare another integer instance variable, baseLength. This represents the number of blocks in the bottom row.

c. In the constructor, specify the appropriate window title using your lastName and assign the above instance variables with the values 20, 300, 50, 20, and 10, respectively.

d. In the paint method, using nested loops, fill the pattern from the bottom row all the way to the top. Notice that the number of blocks decreases by one for each row. Use the baseLength variable to control the loops.

e. In the main method, create the application object, set its size to 550 by 325 and its visibility to true to test the above graphics. Note: For each block, use the fillRoundRect method of the Graphics object. Use a value of 2 for arcWidth and arcHeight.

Part 2

Modify part1 assuming that now you have a bucket of lego blocks with various colors. Write a newclass Legos2 in the same package for this part. Use an array of Color objects and initialize the array with the colors:

Color.red, Color.blue, Color.yellow,
Color.green, Color.pink,Color.black, Color.magenta,
Color.orange, and Color.cyan.

For picking the next color for the block, use a java.util.Random object and generate a random integer using the nextInt method. Use the length of the colors array as an argument (do not hard code the array length).

Since the colors are picked at random, the output varies for each invocation. A sample output is shown below.

Alt text

part 3

Modify part2 and write the Legos3 version so that the same color is not picked consecutively. A sample output is shown below.

Alt text

legomania's People

Contributors

lavivien avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.