Question

In: Computer Science

Could we turn this java code into a GUI game? Add a orange and a green...

Could we turn this java code into a GUI game? Add a orange and a green trophy object.

Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09.

Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09.

Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes 16.35 clicks so its rounded up to 17 clicks to buy again. Same for green but slightly different math.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
public class ImageClickCount {

public static void main(String[] args)throws Exception {

JLabel count_text= new JLabel("Total Click Count : "); // creating count_text instance of JLabel
JPanel group; // panel that uses CardLayout
JFrame frame = new JFrame("Image Click Count"); // creating frame instance of JFrame
BufferedImage img = ImageIO.read(new File("click-here.jpg"));// reading image file
JPanel image_panel = new JPanel(); // creating image_panel instance of JPanel
JLabel picture = new JLabel(new ImageIcon(img)); // creating picture instance of JLabel
image_panel.add(picture); // adding picture to image_panel
// now adding mouse listener to picture
picture.addMouseListener(new MouseAdapter()
{
int count=0; // initializing count variable
public void mouseClicked(MouseEvent e)
{
count++; // incrementing count variable
count_text.setText("Total Click Count : "+count); // setting click count
}
});
group = new JPanel(new CardLayout()); // creating group instance of JPanel
group.add(image_panel); // adding image_panle to group panel
JPanel label_panel = new JPanel(); // creating label_panel instance of JPanel
label_panel.add(count_text); // adding count_text to label_panel
Container pane = frame.getContentPane(); // creating pane instance of Container
// adding group panel to apne with BorderLayout.CENTER
pane.add(group, BorderLayout.CENTER);
  // adding label_panel to pane with BorderLayout.PAGE_END
pane.add(label_panel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setting default close operation
frame.setSize(350, 250); // setting width and height for frame
frame.setVisible(true); // setting frame visibility true
}
}

Solutions

Expert Solution

Solution for the given problem

package test;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class ImageClickCount {

static int orangeTrophyPrice = 15;
static int greenTrophyPrice = 100;

public static void main(String[] args)throws Exception {

JLabel count_text= new JLabel("Total Click Count : "); // creating count_text instance of JLabel
JPanel group; // panel that uses CardLayout
JFrame frame = new JFrame("Image Click Count"); // creating frame instance of JFrame
BufferedImage img = ImageIO.read(new File("click-here.jpg"));// reading image file
JPanel image_panel = new JPanel(); // creating image_panel instance of JPanel
JLabel picture = new JLabel(new ImageIcon(img)); // creating picture instance of JLabel
image_panel.add(picture); // adding picture to image_panel

// now adding mouse listener to picture
picture.addMouseListener(new MouseAdapter()
{
int count=0; // initializing count variable
public void mouseClicked(MouseEvent e)
{
count++; // incrementing count variable
count_text.setText("Total Click Count : "+count); // setting click count
if(count == greenTrophyPrice) {
System.out.println("GREEN TROPHY BOUGHT!");
greenTrophyPrice = (int) Math.ceil(greenTrophyPrice * 1.09);
System.out.println("NEW PRICE FOR GREEN : " + greenTrophyPrice);

}
if(count == orangeTrophyPrice) {
System.out.println("ORANGE TROPHY BOUGHT!");
orangeTrophyPrice = (int) Math.ceil(orangeTrophyPrice * 1.09);
System.out.println("NEW PRICE FOR ORANGE : " + orangeTrophyPrice);
}
}
});

group = new JPanel(new CardLayout()); // creating group instance of JPanel
group.add(image_panel); // adding image_panle to group panel
JPanel label_panel = new JPanel(); // creating label_panel instance of JPanel
label_panel.add(count_text); // adding count_text to label_panel
Container pane = frame.getContentPane(); // creating pane instance of Container

// adding group panel to pane with BorderLayout.CENTER
pane.add(group, BorderLayout.CENTER);
// adding label_panel to pane with BorderLayout.PAGE_END
pane.add(label_panel, BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // setting default close operation
frame.setSize(350, 250); // setting width and height for frame
frame.setVisible(true); // setting frame visibility true
}
}

*********Thank You***********


Related Solutions

Could we turn this java code into a GUI game? Add a orange and a green...
Could we turn this java code into a GUI game? Add a orange and a green trophy object. Orange trophy costs 15 clicks and every time we buy a orange trophy price increases by * 1.09. Green trophy costs 100 clicks and every time we buy a green trophy price increases by * 1.09. Example: you would click the button 15 times in order to be allowed to buy the orange trophy then it resets but then the cost becomes...
The premise of this project is to make a basic GUI and code for the game...
The premise of this project is to make a basic GUI and code for the game Keno. The specifics of the gui and game are below. Description: In this project you will implement the popular casino and state lottery game, Keno. This is a somewhat simple game to understand and play which should allow you to focus on learning GUI development in JavaFX and trying your hand at event driven programing. Implementation Details: You may add as many classes, data...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a)...
Topic is relates to Java programming! Code using java GUI 2. Create the following controls: a) a TextField b) a Button c) a Text d) a Label 3. create the following container and put them the above controls: a) a VBox controller b) a BorderPane controller c) a GridPane
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and goats or 'Bagh chan'. Ive posted the other 3 classes that I need help with, didnt want to put all 4 in one question. public class GameViewer implements MouseListener { // instance variables private int bkSize; // block size - all other measurements to be derived from bkSize private int brdSize; // board size private SimpleCanvas sc; // an object of SimpleCanvas to draw...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and...
**NEED ACTUAL JAVA CODE** Basically this code is defining the board for the game tigers and goats or 'Bagh chan'. Ive posted the other 3 classes that I need help with, didnt want to put all 4 in one question. /** * Maintains and updates the status of the board * i.e. the locations of goats and tigers * * @Student 1 Name: * @Student 1 Number: * * @Student 2 Name: * @Student 2 Number: */ public class Board...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
Write Java code which will create a GUI window on screen. The window has one button:...
Write Java code which will create a GUI window on screen. The window has one button: Doggy. When the user selects Doggy, “Bow Bow” should be printed on screen. Justify your syntax.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT