How To Generate Alternate Color Of Balls Combination For 3 Boxes
Feb 24, 2014
I am working on a project in which I have three box (as of now) and each box will have some color of balls. So I am storing them in a Map of String and List of String as mention below.
Map<String, List<String>> boxBallMap = new LinkedHashMap<String, List<String>>();
Data in the above map is like this -
Java Code:
{box1=[blue, red, orange]}
{box2=[blue, red]}
{box3=[blue, red, orange]} mh_sh_highlight_all('java'); ProblemStatement:-
Basis on the above input, I need to return a mapping which will be List<Map<String, String>>, let's say for above input, below mapping would be return as an output -
Java Code:
[{box1=blue, box2=red, box3=orange},
{box1=red, box3=blue},
{box1=orange, box2=blue, box3=red}] mh_sh_highlight_all('java');
Here if you see, each row has alternate color of balls for each box - meaning blue for box1, red for box2, orange for box3 in first row. I cannot have same color of balls in each row. So this combination is not possible as it has same color of balls for two boxes in one row.
Java Code: {box1=blue, box2=blue, box3=orange} mh_sh_highlight_all('java');
And also, in the second row, I won't use those balls which have been used in the first row for that box. In second row, box1 has red why? bcoz blue was already used in the first row for box1 and box3 has blue and no box2 in second row.
The output combination is getting generated basis on the input being passed as shown above.
I started with the below code -
Java Code:
List<String> balls1 = Arrays.asList("red", "blue", "orange");
List<String> balls2 = Arrays.asList("red", "blue");
List<String> balls3 = Arrays.asList("red", "blue", "orange");
Map<String, List<String>> maps = new LinkedHashMap<String, List<String>>();
[Code] ....
Below is my method in which the crux of logic should be there
Java Code:
private static List<Map<String, String>> generateMappings(Map<String, List<String>> map) {
return null;
} mh_sh_highlight_all('java');
Below algorithm might work but still not sure how should I fit this in the code -
- sort the boxes by the number of balls they have in it (ascending, from the smallest to the largest box).
- while there are colors left
- loop over the sorted list of boxes
- in each iteration pick a color from the box (if there is one left), that is not already picked in the current iteration (of the while loop)
View Replies
ADVERTISEMENT
Feb 24, 2014
This code is for a game I'm making. I'm new to using modulus, and I've been looking all over the place for a tutorial that I can understand. No luck. :
What I'm trying to do is get the console to print "enemy hits player" as soon as enemyAttacksPlayer() is called.
After one second, "player hits enemy" should be printed, so it alternates between these two lines every second.
The issue is when I call enemyAttacksPlayer(), it takes a few seconds to print any lines, and this delay is kinda annoying.
public int time = 0;
public void enemyAttacksPlayer() {
time++;
if (time % 120 == 60) {
System.out.println("enemy hits player");
} if (time % 120 == 0) {
System.out.println("player hits enemy");
}
}
I think I may be using % wrong.
View Replies
View Related
May 3, 2014
I'm new to java and I need changing the direction of these balls from a horizontal axis to a vertical axis bouncing in the center. I've tried reversing the integers but there seems to be something i've missed.
package package_bouncingball;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class Class_bouncingball extends Applet implements Runnable
{
int x_pos1 = 150;
[Code]...
View Replies
View Related
Apr 11, 2014
I am try to move 2 ball at same time in different position but only one ball is display.
Here is my code I used two class one extends JPanel and another for JFrame
package usageThread;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
public class ball extends JPanel implements Runnable {
[Code]...
View Replies
View Related
Jun 16, 2014
I've been stuck on this one for a while. If you scroll down to the while loop there is some code that calculates the angle of a triangle created by two circles colliding. The problem is, The angle between the hypotenuse / x axis, and the angel between the hypotenuse / y axis never go above 65 degrees and i have no clue why?
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class MainTest extends JFrame {
public rectt g = new rectt();
[Code] .....
View Replies
View Related
Jun 10, 2014
No problem setting background color for layouts, e.g. bdrPn.setBackground(new Background((new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY))));
But neither of the following are working for me, running JavaFX 8 on latest OS-X
scene = new Scene(bdrPn, winW, winH, Color.BLACK);
scene.setFill(Color.BLACK);
scene.setFill() worked fine for previous versions of JavaFX.
View Replies
View Related
Oct 20, 2014
I've just started working on a program and i'm already having difficulties with the program. I can't get the background color to change from the basic gray color. Maybe you can tell me what i'm doing wrong.
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.*;
import java.lang.Object;
import java.awt.Color;
public class FrameDemo{
public static void main(String args[]){
Toolkit toolkit = Toolkit.getDefaultToolkit ();
[Code]...
also if theres any way to shorten up what I have there, that would be great.
View Replies
View Related
Nov 24, 2009
If I want to read from file the word "red" And then the following is not the right way like in the code ,, How to set the Color name
File inputFile = new File ("C:\input.txt");
Scanner scan = new Scanner (inputFile);
String RedColor=scan.nextLine();
Color backColor = new Color(redColor);
View Replies
View Related
Jun 4, 2015
In vb.Net, when I do the code msgbox("hello world") and put it in a loop, it will execute that command and show a new messagebox for every time the code goes through the loop. I am wanting to do something similar with java but I'm afraid I don't know how.I know that the following code will display a messagebox as I am describing it
Java Code:
JOptionPane.showMessageDialog(null, "message", "title", 1); mh_sh_highlight_all('java');
But when put in a loop, it does two things:
1) It hangs the program
2) It will only display the first message box and when you press ok, the other message boxes will be disregarded and not displayed (I'm assuming because of the hang).
View Replies
View Related
Nov 25, 2014
For some reason the two JOptionPane message boxes at the end of my code don't seem to open when I run the program.
package assg2.kevin;
import java.awt.HeadlessException;
import javax.swing.*;
import java.util.Arrays;
import java.util.Random;
public class Assg2Kevin {
[Code] ....
View Replies
View Related
Nov 29, 2014
for my class we have to make a rock paper scissors game using GUI boxes. Everything is working fine I'm just having trouble displaying what the computer chose. In other words, after the user selects Rock, paper or scissors, the scoreboard just pops up displaying the score. Here's the last working program before I started tinkering with it.
// your code goes here
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
[Code].....
View Replies
View Related
Jul 9, 2014
I am able to generate text boxes. But I need one specific thing in that:
Through a drop-down I should select a number and those many text boxes should get generated.
Suppose I selected 4 from the drop down list. Then on screen it should display:
Student 1: Textbox
Student2: Textbox
Student3: Textbox
Student4: Textbox
View Replies
View Related
Jun 17, 2015
I am trying to use JTextField. I have windows 7.I have jdk 8u45. I am trying to use multiple text boxes. I have written the following code:
import javax.swing.*;
import java.awt.*;
class Avg5Nums{
int num1, num2, num3, num4, num5;
double Avg;
Avg5Nums(int val1, int val2, int val3, int val4, int val5) {
[Code] ....
View Replies
View Related
May 17, 2014
I have the following task given to me (to be solved in NetBeans) :
View the contents of the file tuinman.txt: each line has a (real) number, followed by a line text.
Write the readGardener () method that reads the file completely. Only if the number is negative, write the rest of the line. (For each input line, a new line output.)
Extra: Adjust your method so you get the text after each negative number, written "word by word", among themselves. A word can contain characters that are not "letters" are (,,. '") - ,this you may ignore.
"tuinman.txt" is dutch for "gardener.txt". This txt file is located in the same map where build,nbproject,src, etc... are located. I couldn't solve this task so i looked at the solution. It didn't work... So i tried the solution of the extra bit:
package labo.pkg2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Methods {
public void readGardener() throws FileNotFoundException {
[Code] ....
The txt file is added as an attachment to this post.
The weird thing is, the first thing the program prints is "Van" "morgen" "ijlt",... wich are words from line 19 in the txt file. All the ones before line 19 the program won't print, even if the number is negative. Even weirder, the next thing that is printed after line 19 is the NUMBER from line 20, nowhere in this code do i tell the program to print a number, and yet it does... But that's not all! From line 19 on, he just prints every single thing in the document except for the negative number, if your mind didn't explode already, it sure did now!
So in short: I need some code that prints all the lines from the txt files that start with a negative number. Only the text may be printed though, there may be no numbers in the output!
The txt file, i couldn't add it as a attachment
123
-456,12 De tuinman en de dood
184,67 was hij, Kees, de enige die het zag, want niemand lette d'r op...
-845,14
-8,12452 Een Perzisch Edelman:
157,24 Dus daar gaan we dan.
[Code] ....
View Replies
View Related
Jan 23, 2015
I have to write an example program to fill boxes most efficiently with bags. All bags are the same size. The boxes come in Large, Medium and Small. Large boxes hold 20 bags. Medium boxes hold 10 bags. Small boxes hold 5 bags. I am getting input from JOptionPane input dialog box and then parsing that input to an integer.
I have to fill the large and medium boxes completely. I am not sure how to do this without an if statement. I can use one, but we have not covered it in class, so I am skeptical about using one to solve the problem. If I just divide the input number with integer division, I'm not going to get the remainder, right? So I'm thinking that I can mod my input number by 20 (the number of bags the user inputs) and then mod that number by 10 and then mod that number by 5, would that work?
View Replies
View Related
Sep 10, 2014
From what I can determine check boxes are applied in SWT to the first column only of a table.
final Table table_project = new Table(g3_tabfolder_projects, SWT.BORDER | SWT.CHECK);
However I require check boxes on all cells. I tried creating a table for each column (which gives me my check boxes) but as the number of enries are often numerous and thus require vertical scroll the tables become out of sync (and to the user that sync is important).
Before I knock up some code to try and synchronise the tables I just wanted to check any alternative approaches. The project is SWT based.
View Replies
View Related
Oct 22, 2014
My application (e.g., the print dialog box) needs to support multiple languages (user selected either at starting application or by switch in code).
How can this be achieved under Windows 7, Enterprise (works OK under Linux). I have tried : E.g.,
Locale.setDefault( .. );
JComponent.setDefaultLocale(Locale.LANG);
Setting resource bundle
but to no avail. The application always inherit the Win 7 system locale.
View Replies
View Related
Nov 7, 2014
I am creating a set of 3 subclasses, 1 superclass, and an application. In my instructions it says to make set methods in my super and subclass by using dialog boxes. In the application you have 3 different arrays where you create objects and are supposed to call the methods from the subclasses to be used in the application. I don't know how to make the dialog boxes from my subclasses to show up in my application.
View Replies
View Related
Nov 18, 2014
I'm having trouble setting up a test for my combination method.Here's the method (It basically finds the most amount of characters that both substrings share and combines the two using the number of shared characters as a breaking point):
public static String combination(String str1, String str2, int overlap) {
assert str1 != null : "Violation of: str1 is not null";
assert str2 != null : "Violation of: str2 is not null";
assert 0 <= overlap
&& overlap <= str1.length()
&& overlap <= str2.length()
&& str1.regionMatches(str1.length() - overlap, str2, 0, overlap) : ""
+ "Violation of: OVERLAPS(str1, str2, overlap)";
[code]....
View Replies
View Related
May 3, 2014
change these horizontally bouncing balls to vertically bouncing balls.
package PkgBallBounce2;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
[Code]....
View Replies
View Related
Feb 25, 2014
I understand the basics of using the JOptionPane Message boxes, but how do i take the information that is entered and store it as a file, or print it. I'm in my 6th week of class, and this happens to be maintenance related.
import javax.swing.JOptionPane;
public class Example_Maintenance_System{
public static void main (String [] args) {
//Operator Name Input
String operator_name;
operator_name = JOptionPane.showInputDialog("Please scan your barcode");
[code]....
View Replies
View Related
Mar 8, 2015
I have an object that has an instance of another object class as its parameter :
CombinationLock oneHundred = new CombinationLock(28,17,39);
Locker Mickey = new Locker(100, "Mickey", 3, oneHundred);
This is for a locker, which has a combination assigned to the student. Within the locker class I have the following constructor:
public Locker(int locker, String student, int numberOfBooks, CombinationLock combo) {
this.locker = locker;
this.combo = combo;
this.student = student;
this.numberOfBooks = numberOfBooks;
}
combo is the private CombinationLocker object I created within the Locker class. Do I need to pass the combo object on to the CombinationLock class? For reason, I do not comprehend, the combination password from the main class is not passing through to the CombinationLock class, and the combination values are all zero.
View Replies
View Related
Oct 3, 2014
I am teaching myself AWT/Swing ... It concerns an application that creates dynamic graphics using the Graphics class. I understand that the code that creates graphics should be put in a "paintComponent" method of a (descendant of a) JPanel class. And that when you want to change something on this panel outside "paintComponent", you have to call the "repaint" method, that somehow causes "paintComponent" to be invoked.
However, I don't fully understand the explanation of the example at [URL]... . It concerns an application that moves a red square when a mouse button is clicked. The code can be found at the link, but I also repeat it below.
package painting;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
[Code]...
This code indeed works. However, I don't understand why. The explanation at the Oracle site is the following.
Because we are manually setting the clip, our moveSquare method invokes the repaint method not once, but twice. The first invocation tells Swing to repaint the area of the component where the square previously was (the inherited behavior uses the UI Delegate to fill that area with the current background color.) The second invocation paints the area of the component where the square currently is.
However, what I don't understand: when repaint is first invoked, squareX and squareY still have their old values. If the invocation of repaint simply causes an invocation of paintComponent, then paintComponent should draw a red square at (squareX, squareY). Just like the second invocation of repaint causes paintComponent to draw a red suare at the new values of (squareX, squareY). Instead, the first invocation results in the red square being wiped out by the background color.
So apparently, the first invocation of repaint does not simply cause an invocation of paintComponent. What does it do?
View Replies
View Related
Oct 18, 2014
How to create text fields, labels and input boxes on a GUI, we haven't covered these in class as of yet, but I want my project to stand out so I'd like to know how to build a GUI now.
View Replies
View Related
Oct 23, 2014
I need to use a counter to keep track of user's input. In this class I am trying to create a combination lock that takes three strings. In the tester class the user inserts three strings and both are compared to see if users input matches correct combination. I have no errors only problem is the setPosition method. My output is always false.
public class CombinationLock {
private String first;
private String second;
private String third;
private String firstGuess;
private String secondGuess;
private String thirdGuess;
private boolean open;
private int counter;
[code]....
View Replies
View Related
Jun 9, 2014
The problem I am trying to solve relates to block-matching or image-within-image recognition. This is an algorithm problem I am working on in java, but computationally, my computer can't handle generating all the combinations at one time.
I see an image, extract the [x,y] of every black pixel and create a set for that image, such as
{[8,0], [9,0], [11,0]}
The set is then augmented so that the first pixel in the set is at [0,0], but the relationship of the pixels is preserved. For example, I see {[8,0], [9,0]} and change the set to {[0,0], [1,0]}. The point of the extraction is that now if I see {[4,0], [5,0]}, I can recognize that basic relationship as two vertically adjacent pixels, my {[0,0], [1,0]}, since it is the same image but only in a different location.
I have a list of these pixel sets, called "seen images". Each 'seen image' has a unique identifier, that allows it to be used as a nested component of other sets. For example:
{[0,0], [1,0]} has the identifier 'Z'
So if I see:
{[0,0], [1, 0], [5,6]}
I can identify and store it as:
{[z], [5, 6]}
The problem with this is that I have to generate every combination of [x,y]'s within the pixel set to check for a pattern match, and to build the best representation. Using the previous example, I have to check:
{[0,0], [1,0]},
{[0,0], [5,6]},
{[1,0], [5,6]} which is {[0,0], [4,5]}
{[0,0], [1,0], [5,6]}
And then if a match occurs, that subset gets replaced with it's ID, merged with the remainder of the original set, and the new combination needs to be checked if it is a 'seen image':
{[z],[5, 6]}
The point of all that is to match as many of the [x,y]'s possible, using the fewest pre-existing pieces as components, to represent a newly seen image concisely. The greedy solution to get the component that matches the largest subset is not the right one. Complexity arises in generating all of the combinations that I need to check, and then the combinations that spawn from finding a match, meaning that if some match and swap produces {[z], [1,0], [2,0]}, then I need to check (and if matched, repeat the process):
{[z], [1,0]}
{[z], [2,0]}
{[1,0], [2,0]} which is {[0,0], [1,0]}
{[z], [1,0], [2,0]}
Currently I generate the pixel combinations this way (here I use numbers to represent pixels 1 == [x,y]) Ex. (1, 2, 3, 4): Make 3 lists:
1.) 2.) 3.)
12 23 34
13 24
14
Then for each number, for each list starting at that number index + 1, concatenate the number and each item and store on the appropriate list, ex. (1+23) = 123, (1+24) = 124
1.) 2.) 3.)
12 23 34
13 24
14
---- ---- ----
123 234
124
134
So those are all the combinations I need to check if they are in my 'seen images'. This is a bad way to do this whole process. I have considered different variations / optimizations, including once the second half of a list has been generated (below the ----), check every item on the list for matches, and then destroy the list to save space, and then continue generating combinations. Another option would be to generate a single combination, and then check it for a match, and somehow index the combinations so you know which one to generate next.
How to optimize what I am doing for a set of ~million items. I also have not yet come up with a non-recursive or efficient way to handle that each match generates additional combinations to check.
View Replies
View Related