Related questions
Concept explainers
Java
In this assignment, you will practice using an ArrayList. Your class will support a sample program that demonstrates some of the kinds of operations a windowing system must perform. Instead of windows, you will be manipulating "tiles." A tile consists of a position (specified by the upper-left corner x and y), a width, a height, and color. Positions and distances are specified in terms of pixels, with the upper-left corner being (0, 0) and the x and y coordinate increasing as you move left and down, respectively.
You won’t have to understand a lot about tiles and coordinates. That code has been written for you. You are writing a small part of the overall system that keeps track of the list of tiles. You are required to implement this using an ArrayList. The only method you need to use from the Tile class is the following:
// returns true if the given (x, y) point falls inside this tile
public boolean inside(int x, int y)
Your class is to be called TileList and must implement the following public methods:
// post: constructs an empty tile list
public TileList()
// post: searches through the list of tiles and returns a
// reference to the last tile for which (x, y) is
// inside the tile; returns null if (x, y) is not
// inside any tile of the list; moves the found tile
// to the back of the list
public Tile moveToBack(int x, int y)
// post: inserts t at the back of the list of tiles
public void insertBack(Tile t)
// post: returns the number of tiles in this list
public int size()
// post: returns the Tile at the given index
public Tile get(int index)
The file TileMain.java contains the main driver program. Once you have written your solution to TileList and it compiles, try compiling and running TileMain. You should be able to create rectangles by clicking and dragging to indicate the endpoints. You should also be able to click on a tile to bring it to the top and you should be able to click and drag to move a tile to a new location.
Supporting Files:
Tile.java
(See photo)
TileFrame.java
// This class is the top-level frame for a set of colored tiles.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TileFrame extends JFrame {
private TilePanel panel;
public TileFrame() {
setSize(400, 400);
setTitle("Fun with Tiles");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new TilePanel(Color.RED);
add(panel, BorderLayout.CENTER);
addColorButtons();
}
private void addColorButtons() {
JPanel p = new JPanel();
p.setBackground(Color.CYAN);
ButtonGroup g = new ButtonGroup();
addButton(p, g, "Green", false, Color.GREEN);
addButton(p, g, "Blue", false, Color.BLUE);
addButton(p, g, "Red", true, Color.RED);
add(p, BorderLayout.NORTH);
}
private void addButton(JPanel p, ButtonGroup g, String name,
boolean selected, final Color color) {
JRadioButton button = new JRadioButton(name, selected);
p.add(button);
g.add(button);
button.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
panel.setColor(color);
}
}
});
}
}
TileListener.java
(See photo)
TileMain.java
// This program allows a user to explore a system of overlapping tiles
// that has similar properties to a windowing system. Click and drag the
// mouse to create rectangles. Click on tiles to bring them to the front.
// Click and drag a rectangle to move it.
public class TileMain {
public static void main(String[] args) {
TileFrame frame = new TileFrame();
frame.setVisible(true);
}
}
TilePanel.java
// This class keeps track of a set of colored tiles in a panel.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class TilePanel extends JPanel {
private TileList tiles;
private Color color;
public TilePanel(Color color) {
setBackground(Color.WHITE);
tiles = new TileList();
this.color = color;
MouseInputListener listener = new TileListener(tiles, this);
addMouseListener(listener);
addMouseMotionListener(listener);
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < tiles.size(); i++) {
tiles.get(i).draw(g);
}
}
}
The answer is given below:-
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps with 2 images
- Rename the show method to people Override the show method in student and in the other class you created (this will be the polymorphic method) In main program, create an array of 3 objects of class people Instantiate each array position with a different object (people, student, other) With a loop, display all the objects in the array.arrow_forwardWrite a program in JAVA to input the number of elements in an array, also input the values in the array from the console. Replace all those elements which are even and greater than 5, with -99. Print the final array. Constraints: The array should initially not contain -99. The value of the number of elements should not be greater than 50.arrow_forwardImplement the"paint fill"function that one might see on many image editing programs. That is, given a screen (represented by a two-dimensional array of colors), a point, and a new color, fill in the surrounding area until the color changes from the original color.arrow_forward
- Write a Java program (Hangman.java) to play the classic word game Hangman. In the game, a word is randomly selected from an array of possible words. Then, the user is prompted to guess letters in the word one at a time. When the user makes a correct guess, all instances of that letter in the word are shown. When all letters in the word have been guessed, the game ends and the score (number of missed guesses) is displayed. A user can play the game multiple times if they choose. Here is a sample run of a correct program (user input indicated by orange text):Enter a letter in word ******** > cEnter a letter in word c******* > rEnter a letter in word c******r > s s is not in the wordEnter a letter in word c******r > tEnter a letter in word c****t*r > mEnter a letter in word c*m**t*r > t t is already in the wordEnter a letter in word c*m**t*r > pEnter a letter in word c*mp*t*r > oEnter a letter in word comp*t*r > eEnter a letter in word comp*ter >...arrow_forwardJAVA you guys returned the question and said that it is a writing assignment but it is not. It is a java question, multiple-choice no coding. Please help me with this.arrow_forwardThis is in Java. The assignement is to create a class. I am confused on how to write de code for an array that is up to 5. Also I am confused regarding how to meet the requirements of the constructor. This is what I have. public class InventoryOnShelf { //fields private int[] itemOnShelfList []; private int size; public InventoryOnShelf() { }arrow_forward
- I need to create a code for tic tac toe in java where you play against a computer. I need the board to be in an array of chararrow_forwardThis question is in javaYou are walking along a hiking trail. On this hiking trail, there is elevation marker at every kilometer. The elevation information is represented in an array of integers. For example, if the elevation array is [100, 50, 20, 30, 50, 40], that means at kilometer 0, the elevation is 100 meters; at kilometer 1, the elevation is 50 meters; at kilometer 2, the elevation is 20 meters; at kilometer 3, the elevation is 30 meters; at kilometer 4, the elevation is 50 meters; at kilometer 5, the elevation is 40 meters. a) Write a method called dangerousDescent that determines whether there is a downhill section in the hiking trail with a slope of more than -0.05. The slope is calculated by the rise in elevation over the run in distance. For example, the last kilometer section has a gradient of -0.01 because the rise in elevation is 40 - 50 = -10 meters. The distance covered in 1000 meters. -10 / 1000 = 0.01. Your method should return true if there exists a dangerous...arrow_forwardBuild two classes (Fraction and FractionCounter) and a Driver for use in counting the number of unique fractions read from a text file. We’ll also reuse the ObjectList class we built in lab to store our list of unique FractionCounters, instead of directly using arrays or the ArrayList. Remember NO DECIMALS! Handle input of any length Introduction Your project is to read in a series of fractions from a text file, which will have each line formatted as follows: "A/B". A sample text file is listed below, and the purpose of your program is to read in one line at a time and build a Fraction object from A and B. For each unique Fraction seen, your program will create a FractionCounter object used to track the number of occurrences of that specific fraction. When all the input is consumed, your program will print out its ObjectList of unique FractionCounters, which should report the fraction and its count – see output below. You can assume no blank lines or misleading characters; see...arrow_forward
- Helparrow_forwardUse the ArrayList class Add and remove objects from an ArrayList Protect from index errors when removing Practice with input loop Details: This homework is for you to get practice adding and removing objects from an ArrayList. The Voter class was used to create instances of Voters which held their name and a voter identification number as instance variables, and the number of instances created as a static variable. This was the class diagram: The constructor takes a string, passed to the parameter n, which is the name of the voter and which should be assigned to the name instance variable. Every time a new voter is created, the static variable nVoters should be incremented. Also, every time a new voter is created, a new voterID should be constructed by concatenating the string "HI" with the value of nVoters and the length of the name. For example, if the second voter is named "Clark Kent", then the voterID should be "HI210" because 2 is the value of nVoters and 10 is the number...arrow_forwardCan you please help me with this, its in java. Thank you. Write classes in an inheritance hierarchy Implement a polymorphic method Create an ArrayList object Use ArrayList methods For this, please design and write a Java program to keep track of various menu items. Your program will store, display and modify salads, sandwiches and frozen yogurts. Present the user with the following menu options: Actions: 1) Add a salad2) Add a sandwich3) Add a frozen yogurt4) Display an item5) Display all items6) Add a topping to an item9) QuitIf the user does not enter one of these options, then display:Sorry, <NUMBER> is not a valid option.Where <NUMBER> is the user's input. All menu items have the following: Name (String) Price (double) Topping (StringBuilder or StringBuffer of comma separated values) In addition to everything that a menu item has, a main dish (salad or sandwich) also has: Side (String) A salad also has: Dressing (String) A sandwich also has: Bread type...arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education