Question

Using Java and a mouse or keyboard listener, create an image containing some object whose color...

Using Java and a mouse or keyboard listener, create
an image containing some object whose color can be changed when
some input is received from the keyboard or mouse. If you are
adventurous, you can try inputting a color name from the keyboard
and change the color to the one named in that input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 import java.awt.*; import javax.accessibility.AccessibleContext; import javax.swing.*; import javax.swing.plaf.basic.BasicComboPopup; public class ComboTest { public JComponent makeUI() { // TEST: // UIManager.put("ComboBox.selectionBackground", Color.GREEN); // UIManager.put("ComboBox.selectionForeground", Color.CYAN); String[] model = {"Male", "Female"}; JComboBox<String> combo1 = new JComboBox<>(model); AccessibleContext ac = combo1.getAccessibleContext(); BasicComboPopup pop = (BasicComboPopup) ac.getAccessibleChild(0); JList list = pop.getList(); list.setSelectionForeground(Color.WHITE); list.setSelectionBackground(Color.ORANGE); // ???: The focus border of the JComboBox is not drawn. JComboBox<String> combo2 = new JComboBox<>(model); combo2.setRenderer(new DefaultListCellRenderer() { @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean hasFocus) { JLabel l = (JLabel) super.getListCellRendererComponent( list, value, index, isSelected, hasFocus); if (isSelected) { l.setForeground(Color.WHITE); l.setBackground(Color.ORANGE); } else { l.setForeground(Color.BLACK); l.setBackground(Color.WHITE); } return l; } }); // // TEST: // JComboBox<String> combo3 = new JComboBox<>(model); // combo3.setRenderer(new DefaultListCellRenderer() { // @Override public Component getListCellRendererComponent( // JList list, Object value, int index, // boolean isSelected, boolean hasFocus) { // list.setSelectionForeground(Color.WHITE); // list.setSelectionBackground(Color.ORANGE); // return super.getListCellRendererComponent( // list, value, index, isSelected, hasFocus); // } // }); JPanel box = new JPanel(new GridLayout(0, 1, 10, 10)); box.add(combo1); box.add(combo2); // box.add(combo3); JPanel p = new JPanel(new BorderLayout()); p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); p.add(box, BorderLayout.NORTH); return p; } public static void main(String[] args) { EventQueue.invokeLater(() -> { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception e) { e.printStackTrace(); } JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(new ComboTest().makeUI()); f.setSize(320, 240); f.setLocationRelativeTo(null); f.setVisible(true); }); } }
Add a comment
Know the answer?
Add Answer to:
Using Java and a mouse or keyboard listener, create an image containing some object whose color...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • PLEASE HELP IN JAVA: This is an exercise in using the java LinkedList class. Using input...

    PLEASE HELP IN JAVA: This is an exercise in using the java LinkedList class. Using input file words.txt , create a LinkedList BUT you must NOT add a word that is already in the list and you MUST add them in the order received to the end of the list. For example, if your input was cat, cat, dog, mouse, dog, horse your list would now contain cat, dog, mouse, horse. And the head of the list would be cat,...

  • Write an ADT named circle in java containing the following. Include data: radius (double) Include methods:...

    Write an ADT named circle in java containing the following. Include data: radius (double) Include methods: A no-argument constructor A constructor that sets the data to passed values Getters and setters for each piece of data getArea (Math.PI * radius * radius … try using a Math method here) getDiameter (radius * 2) getCircumference (2 * Math.PI * radius) A toString( ) method to display the object data Include Javadoc comments for the class and every method, and generate the...

  • Not sure about this Java question. Thank you! Which line below would create an object of...

    Not sure about this Java question. Thank you! Which line below would create an object of the Scanner class so you can input from the keyboard into your program? O a. Scanner mylnput = new Scanner(System.in); O b. Scanner mylnput = new Scanner.System.in; O c. Scanner new = mylnput as Scanner, O d. Scanner mylnput = new System(Scanner)

  • Java FX Application Purpose The purpose of this assignment is to get you familiar with the...

    Java FX Application Purpose The purpose of this assignment is to get you familiar with the basics of the JavaFX GUI interface components. This assignment will cover Labels, Fonts, Basic Images, and Layouts. You will use a StackPane and a BorderPane to construct the layout to the right. In addition you will use the Random class to randomly load an image when the application loads Introduction The application sets the root layout to a BorderPane. The BorderPane can be divided...

  • You wish to create an image that is 10 meters from an object. of the object....

    You wish to create an image that is 10 meters from an object. of the object. You wish to accomplish this using one spherical mirror a. what type of mirror do you need? b. will the image be real or virtual? b. What is the focal length f of the mirror that would accomplish this? 9 A T rex chases people in Jurassic Park as they desperately try to escape in th fast, as they can see in the outside...

  • Create a new Java class called: House public class House Remember that Java is case-sensitive. This...

    Create a new Java class called: House public class House Remember that Java is case-sensitive. This means that the class name Diamond should start with a capital letter, and then the rest of the letters should be all lower-case. It would work if you were to use any combination of upper and lower-case letters, but that idea does not follow the assignment instructions. Write a Java application program that prints, the following house drawing to the screen: The house is...

  • JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window....

    JAVA SOLUTION This lab has four parts: Create a window. Create 5 buttons in that window. Create an event when a button is pushed. Create an event listener that will respond to the button being pushed event. Task 1 – Create a Window For Java: Please ensure that the following import statements are used: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.GridPane; import javafx.scene.text.Text; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; Next, ensure that there is a main method which is...

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • JAVA ONLY. For this assignment you will create an employee application, containing contact information, salary, and...

    JAVA ONLY. For this assignment you will create an employee application, containing contact information, salary, and position. You will also define the type of company. Is it an engineering firm, a supermarket, or a call center? Once the application has been completed, I should be able to perform a query for any piece of information associated with each employee.   For instance, if I search for employees who make over $50,000, all of the employees whose salaries are greater than $50,000 should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT