Go to Page in epub reader (PageTurner) - java

I have to implement Go To Page feature in epub reader. I have try to implement this feature in source code of Page-Turner, but its not working successfully because of multiple xhtml in .epub file, as we know that each chapter have single xhtml file and its divide as per screen size in this app. So whenever screen size is big then total number of pages are less and more number of pages when screen is small, So there is no fix page number where to jump. I have edit and try to implement like giver below.
ReadingFragment.java
public void performSearch(String query) {
int index = Integer.parseInt(query);
if (index > bookView.getTotalNumberOfPages()) {
Toast.makeText(context, "Please enter number between 0 to " + bookView.getTotalNumberOfPages(), Toast.LENGTH_SHORT).show();
} else {
bookView.gotoPageNumber(index);
}
}
BookView.java
public void gotoPageNumber(int pageNum) {
strategy.gotoPage(pageNum);
progressUpdate();
}
PageChangeStrategy.java
public void gotoPage(int pageNumber);
FixedPagesStrategy.java
#Override
public void gotoPage(int pageNumber) {
PageTurnerSpine spinePos = bookView.getSpine();
this.storedPosition = -1;
int currentPage = getCurrentPage() + spinePos.getUptoPage(spinePos.getPosition());
Log.e(TAG, "Adding >> Upto Page : " + spinePos.getUptoPage(spinePos.getPosition())
+ ", currentPage : " + getCurrentPage());
Log.e(TAG, "pagenum : " + pageNum);
if (pageNumber > currentPage) { //pageNumber is greater then current page
int jumpSpine = spinePos.getIndexFromPage(pageNumber);
int currentSpine = spinePos.getPosition();
Log.e(TAG, "jumpSpine : " + jumpSpine + ", currentSpine : " + currentSpine);
if (jumpSpine == currentSpine) {
int diffrence = pageNumber - currentPage;
Log.e(TAG, "diffrence < : " + diffrence);
Log.e(TAG, "Minimum >> PageOffSets - 1 : " + (spinePos.getPageOffSets(currentSpine) - 1)
+ ", pageNum + diffrence : " + (pageNum + diffrence));
this.pageNum = Math.min(pageNum + diffrence, spinePos.getPageOffSets(currentSpine) - 1);
updatePosition();
} else {
PageTurnerSpine spine = bookView.getSpine();
if (spine == null || !spine.navigateFrontSpine(spine.getIndexFromPage(pageNumber))) {
return;
}
this.pageNum = 0;
gotoPage(pageNumber);
}
} else { //pageNumber is less then current page
int jumpSpine = spinePos.getIndexFromPage(pageNumber);
int currentSpine = spinePos.getPosition();
Log.e(TAG, "jumpSpine : " + jumpSpine + ", currentSpine : " + currentSpine);
if (jumpSpine == currentSpine) {
int diffrence = currentPage - pageNumber;
Log.e(TAG, "diffrence > : " + diffrence);
Log.e(TAG, "pagenum - diffrence : " + (pageNum - diffrence));
this.pageNum = Math.max(pageNum - diffrence, 0);
updatePosition();
} else {
PageTurnerSpine spine = bookView.getSpine();
if (spine == null || !spine.navigateBackSpine(spine.getIndexFromPage(pageNumber))) {
return;
}
this.pageNum = 0;
gotoPage(pageNumber);
}
}
Log.e(TAG, "In last pageNum : " + pageNum);
}
PageTurnerSpine.java
public int getIndexFromPage(int pageNumber) {
int total = 0;
int totalIndex = 0;
for (List<Integer> pagesPerSection : pageOffsets) {
total += pagesPerSection.size();
totalIndex = totalIndex + 1;
if (total - 1 >= pageNumber) {
return totalIndex - 1;
}
}
return 0;
}
public int getUptoPage(int position) {
int total = 0, max = 0;
for (List<Integer> pagesPerSection : pageOffsets) {
max = max + 1;
if (position == max - 1) {
return total;
}
total += pagesPerSection.size();
}
return 0;
}
public int getPageOffSets(int position) {
int max = 0;
for (List<Integer> pagesPerSection : pageOffsets) {
max = max + 1;
if (position == max - 1) {
return pagesPerSection.size();
}
}
return 0;
}
public boolean navigateFrontSpine(int indexSpine) {
if (this.position == size() - 1) {
return false;
}
this.position = indexSpine;
return true;
}
public boolean navigateBackSpine(int indexSpine) {
if (this.position == 0) {
return false;
}
this.position = indexSpine;
return true;
}
When i apply this code and run some time it work correctly. but some time it jump on other page number, like if i enter 110 then it will jump on 109. and when i am trying to jump on chapter starting page then contain dose not changes. Please help me.
https://github.com/NightWhistler/PageTurner
In this source code i have edited some file given above. they already exists in this project.
Mostly i see that most of epub reader like Kindle, FBReader, etc... does not implement Go To Page feature. So, i want to also know that is it possible to implement this feature or not?
Thanks for Help :)

You really need to consult the IDPF Epub standards at IDPF.org/epub .
One approach, that I think is optional within the standard (but am not sure about) is to mark the content with the physical page numbers from the paper book (if there is one), or decide on your own numbering system along with your Table of Contents, and use a corresponding virtual page as the start of it.
This enables going to the start of the same page, but the number of virtual pages per physical page will vary, depending on font sizes etc. currently in use.
It's a data issue as much as a programming one.

Related

I have some problems with ArrayList (quiz of head first java)

I've just started learning java since last week. I'm using book called 'head first java' and i'm struggling with solving problems about ArrayList. Error says "The method setLocationCells(ArrayList) in the type DotCom is not applicable for the
arguments (int[])" and I haven't found the solution :( help me..!
enter image description here
This looks like a Locate & Conquer type game similar to the game named Battleship with the exception that this game is a single player game played with a single hidden ship in a single horizontal row of columnar characters. Rather simplistic but kind of fun to play I suppose. The hard part is to locate the hidden ship but once you've located it, conquering (sinking) it becomes relatively easy. I'm sure this isn't the games' intent since it is after all named "The Dot Com Game" but the analogy could be possibly helpful.
There are several issues with your code but there are two major ones that just can not be there for the game to work:
Issue #1: The call to the DotCom.setLocationCells() method:
The initial problem is located within the DotComGame class on code line 13 (as the Exception indicates) where the call is made to the DotCom.setLocationCells() method. As already mentioned in comments the wrong parameter type is passed to this method. You can not pass an int[] Array to the setLocationCell() method when this method contains a parameter signature that stipulates it requires an ArrayList object. The best solution in my opinion would be to satisfy the setLocationCells() method parameter requirement...supply an ArrayList to this method.
The reason I say this is because all methods within the DotCom class work with an established ArrayList and one of the tasks of one of these methods (the checkYourself() method) actually removes elements from the ArrayList which is easy to do from a collection but very cumbersome to do the same from an Array.
To fix this problem you will need to change the data type for the locations variable located within the DotComGame class. Instead of using:
int[] locations = {randomNum, randomNum + 1, randomNum + 2};
you should have:
ArrayList<Integer> locations = new ArrayList<>(
Arrays.asList(random, randomNum + 1, randomNum + 2));
or you could do it this way:
ArrayList<Integer> locations = new ArrayList<>();
locations.add(randomNum);
locations.add(randomNum + 1);
locations.add(randomNum + 2);
There are other ways but these will do for now. Now, when the call to the setLocationCells() method is made you ahouldn't get an exception this issue should now be resolved.
Issue #2: The call to the DotCom.checkYourself() method:
Again, this particular issue is located within the DotComGame class on code line 18 where the call is made to the DotCom.checkYourself() method. Yet another parameter data type mismatch. You are trying to pass a variable of type String (named guess) to this method whereas its signature stipulates that it requires an integer (int) value. That again is a no go.
To fix this problem you will need to convert the string numerical value held by the guess variable to an Integer (int) value. So instead of having this:
while(isAlive) {
String guess = helper.getUserInput("Enter a Number: ");
String result = theDotCom.checkYourself(guess);
// ... The rest of your while loop code ...
}
you should have something like:
while(isAlive) {
String guess = helper.getUserInput("Enter a Number: ");
/* Validate. Ensure guess holds a string representation
of a Integer numerical value. */
if (!guess.matches("\\d+")) {
System.err.println("Invalid Value (" + guess
+ ") Supplied! Try again...");
continue;
}
int guessNum = Integer.parseInt(guess);
String result = theDotCom.checkYourself(guessNum);
numOfGuesses++;
if (result.equals("kill")) {
isAlive = false;
System.out.println(numOfGuesses + " guesses!");
}
else if (result.equals("hit")) {
// Do Something If You Like
System.out.println("HIT!");
}
else {
System.out.println("Missed!");
}
}
Below is a game named Simple Battleship which I based off of your code images (please don't use images for code anymore - I hate using online OCR's ;)
BattleshipGame.java - The application start class:
import java.awt.Toolkit;
public class BattleshipGame {
public static int gameLineLength = 10;
public static void main(String[] args) {
GameHelper helper = new GameHelper();
Battleship theDotCom = new Battleship();
int score = 0; // For keeping an overall score
// Display About the game...
System.out.println("Simple Battleship Game");
System.out.println("======================");
System.out.println("In this game you will be displayed a line of dashes.");
System.out.println("Each dash has the potential to hide a section of a");
System.out.println("hidden Battleship. The size of this ship is randomly");
System.out.println("chosen by the game engine and can be from 1 to 5 sections");
System.out.println("(characters) in length. The score for each battle is based");
System.out.println("on the length of the game line that will be displayed to");
System.out.println("you (default is a minimum of 10 charaters). You now have");
System.out.println("the option to supply the game line length you want to play");
System.out.println("with. If you want to use the default then just hit ENTER:");
System.out.println();
// Get the desire game line length
String length = helper.getUserInput("Desired Game Line Length: --> ", "Integer", true, 10, 10000);
if (!length.isEmpty()) {
gameLineLength = Integer.parseInt(length);
}
System.out.println();
// Loop to allow for continuous play...
boolean alwaysReplay = true;
while (alwaysReplay) {
int numOfGuesses = 0;
/* Create a random ship size to hide within the line.
It could be a size from 1 to 5 characters in length. */
int shipSize = new java.util.Random().nextInt((5 - 1) + 1) + 1;
int randomNum = (int) (Math.random() * (gameLineLength - (shipSize - 1)));
int[] locations = new int[shipSize];
for (int i = 0; i < locations.length; i++) {
locations[i] = randomNum + i;
}
System.out.println("Destroy the " + shipSize + " character ship hidden in the");
System.out.println("displayed line below:");
System.out.println();
String gameLine = String.join("", java.util.Collections.nCopies(gameLineLength, "-"));
theDotCom.setLocationCells(locations);
// Play current round...
boolean isAlive = true;
while (isAlive == true) {
System.out.println(gameLine);
String guess = helper.getUserInput("Enter a number from 1 to " + gameLineLength
+ " (0 to quit): --> ", "Integer", 1, gameLineLength);
int idx = Integer.parseInt(guess);
if (idx == 0) {
System.out.println("Quiting with an overall score of: " + score + " ... Bye-Bye");
alwaysReplay = false;
break;
}
idx = idx - 1;
String result = theDotCom.checkYourself(idx);
numOfGuesses++;
System.out.println(result);
if (result.equalsIgnoreCase("kill")) {
Toolkit.getDefaultToolkit().beep();
isAlive = false;
/* Tally the score dependent upon the gameLineLength... */
if (gameLineLength <= 10) { score += 5; }
else if (gameLineLength > 10 && gameLineLength <= 20) { score += 10; }
else if (gameLineLength > 20 && gameLineLength <= 30) { score += 15; }
else if (gameLineLength > 30 && gameLineLength <= 40) { score += 20; }
else { score += 25; }
gameLine = gameLine.substring(0, idx) + "x" + gameLine.substring(idx + 1);
System.out.println(gameLine);
System.out.println(numOfGuesses + " guesses were made to sink the hidden ship.");
System.out.println("Your overall score is: " + (score < 0 ? 0 : score));
}
else if (result.equalsIgnoreCase("hit")) {
gameLine = gameLine.substring(0, idx) + "x" + gameLine.substring(idx + 1);
}
if (result.equalsIgnoreCase("miss")) {
score -= 1;
}
System.out.println();
}
// Play Again? [but only if 'alwaysReplay' holds true]
if (alwaysReplay) {
String res = helper.getAnything("<< Press ENTER to play again >>\n"
+ "<< or enter 'q' to quit >>");
if (res.equalsIgnoreCase("q")) {
System.out.println("Quiting with an overall score of: " + score + " ... Bye-Bye");
break;
}
System.out.println();
}
}
}
}
GameHelper.java - The GameHelper class:
import java.util.Scanner;
public class GameHelper {
private final Scanner in = new Scanner(System.in);
public String getUserInput(String prompt, String responseType, int... minMAX) {
int min = 0, max = 0;
if (minMAX.length == 2) {
min = minMAX[0];
max = minMAX[1];
}
if (minMAX.length > 0 && min < 1 || max < 1) {
throw new IllegalArgumentException("\n\ngetUserInput() Method Error! "
+ "The optional parameters 'min' and or 'max' can not be 0!\n\n");
}
String response = "";
while (response.isEmpty()) {
if (prompt.trim().endsWith("-->")) {
System.out.print(prompt);
}
else {
System.out.println(prompt);
}
response = in.nextLine().trim();
if (responseType.matches("(?i)\\b(int|integer|float|double)\\b")) {
if (!response.matches("-?\\d+(\\.\\d+)?") ||
(responseType.toLowerCase().startsWith("int") && response.contains("."))) {
System.err.println("Invalid Entry (" + response + ")! Try again...");
response = "";
continue;
}
}
// Check entry range value if the entry is to be an Integer
if (responseType.toLowerCase().startsWith("int")) {
int i = Integer.parseInt(response);
if (i != 0 && (i < min || i > max)) {
System.err.println("Invalid Entry (" + response + ")! Try again...");
response = "";
}
}
}
return response;
}
public String getUserInput(String prompt, String responseType, boolean allowNothing, int... minMAX) {
int min = 0, max = 0;
if (minMAX.length == 2) {
min = minMAX[0];
max = minMAX[1];
}
if (minMAX.length > 0 && min < 1 || max < 1) {
throw new IllegalArgumentException("\n\ngetUserInput() Method Error! "
+ "The optional parameters 'min' and or 'max' can not be 0!\n\n");
}
String response = "";
while (response.isEmpty()) {
if (prompt.trim().endsWith("-->")) {
System.out.print(prompt);
}
else {
System.out.println(prompt);
}
response = in.nextLine().trim();
if (response.isEmpty() && allowNothing) {
return "";
}
if (responseType.matches("(?i)\\b(int|integer|float|double)\\b")) {
if (!response.matches("-?\\d+(\\.\\d+)?") ||
(responseType.toLowerCase().startsWith("int") && response.contains("."))) {
System.err.println("Invalid Entry (" + response + ")! Try again...");
response = "";
continue;
}
}
// Check entry range value if the entry is to be an Integer
if (responseType.toLowerCase().startsWith("int")) {
int i = Integer.parseInt(response);
if (i != 0 && (i < min || i > max)) {
System.err.println("Invalid Entry (" + response + ")! Try again...");
response = "";
}
}
}
return response;
}
public String getAnything(String prompt) {
if (prompt.trim().endsWith("-->")) {
System.out.print(prompt);
}
else {
System.out.println(prompt);
}
return in.nextLine().trim();
}
}
Battleship.java - The Battleship class:
import java.util.ArrayList;
public class Battleship {
private ArrayList<Integer> locationCells;
public void setLocationCells(java.util.ArrayList<Integer> loc) {
locationCells = loc;
}
// Overload Method (Java8+)
public void setLocationCells(int[] loc) {
locationCells = java.util.stream.IntStream.of(loc)
.boxed()
.collect(java.util.stream.Collectors
.toCollection(java.util.ArrayList::new));
}
/*
// Overload Method (Before Java8)
public void setLocationCells(int[] loc) {
// Clear the ArrayList in case it was previously loaded.
locationCells.clear();
// Fill the ArrayList with integer elements from the loc int[] Array
for (int i = 0; i < loc.length; i++) {
locationCells.add(loc[i]);
}
}
*/
/**
* Completely removes one supplied Integer value from all elements
* within the supplied Integer Array if it exist.<br><br>
*
* <b>Example Usage:</b><pre>
*
* {#code int[] a = {103, 104, 100, 10023, 10, 140, 2065};
* a = removeFromArray(a, 104);
* System.out.println(Arrays.toString(a);
*
* // Output will be: [103, 100, 10023, 10, 140, 2065]}</pre>
*
* #param srcArray (Integer Array) The Integer Array to remove elemental
* Integers from.<br>
*
* #param intToDelete (int) The Integer to remove from elements within the
* supplied Integer Array.<br>
*
* #return A Integer Array with the desired elemental Integers removed.
*/
public static int[] removeFromArray(int[] srcArray, int intToDelete) {
int[] arr = {};
int cnt = 0;
boolean deleteIt = false;
for (int i = 0; i < srcArray.length; i++) {
if (srcArray[i] != intToDelete) {
arr[cnt] = srcArray[i];
cnt++;
}
}
return arr;
}
public String checkYourself(int userInput) {
String result = "MISS";
int index = locationCells.indexOf(userInput);
if (index >= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "KILL";
}
else {
result = "HIT";
}
}
return result;
}
}

Sort Algorithm Java

Right now i'm struggeling with a basic algorithm, that shall sort a linked list. I have two additional linked lists (in the beginning empty), in which i can copy the Integer Objects of the first linkedlist.
My problem is, that all of my tries simply doesn't work. In the copied example at the bottom, it goes through both of the while loops, but i don't know how to loop everything, until everything is sorted in the third linked list (zug3.zug3). Also i shall compare the actual smallest value of zug1 to the smallest of zug2 and then continue sorting in the list where the value is smaller. That is not possible at the start of sorting, because if i wanna getSmallest() of an empty List, it will get a null pointer exception.
I'm tryin this now since three days with different, for-loops, while-loops, if-else sentences but i don't find out, how to make it work accurate.
Please help!
Example of the Programm:
public class Abstellgleis {
LinkedList<Integer> zug1 = new LinkedList<Integer>();
void initialize() {
for (int i = 0; i <15;i++) {
Random zahl = new Random();
int integer = zahl.nextInt(15);
zug1.add(integer);
}
}
public void wagenAnkoppeln(int i) {
zug1.addFirst(i);
}
int wagenAbkoppeln() {
int waggonNummer = zug1.getFirst();
zug1.removeFirst();
return waggonNummer;
}
int getSmallest() {
int smallest = zug1.size();
for( int i =1; i <zug1.size()-1; i++)
{
if(zug1.get(i) < smallest )
{
//int smallest = integers.get(Oedipus);
smallest = zug1.get(i);
}
}
return smallest;
}
}
public class Rangiergleis {
LinkedList<Integer> zug2 = new LinkedList<Integer>();
void waggonAnkoppeln(int i) {
zug2.addFirst(i);
}
int waggonAbkoppeln() {
int waggonNummer = zug2.getFirst();
zug2.removeFirst();
return waggonNummer;
}
int getSmallest() {
int smallest = 100;
for (int i = 0; i < zug2.size() - 1; i++) {
if (zug2.get(i) < smallest) {
smallest=zug2.get(i);
}
}
return smallest;
}
}
public class Zuggleis {
LinkedList<Integer> zug3 = new LinkedList<Integer>();
void waggonAnkoppeln(int i) {
zug3.addLast(i);
}
}
public class Steuerung {
public static void main(String[] args) {
Abstellgleis zug1 = new Abstellgleis();
zug1.initialize();
Rangiergleis zug2 = new Rangiergleis();
Zuggleis zug3 = new Zuggleis();
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Abstellgleis: " + zug3.zug3);
while (!zug1.zug1.isEmpty()) {
if (zug1.zug1.getFirst() != zug1.getSmallest()) {
zug2.waggonAnkoppeln(zug1.zug1.getFirst());
System.out.println("Vom Abstellgleis wurde Wagen " +
zug1.zug1.getFirst() + " aufs Rangiergleis bewegt");
zug1.zug1.removeFirst();
}
else if (zug1.zug1.getFirst() == zug1.getSmallest()) {
zug3.waggonAnkoppeln(zug1.zug1.getFirst());
System.out.println(zug1.zug1.getFirst() + "wurde aufs Zuggleis bewegt");
zug1.zug1.removeFirst();
}
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Zuggleis: " + zug3.zug3);
}
while (!zug2.zug2.isEmpty()) {
if (zug2.zug2.getFirst() != zug2.getSmallest()) {
zug1.wagenAnkoppeln(zug2.zug2.getFirst());
System.out.println("Vom Rangiergleis wurde Wagen " +
zug2.zug2.getFirst() + " aufs Abstellgleis bewegt");
zug2.zug2.removeFirst();
}
else if (zug2.zug2.getFirst() == zug2.getSmallest()) {
zug3.waggonAnkoppeln(zug2.zug2.getFirst());
System.out.println(zug2.zug2.getFirst() + " wurde vom Rangiergleis aufs Zuggleis bewegt");
zug2.zug2.removeFirst();
}
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Zuggleis: " + zug3.zug3);
}
if (zug1.zug1.isEmpty()) {
while (!zug2.zug2.isEmpty())
if (zug2.zug2.getFirst() != zug2.getSmallest()) {
zug1.wagenAnkoppeln(zug2.zug2.getFirst());
System.out.println("Vom Abstellgleis wurde Wagen " +
zug2.zug2.getFirst() + " aufs Rangiergleis bewegt");
zug2.zug2.removeFirst();
}
else if (zug2.zug2.getFirst() == zug2.getSmallest()) {
zug3.waggonAnkoppeln(zug2.zug2.getFirst());
System.out.println(zug2.zug2.getFirst() + "wurde aufs Zuggleis bewegt");
zug2.zug2.removeFirst();
}
System.out.println("Abstellgleis:" + zug1.zug1);
System.out.println("Rangiergleis: " + zug2.zug2);
System.out.println("Zuggleis: " + zug3.zug3);
}
}
}
Your getSmallest methods in Abstellgleis and Rangiergleis don’t look right. In the first you start by setting smallest to zug1.size(). First time when the size is 15 this is probably fine, but as the Zug grows shorter, there may come a point when the size is smaller than the smallest element, and then your method will give the wrong result. In Rangiergleis you are initializing to 100, that’s sounder. In both methods you are missing the last element. For example in Abstellgleis.getSmallest():
for( int i =1; i <zug1.size()-1; i++)
This is in fact missing both the first and the last element. Elements are indexed 0 through zug1.size() - 1, so it should be one of the two following:
for (int i = 0; i < zug1.size(); i++) {
for (int i = 0; i <= zug1.size() - 1; i++) {
The former would be conventional. If you are sure there is at least one wagon in the train, you may of course initialize smallest to zug1.get(0) and the have the loop run from 1 (this could have been what you intended).
In Rangiergleis.getSmallest() your loop runs from 0 as it should, but is missing the last element in the same way as in Abstellgleis.

Setting band level in Equalizer not working

I am writing an app that plays 4 mp3 files simultaneously using 4 MediaPlayer objects. I have added 3 custom knobs to control bass, mid, and treble using 4 equalizers, each attached to its specific MediaPlayer. I have verified that I am passing the correct values (in Millibels) to setBandLevel for each as specified, but I don't hear any difference in sound when spinning the controls. I have added the relevant code below.
One thing I noticed that seems really odd is that the Equalizer method I am calling expects a short, but the frequency range upper limits come back as high as 6,000,000 millibels. I may be missing something here, but a short in android is 2^16 which should mean an upper limit of 64k.
public void setBandLevel(short band, short level)
The code runs without hitting exceptions and I have traces all values to verify each frequency is within its band's range, but I get no variance in sound when I spin the control knobs. I have tested this on K, L and M with the same results.
I appreciate any insight into this.
public void onResume() {
super.onResume();
// Load an ad into the AdMob banner view.
AdView adView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
//initialize views
initializeViews();
setInstrumentNames();
mediaPlayer1.start();
mediaPlayer2.start();
mediaPlayer3.start();
mediaPlayer4.start();
// Start the songtime thread and remember its object so it can be stopped legally
play();
setEqualizer();
setupKnobButtons();
}
public void setEqualizer() {
eq1 = new Equalizer(0, mediaPlayer1.getAudioSessionId());
eq2 = new Equalizer(0, mediaPlayer2.getAudioSessionId());
eq3 = new Equalizer(0, mediaPlayer3.getAudioSessionId());
eq4 = new Equalizer(0, mediaPlayer4.getAudioSessionId());
if (eq1 != null && eq2 != null && eq3 != null && eq4 != null) {
int z = eq1.setEnabled(true);
int c = eq2.setEnabled(true);
int d = eq3.setEnabled(true);
int e = eq4.setEnabled(true);
// The number of bands is usually 5 but could be up to 13
num_bands = eq1.getNumberOfBands();
// The band range will always return the min and max
// Gets the level range for use by setBandLevel(short,short)}. The level is expressed in
// milliBel. Return the band level range in an array of short integers. The first element is the lower
// limit of the range, the second element the upper limit.
// --- THIS IS FOR THE FULL RANGE OF ALL BANDS ---///
range = eq1.getBandLevelRange();
min_level = range[0];
max_level = range[1];
Log.d(TAG_BANDLEVELS, "min level = " + min_level + " max level = " + max_level);
// Loop through all the bands ( usually 5 ) and create 2 arrays
// ( one for lower limit of each band and one for the upper )
for (int i = 0; i < num_bands; i++) {
freq_range = eq1.getBandFreqRange((short) i);
freqRangeLowerLimit.add(freq_range[0]);
freqRangeUpperLimit.add(freq_range[1]);
Log.d(TAG_BANDLEVELS, "freqRangeLowerLimit = " + freq_range[0] +
" freqRangeUpperLimit = " + freq_range[1] + " band = " + i);
}
}
}
private void setBandLevels(int percentage, int new_level1, int new_level2, int nBand) {
if (eq1.hasControl()) {
eq1.setBandLevel((short) nBand, (short) new_level1);
}
else
Log.d(TAG_BANDLEVELS, "EQ 1 DOES NOT HAVE CONTROL");
if (eq2.hasControl()) {
eq2.setBandLevel((short) nBand, (short) new_level1);
}
else
Log.d(TAG_BANDLEVELS, "EQ 2 DOES NOT HAVE CONTROL");
if (eq3.hasControl()) {
eq3.setBandLevel((short) nBand, (short) new_level1);
}
else
Log.d(TAG_BANDLEVELS, "EQ 3 DOES NOT HAVE CONTROL");
if (eq4.hasControl()) {
eq4.setBandLevel((short) nBand, (short) new_level1);
}
else
Log.d(TAG_BANDLEVELS, "EQ 4 DOES NOT HAVE CONTROL");
// Mid and Treble use 2 bands (Mid = 2nd and 3rd Treble = 4th and 5th
if (nBand == PlayFragment.BAND_BASS || nBand == PlayFragment.BAND_TREBLE) {
try {
eq1.setBandLevel((short) (nBand+1), (short) new_level2);
eq2.setBandLevel((short) (nBand+1), (short) new_level2);
eq3.setBandLevel((short) (nBand+1), (short) new_level2);
eq4.setBandLevel((short) (nBand+1), (short) new_level2);
} catch (Throwable ex2) {
ex2.printStackTrace();
}
}
}
public void setupKnobButtons() {
RoundKnobButton rnbBass = (RoundKnobButton) getView().findViewById(R.id.btRoundBass);
rnbBass.SetListener(new RoundKnobButtonListener() {
TextView tvEQBass = (TextView) getView().findViewById(R.id.tvEQBass);
public void onStateChange(boolean newstate) {
Toast.makeText(getActivity(), "New state:" + newstate, Toast.LENGTH_SHORT).show();
}
public void onRotate(final int percentage) {
tvEQBass.post(new Runnable() {
public void run() {
tvEQBass.setText("\n" + percentage + "%\n");
// Bass only uses the first band
int lower1 = freqRangeLowerLimit.get(0);
int upper1 = freqRangeUpperLimit.get(0);
int new_level1 = lower1 + ((upper1 - lower1) * percentage/100);
// new_level1 /= 1000;
// int new_level1 = calc1/100;
int lower2 = freqRangeLowerLimit.get(1);
int upper2 = freqRangeUpperLimit.get(1);
int new_level2 = lower1 + ((upper2 - lower2) * percentage/100);
// new_level2 /= 1000;
// int new_level2 = calc2/100;
setBandLevels(percentage, new_level1, new_level2, BAND_BASS);
Log.d(TAG_BANDLEVELS, "Band = Bass " + "New Level1 = " + new_level1 + "New Level2 = " + new_level2 + " Percentage = " + percentage);
}
});
}
});

ArrayList vs. Array. Why is one working and one isn't?

I've been trying to switch an older assignment over from an array to an arraylist, but for whatever reason my find method is not working when I modify it to use arrayList.. Seems to always be returning -1.
This is part of a large class so I don't want to include everything unless completely necessary, but I did include the declarations in case they are important:
public class Switch {
private SwitchEntry[] camTable;
private int numEntries;
private int maxEntries;
public Switch() {
camTable = new SwitchEntry[100]; // default value
numEntries = 0;
maxEntries = 100;
}
public Switch(int maxEntries) {
camTable = new SwitchEntry[maxEntries];
numEntries = 0;
this.maxEntries = maxEntries;
}
Original:
public int find (MACAddress source) {
int found = -1;
for (int i=0; i < numEntries; i++)
if (source.isEqual (camTable[i].getAddress())){
found = i;
break;
}
return found;
}
Modified:
public int find (MACAddress source) {
int found = -1;
for (int i=0; i < numEntries; i++)
if (source.isEqual (camTable.get(i).getAddress())){
found = i;
break;
}
return found;
}
Where numEntries is modified & where the new entries are added into the arrayList:
public void processFrame(Frame inFrame) {
// first, add source MAC to camTable if not already there
if (find(inFrame.getSource()) == -1) {
if (numEntries >= maxEntries) {
System.out.println ("Error...camTable is full - cannot add " + inFrame.getSource());
} else {
camTable.add(new SwitchEntry(inFrame.getSource(), inFrame.getPort())); //PROBLEM LINE
System.out.println ("Adding " + inFrame.getSource() + " to camTable");
}
}
//process frame
int index = find(inFrame.getDestination());
if (index != -1){
System.out.print ("Sending frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
System.out.println (" out port " + camTable.get(index).getPort() );
} else {
System.out.print ("Flooding frame with data " + inFrame.getData() + " from " + inFrame.getSource() + " to " + inFrame.getDestination());
System.out.println (" out all ports" );
}
}
Solution:
camTable.add(numEntries++, new SwitchEntry(inFrame.getSource(),inFrame.getPort()));
Try This
public int find (MACAddress source) {
int found = -1;
ArrayList<MACAddress> camTable = new ArrayList<MACAddress>();
ListIterator<MACAddress> itr = camTable.listIterator();
while(itr.hasNext()){
MACAddress tempAdd = itr.next();
if(source.getAddress().equals(tempAdd.getAddress())){
found = itr.nextIndex();
return found;
}
return found;
}
I assume in ArrayList you store the objects of MACAddress. in if condition i check the source.getAddress to tempAdd.getAddress() is same then it will retun index of ArrayList. here ArrayList is local variable but you can create as a class variable
Use Contain method of collection.(ArrayList)
http://www.tutorialspoint.com/java/util/arraylist_contains.htm
Solution was straight-forward and just an oversight by me. All I had to do was add numEntries back into my add statement, which I neglected to fix after changing from an array to arrayList
Solution is posted in the original question now:

load a world asynchronously or without blocking the main thread

I'm working on a minigames plugin. After an arena finishes it should be regenerated - I use the unload and load trick. It has an obvious disadvantage - it freezes the server for a while to prepare spawn areas. I decided to put the arena reset code into an runnable asynchronous task runTaskAsynchronously(). However, when the server tries to run the code inside the thread, it throws an exception:
Caused by: java.lang.IllegalStateException: Asynchronous entity world add!
Here's a part of my code:
getServer().getScheduler().runTaskAsynchronously(this, new Runnable()
{
#Override
public void run()
{
String w_name = world.getName();
getServer().unloadWorld(world.getName(), false);
world = getServer().createWorld(new WorldCreator(w_name));
}
});
Any suggestions how to deal with this problem?
Bukkit doesn't like it when you try to edit anything through the API in an async task. Reading and processing is fine but bukkit enforces nothing when it comes to thread safety and thus affecting the world with more than 1 thread can cause issues.
Try splitting up your arena reset into smaller chunks and spreading out the operation over several ticks with a series of synchronous tasks, might help with the performance.
This isn't my code but it does a decent job of demonstrating the idea https://gist.github.com/aadnk/5443172
-- You can load worlds async using this: http://pastebin.com/K9CuVMS5 --
No you cannot, and if you tried it would have a high chance of world corruption. But if you don't care about it this is what you can do:
Bukkit loads worlds via Bukkit.createWorld(WorldCreator) which activates Server.createWorld(WorldCreator) which activates:
Validate.notNull(creator, "Creator may not be null");
String name = creator.name();
ChunkGenerator generator = creator.generator();
File folder = new File(this.getWorldContainer(), name);
World world = this.getWorld(name);
WorldType type = WorldType.getType(creator.type().getName());
boolean generateStructures = creator.generateStructures();
if(world != null) {
return world;
} else if(folder.exists() && !folder.isDirectory()) {
throw new IllegalArgumentException("File exists with the name \'" + name + "\' and isn\'t a folder");
} else {
if(generator == null) {
generator = this.getGenerator(name);
}
WorldLoaderServer converter = new WorldLoaderServer(this.getWorldContainer());
if(converter.isConvertable(name)) {
this.getLogger().info("Converting world \'" + name + "\'");
converter.convert(name, new ConvertProgressUpdater(this.console));
}
int dimension = 10 + this.console.worlds.size();
boolean used = false;
do {
Iterator sdm = this.console.worlds.iterator();
while(sdm.hasNext()) {
WorldServer hardcore = (WorldServer)sdm.next();
used = hardcore.dimension == dimension;
if(used) {
++dimension;
break;
}
}
} while(used);
boolean var25 = false;
ServerNBTManager var24 = new ServerNBTManager(this.getWorldContainer(), name, true);
WorldData worlddata = var24.getWorldData();
if(worlddata == null) {
WorldSettings internal = new WorldSettings(creator.seed(), EnumGamemode.getById(this.getDefaultGameMode().getValue()), generateStructures, var25, type);
internal.setGeneratorSettings(creator.generatorSettings());
worlddata = new WorldData(internal, name);
}
worlddata.checkName(name);
WorldServer var26 = (WorldServer)(new WorldServer(this.console, var24, worlddata, dimension, this.console.methodProfiler, creator.environment(), generator)).b();
if(!this.worlds.containsKey(name.toLowerCase())) {
return null;
} else {
var26.scoreboard = this.getScoreboardManager().getMainScoreboard().getHandle();
var26.tracker = new EntityTracker(var26);
var26.addIWorldAccess(new WorldManager(this.console, var26));
var26.worldData.setDifficulty(EnumDifficulty.EASY);
var26.setSpawnFlags(true, true);
this.console.worlds.add(var26);
if(generator != null) {
var26.getWorld().getPopulators().addAll(generator.getDefaultPopulators(var26.getWorld()));
}
this.pluginManager.callEvent(new WorldInitEvent(var26.getWorld()));
System.out.print("Preparing start region for level " + (this.console.worlds.size() - 1) + " (Seed: " + var26.getSeed() + ")");
if(var26.getWorld().getKeepSpawnInMemory()) {
short short1 = 196;
long i = System.currentTimeMillis();
for(int j = -short1; j <= short1; j += 16) {
for(int k = -short1; k <= short1; k += 16) {
long l = System.currentTimeMillis();
if(l < i) {
i = l;
}
if(l > i + 1000L) {
int chunkcoordinates = (short1 * 2 + 1) * (short1 * 2 + 1);
int j1 = (j + short1) * (short1 * 2 + 1) + k + 1;
System.out.println("Preparing spawn area for " + name + ", " + j1 * 100 / chunkcoordinates + "%");
i = l;
}
BlockPosition var27 = var26.getSpawn();
var26.chunkProviderServer.getChunkAt(var27.getX() + j >> 4, var27.getZ() + k >> 4);
}
}
}
this.pluginManager.callEvent(new WorldLoadEvent(var26.getWorld()));
return var26.getWorld();
}
}
Now by creating you own world loader, you can make it so it only generates a chunk every tick or so.

Categories

Resources