dropdown error on device in codenameone - codenameone

I am implementing drop-drown feature in my app. Its implemented using container with list of elements in it.
Suppose the drop down list has following items aa1, aa2, aa3, aa4 aa5 and so on. And if i search as 'aa' it displays items starting from 'aa', if I select aa5 from list, it takes aa1 and displays that. But whereas if I scroll the items and select its working fine. This problem occurring only on iOS device working perfectly fine on simulator.
the first picture depicts how drop down looks like, in second picture if I search 'ee', it gives list of items starting with 'ee'. If I select 'ee5', it sets to ee1 as shown in picture 3. Problem only on device. Any workaround for this?
So, please let me know whats the issue with this.
Thanks
[![enter image description here][1]][1]
private CustomList itemList;
class CustomList extends List {
int startYPos = -1;
long lastDiff = 0;
Timer t = null;
int draggingState = 0;
public CustomList() {
this.setTensileDragEnabled(false);
}
}
private class ButtonListener implements ActionListener {
public void actionPerformed(final ActionEvent evt) {
final Runnable rn = new Runnable() {
public void run() {
// Create and show a dialog to allow users to make a selection.
final UiBuilder uib = dm.UiBuilder();
dialog = (Dialog) uib.createContainer(DESIGNER_NAME_DIALOG_COMBOBOX_CONTAINER);
GenericSpinner itemSpinner = (GenericSpinner) uib.findByName(DESIGNER_NAME_DIALOG_COMBOBOX_GENERIC_SPINNER, dialog);
itemSpinner.setPreferredW(Display.getInstance().getDisplayWidth() * 4 / 5);
//remove from parent and replace with a linear list
Container parent = itemSpinner.getParent();
parent.removeComponent(itemSpinner);
// Add the searchable text field box
final TextField tf = (TextField)uib.findByName("Search", dialog);
tf.addDataChangedListener(new DataChangedListener() {
#Override
public void dataChanged(int type, int index) {
Object[] items = model.getFilteredItems(tf.getText());
itemList.setModel(new DefaultListModel(items));
}
});
itemList = new CustomList();
itemList.getAllStyles().setBgTransparency(0);
itemList.setItemGap(0);
parent.addComponent(BorderLayout.CENTER, itemList);
final String[] items = model.getItems();
itemList.setModel(new DefaultListModel(items));
itemList.getStyle().setMargin(10, 10, 10, 10);
itemList.setFireOnClick(true);
itemList.setLongPointerPressActionEnabled(false);
ActionListener list = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (model.isUserEditable() && model.getItemCount() > 0) {
int i = itemList.getSelectedIndex();
if (i > items.length - 1) {
return;
}
itemList.getModel().setSelectedIndex(i);
model.onUserDataEntered((String) itemList.getModel().getItemAt(i));
String textToDisplay = (String) itemList.getModel().getItemAt(i);
button.setText(textToDisplay);
}
dialog.dispose();
}
};
itemList.addActionListener(list);
CommonTransitions tran = CommonTransitions.createEmpty();
dialog.setTransitionInAnimator(tran);
dialog.setTransitionOutAnimator(tran);
itemList.setRenderer(new ListRenderer());
//related to dialog to show list of items
//how much space do we really need???
if (cellHeight == 0) {
int dip = Display.getInstance().convertToPixels(1);
int siz = 2;
if (Display.getInstance().isTablet()) {
siz = 4;
}
siz *= 2;
cellHeight = siz * dip;
}
int heightRequired = cellHeight * (items.length + 8);
//is this too much for the screen - we will use 3/4 of the screen height max
int availableHeight = Display.getInstance().getDisplayHeight() * 3;
availableHeight /= 4;
if (heightRequired > availableHeight) {
int topPos = Display.getInstance().getDisplayHeight() / 8;
int bottomPos = topPos + availableHeight;
dialog.show(topPos, topPos, 40, 40);
}
else {
int topPos = (Display.getInstance().getDisplayHeight() - heightRequired) / 2;
int bottomPos = topPos + heightRequired;
dialog.show(topPos, topPos, 40, 40);
}
}
};
}
}
//new code using Multibutton implementation
final String[] listItems = model.getItems();
Display.getInstance().callSerially(() ->{
multiButton= new MultiButton();
multiButton.setTextLine1(s);
dialog.add(multiButton);
multiButton.addActionListener(e -> Log.p("you picked " + multiButton.getSelectCommandText(), Log.ERROR));
}
dialog.revalidate();
});

I would recommend using a Container and simple layout search as demonstrated by code such as this. The code below was taken from the Toolbar javadoc:
Image duke = null;
try {
duke = Image.createImage("/duke.png");
} catch(IOException err) {
Log.e(err);
}
int fiveMM = Display.getInstance().convertToPixels(5);
final Image finalDuke = duke.scaledWidth(fiveMM);
Toolbar.setGlobalToolbar(true);
Form hi = new Form("Search", BoxLayout.y());
hi.add(new InfiniteProgress());
Display.getInstance().scheduleBackgroundTask(()-> {
// this will take a while...
Contact[] cnts = Display.getInstance().getAllContacts(true, true, true, true, false, false);
Display.getInstance().callSerially(() -> {
hi.removeAll();
for(Contact c : cnts) {
MultiButton m = new MultiButton();
m.setTextLine1(c.getDisplayName());
m.setTextLine2(c.getPrimaryPhoneNumber());
Image pic = c.getPhoto();
if(pic != null) {
m.setIcon(fill(pic, finalDuke.getWidth(), finalDuke.getHeight()));
} else {
m.setIcon(finalDuke);
}
hi.add(m);
}
hi.revalidate();
});
});
hi.getToolbar().addSearchCommand(e -> {
String text = (String)e.getSource();
if(text == null || text.length() == 0) {
// clear search
for(Component cmp : hi.getContentPane()) {
cmp.setHidden(false);
cmp.setVisible(true);
}
hi.getContentPane().animateLayout(150);
} else {
text = text.toLowerCase();
for(Component cmp : hi.getContentPane()) {
MultiButton mb = (MultiButton)cmp;
String line1 = mb.getTextLine1();
String line2 = mb.getTextLine2();
boolean show = line1 != null && line1.toLowerCase().indexOf(text) > -1 ||
line2 != null && line2.toLowerCase().indexOf(text) > -1;
mb.setHidden(!show);
mb.setVisible(show);
}
hi.getContentPane().animateLayout(150);
}
}, 4);
hi.show();

Related

Implementing safe areas with Infinite Container pull to refresh

How can I set the safe area for pull to refresh? I have a form with with an infinite container. I set the IC with safe area ic.setSafeArea(true). When I rotate the screen the safe area is respected(see image 1). If i pull to refresh the safe area is not respected (see image 2). With the below code taken from the InfiniteContainerSample, how can I ensure the safe area is respected after pull to refresh?
image 1
public void showForm() {
Form hi = new Form("InfiniteContainer", new BorderLayout());
Style s = UIManager.getInstance().getComponentStyle("MultiLine1");
FontImage p = FontImage.createMaterial(FontImage.MATERIAL_PORTRAIT, s);
EncodedImage placeholder = EncodedImage.createFromImage(p.scaled(p.getWidth() * 3, p.getHeight() * 3), false);
InfiniteContainer ic = new InfiniteContainer() {
#Override
public Component[] fetchComponents(int index, int amount) {
java.util.List<Map<String, Object>> data = fetchPropertyData("Leeds");
MultiButton[] cmps = new MultiButton[data.size()];
for(int iter = 0 ; iter < cmps.length ; iter++) {
Map<String, Object> currentListing = data.get(iter);
if(currentListing == null) {
return null;
}
String thumb_url = (String)currentListing.get("thumb_url");
String guid = (String)currentListing.get("guid");
String summary = (String)currentListing.get("summary");
cmps[iter] = new MultiButton(summary);
cmps[iter].setIcon(URLImage.createToStorage(placeholder, guid, thumb_url));
}
return cmps;
}
};
ic.setUIID("Blue");
ic.setSafeArea(true);
ic.addComponent(new Label("This is a test"));
hi.add(BorderLayout.CENTER, ic);
hi.show();
}
int pageNumber = 1;
java.util.List<Map<String, Object>> fetchPropertyData(String text) {
try {
ConnectionRequest r = new ConnectionRequest();
r.setPost(false);
r.setUrl("http://api.nestoria.co.uk/api");
r.addArgument("pretty", "0");
r.addArgument("action", "search_listings");
r.addArgument("encoding", "json");
r.addArgument("listing_type", "buy");
r.addArgument("page", "" + pageNumber);
pageNumber++;
r.addArgument("country", "uk");
r.addArgument("place_name", text);
NetworkManager.getInstance().addToQueueAndWait(r);
Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(r.getResponseData()), "UTF-8"));
Map<String, Object> response = (Map<String, Object>)result.get("response");
return (java.util.List<Map<String, Object>>)response.get("listings");
} catch(Exception err) {
Log.e(err);
return null;
}
}
Thanks for reporting this. This has been fixed on github. It will be included in the next release on Friday.

Processing: webcam save and playback bug

I should be beyond this, but I can't get to the bottom of this error. I'm trying to write a sketch that records the feed of my Mac camera and stores each recording "session" into a PImage array, then adds that to a list of sessions (a PImage[] ArrayList). I'm using a 'Replay' class to access the images stored in memory to and replay them at a random position. The code below should be ready to be copied straight into the IDE.
Any help would be greatly appreciated. I have no idea why the replay objects are always displaying the live image. Thanks!
import processing.video.*;
Capture cam;
ArrayList<PImage[]> allImages;
ArrayList<PImage> currentImages;
ArrayList<Replay> replays;
boolean recording = false;
boolean finishedSaving = true;
int currentIndex = 0;
void setup() {
size(1056, 704, P2D);
frameRate(30);
allImages = new ArrayList<PImage[]>();
currentImages = new ArrayList<PImage>();
replays = new ArrayList<Replay>();
String[] cams = Capture.list();
if (cams.length == 0) {
println("No cams!");
exit();
} else {
cam = new Capture(this, 1056, 704, cams[0], 30);
cam.start();
}
}
void draw() {
background(0);
if (cam.available() == true) {
cam.read();
}
for (Replay r : replays) {
r.display();
}
if (recording) {
currentImages.add(cam);
noFill();
stroke(255, 0, 0);
strokeWeight(5);
rect(0, 0, cam.width/3, cam.height/3);
} else {
saveToArray();
}
image(cam, 0, 0, cam.width/3, cam.height/3);
}
void saveToArray() {
if (!finishedSaving) {
PImage[] tempImages = currentImages.toArray(new PImage[currentImages.size()]);
allImages.add(tempImages);
currentImages.clear();
println("Finished saving to allImages array");
println("allImages array size now = " + allImages.size());
replays.add(new Replay(currentIndex));
println("Added new Replay (index: " + currentIndex + ")");
currentIndex++;
finishedSaving = true;
println();
}
}
void keyPressed() {
if (key == 'r' || key == 'R') {
recording = !recording;
println("Recording: " + recording);
finishedSaving = false;
}
}
class Replay {
PVector position;
float w, h;
PImage[] images;
int count;
Replay(int allImagesIndex) {
w = cam.width/3;
h = cam.height/3;
position = new PVector(random(width-w), random(height-h));
count = 1;
images = allImages.get(allImagesIndex);
}
void display() {
image(images[count], position.x, position.y, w, h);
count++;
if (count > images.length-1) count = 1;
}
}
Looks like this was due to the camera feed always being assignment to each individual image. To get a "copy" of the "current" video stream frame, I just added .get() to the feed so it fetches the pixels and stores them in a PImage variable.
currentImages.add(cam.get());

How to build a swipable intro screen?

A lot of apps have swipable intro screens - You know - those with the dots below which indicate the page one is currently viewing.
What would be the best way to create one in Codename One - a Container with snapToGrid?
I have my own implementation for this use case. There are two classes : TutoDialog which could be in your case the "intro screens" dialog and Caroussel with the dots indicator.
A tuto dialog has a title and some images in parameter. It automatically adjust the number of dots of the caroussel according to the number of images. For my use case, each image is a screenshot of my app with some advise. The tuto dialog contains 3 buttons to navigate between images (next/previous/finish).
public class Caroussel extends Container {
private final static Image CIRCLE = MainClass.getResources().getImage("circle-blue20.png");
private final static Image CIRCLE_EMPTY = MainClass.getResources().getImage("circle-empty-blue20.png");
private Label[] circles;
private int currentIndex = -1;
public Caroussel(int nbItems, boolean selectFirst) {
if (nbItems < 2 || nbItems > 50) {
throw new IllegalArgumentException("Can't create Caroussel component with nbItems<2 || nbItems>50 ! ");
}
this.circles = new Label[nbItems];
setLayout(new BoxLayout(BoxLayout.X_AXIS));
for (int i = 0; i < nbItems; i++) {
circles[i] = new Label("", CIRCLE_EMPTY);
add(circles[i]);
}
if (selectFirst) {
select(0);
}
}
public void select(int index) {
if (index >= 0 && index <= circles.length) {
if (currentIndex > -1) {
circles[currentIndex].setIcon(CIRCLE_EMPTY);
}
circles[index].setIcon(CIRCLE);
currentIndex = index;
repaint();
}
}
public void selectNext() {
if (currentIndex <= circles.length) {
select(currentIndex + 1);
}
}
public void selectPrevious() {
if (currentIndex >= 1) {
select(currentIndex - 1);
}
}}
And
public class TutoDialog extends Dialog {
private Caroussel caroussel = null;
public TutoDialog(String title, Image... images) {
if (images == null) {
return;
}
this.caroussel = new Caroussel(images.length, true);
setTitle(title);
setAutoAdjustDialogSize(true);
getTitleComponent().setUIID("DialogTitle2");
setBlurBackgroundRadius(8.5f);
Tabs tabs = new Tabs();
tabs.setSwipeActivated(false);
tabs.setAnimateTabSelection(false);
int px1 = DisplayUtil.getScaledPixel(800), px2 = DisplayUtil.getScaledPixel(600);
for (Image img : images) {
tabs.addTab("", new Label("", img.scaled(px1, px2)));
}
Container cButtons = new Container(new BorderLayout());
Button bSuivant = new Button("button.suivant");
Button bPrecedent = new Button("button.precedent");
Button bTerminer = new Button("button.terminer");
bPrecedent.setVisible(false);
bTerminer.setVisible(false);
bSuivant.addActionListener(new ActionListener<ActionEvent>() {
public void actionPerformed(ActionEvent evt) {
int currentInd = tabs.getSelectedIndex();
if (currentInd == 0) {
bPrecedent.setVisible(true);
}
if (currentInd + 1 <= tabs.getTabCount() - 1) {
if (caroussel != null)
caroussel.selectNext();
tabs.setSelectedIndex(currentInd + 1);
if (currentInd + 1 == tabs.getTabCount() - 1) {
bTerminer.setVisible(true);
bSuivant.setVisible(false);
cButtons.revalidate();
}
}
};
});
bPrecedent.addActionListener(new ActionListener<ActionEvent>() {
public void actionPerformed(ActionEvent evt) {
int currentInd = tabs.getSelectedIndex();
tabs.setSelectedIndex(currentInd - 1);
bSuivant.setVisible(true);
if (caroussel != null)
caroussel.selectPrevious();
if (currentInd - 1 == 0) {
bPrecedent.setVisible(false);
cButtons.revalidate();
}
};
});
bTerminer.addActionListener(new ActionListener<ActionEvent>() {
#Override
public void actionPerformed(ActionEvent evt) {
tabs.setSelectedIndex(0);
bPrecedent.setVisible(false);
bTerminer.setVisible(false);
bSuivant.setVisible(true);
if (caroussel != null)
caroussel.select(0);
TutoDialog.this.dispose();
}
});
cButtons.add(BorderLayout.WEST, bPrecedent).add(BorderLayout.CENTER, bSuivant).add(BorderLayout.EAST, bTerminer);
add(BoxLayout.encloseY(tabs, BoxLayout.encloseY(FlowLayout.encloseCenter(caroussel), cButtons)));
}
public static void showIfFirstTime(AbstractComponentController ctrl) {
if (ctrl == null) {
Log.p("Can't execute method showIfFirstTime(...) with null AbstractComponentController");
return;
}
String key = getKey(ctrl);
if (ctrl.getTutoDlg() != null && !Preferences.get(key, false)) {
Display.getInstance().callSerially(new Runnable() {
#Override
public void run() {
Preferences.set(key, true);
ctrl.getTutoDlg().show();
}
});
}
}
public static String getKey(AbstractComponentController ctrl) {
String key = "tuto" + ctrl.getClass().getSimpleName();
if (UserController.getCurrentUser() != null) {
key += "-" + UserController.getCurrentUser().getId();
}
return key;
}
public static boolean isAlreadyShown(AbstractComponentController ctrl) {
return Preferences.get(getKey(ctrl), false);
}
}
It's look like this :
OK - so that is my first attempt and I am pretty content with that:
private void showIntro() {
Display display = Display.getInstance();
int percentage = 60;
int snapWidth = display.getDisplayWidth() * percentage / 100;
int snapHeight = display.getDisplayHeight() * percentage / 100;
Dialog dialog = new Dialog(new LayeredLayout()) {
#Override
protected Dimension calcPreferredSize() {
return new Dimension(snapWidth, snapHeight);
}
};
Tabs tabs = new Tabs();
tabs.setTensileLength(0);
tabs.hideTabs();
int[] colors = {
0xc00000,
0x00c000,
0x0000c0,
0x909000,
0x009090,
};
for (int colorIndex = 0; colorIndex < colors.length; colorIndex++) {
Container containerElement = new Container() {
#Override
protected Dimension calcPreferredSize() {
return new Dimension(snapWidth, snapHeight);
}
};
Style style = containerElement.getAllStyles();
style.setBgTransparency(0xff);
style.setBgColor(colors[colorIndex]);
tabs.addTab("tab" + tabs.getTabCount(), containerElement);
}
int tabCount = tabs.getTabCount();
Button[] buttons = new Button[tabCount];
Style styleButton = UIManager.getInstance().getComponentStyle("Button");
styleButton.setFgColor(0xffffff);
Image imageDot = FontImage.createMaterial(FontImage.MATERIAL_LENS, styleButton);
for (int tabIndex = 0; tabIndex < tabCount; tabIndex++) {
buttons[tabIndex] = new Button(imageDot);
buttons[tabIndex].setUIID("Container");
final int tabIndexFinal = tabIndex;
buttons[tabIndex].addActionListener(aActionEvent -> tabs.setSelectedIndex(tabIndexFinal, true));
}
Container containerButtons = FlowLayout.encloseCenter(buttons);
dialog.add(tabs);
Button buttonWest = new Button("Skip");
buttonWest.setUIID("Container");
buttonWest.getAllStyles().setFgColor(0xffffff);
buttonWest.addActionListener(aActionEvent -> dialog.dispose());
Button buttonEast = new Button(">");
buttonEast.setUIID("Container");
buttonEast.getAllStyles().setFgColor(0xffffff);
buttonEast.addActionListener(aActionEvent -> {
int selectedIndex = tabs.getSelectedIndex();
if (selectedIndex < (tabs.getTabCount() - 1)) {
tabs.setSelectedIndex(selectedIndex + 1, true);
} else {
dialog.dispose();
}
});
Container containerSouth = BorderLayout.south(BorderLayout.centerAbsoluteEastWest(containerButtons, buttonEast, buttonWest));
Style styleContainerSouth = containerSouth.getAllStyles();
styleContainerSouth.setMarginUnit(
Style.UNIT_TYPE_DIPS,
Style.UNIT_TYPE_DIPS,
Style.UNIT_TYPE_DIPS,
Style.UNIT_TYPE_DIPS);
styleContainerSouth.setMargin(2, 2, 2, 2);
dialog.add(containerSouth);
SelectionListener selectionListener = (aOldSelectionIndex, aNewSelectionIndex) -> {
for (int buttonIndex = 0; buttonIndex < buttons.length; buttonIndex++) {
if (buttonIndex == aNewSelectionIndex) {
buttons[buttonIndex].getAllStyles().setOpacity(0xff);
} else {
buttons[buttonIndex].getAllStyles().setOpacity(0xc0);
}
}
buttonEast.setText((aNewSelectionIndex < (tabs.getTabCount() - 1)) ? ">" : "Finish");
buttonEast.getParent().animateLayout(400);
};
tabs.addSelectionListener(selectionListener);
dialog.addShowListener(evt -> {
buttonEast.getParent().layoutContainer();
selectionListener.selectionChanged(-1, 0);
});
Command command = dialog.showPacked(BorderLayout.CENTER, true);
}

Image Viewer auto slide codenamone

How can i auto slide the imageViewer for instance in the interval of 3 seconds.
Or is there any other component that can do the auto swipe task.
ImageViewer imv = new ImageViewer();
DefaultListModel<Image> images = new DefaultListModel<Image>(new Image[]{a, thumbnail1, thumbnail2});
imv.setImage(images.getItemAt(0));
imv.setImageList(images);
imv.setSwipePlaceholder(Image.createImage(100, 100));
Declare this global static variable:
private static int slideIndex = 0;
//And this UITimer
UITimer t;
And then try out below code
final ImageViewer imv = new ImageViewer();
final DefaultListModel<Image> images = new DefaultListModel<Image>(new Image[]{a, thumbnail1, thumbnail2});
imv.setImage(images.getItemAt(0));
imv.setImageList(images);
imv.setSwipePlaceholder(Image.createImage(100, 100));
Runnable r = new Runnable() {
public void run() {
if (slideIndex < images.getSize()) {
slideIndex++;
} else {
slideIndex = 0;
}
Image nextImage = (Image) images.getItemAt(slideIndex);
if (nextImage != null) {
imv.setImage(nextImage);
}
}
};
if (t == null) {
t = new UITimer(r);
}
if (t != null) {
t.schedule(3000, true, f); //3 seconds
}

(Processing) Removing Previously Used Object in Array

Sorry for the horrible wording of the title. I'm creating a game of "War" in Processing for my Programming class. I need to change my code so that each card that is used is removed from the deck/array. I stumbled upon some posts and Google results mentioning "ArrayList", but I'm still sort of clueless.
The following code displays two separate, random cards and displays two new random cards when the mouse is clicked.
(First Tab 'War')
void draw(){
image(card[imageIndex],40,150);
image(card2[imageIndex2],340,150);
}
void mousePressed(){
imageIndex = int(random(card.length));
imageIndex2 = int(random(card2.length));
}
(Second Tab 'Card')
PImage[] card = new PImage[13];
PImage[] card2 = new PImage[13];
int imageIndex = int(random(0,12)),
imageIndex2 = int(random(0,12));
void setup(){
size(500,500);
card[0] = loadImage("2_of_clubs.jpg");
card[1] = loadImage("3_of_clubs.jpg");
card[2] = loadImage("4_of_clubs.jpg");
card[3] = loadImage("5_of_clubs.jpg");
card[4] = loadImage("6_of_clubs.jpg");
card[5] = loadImage("7_of_clubs.jpg");
card[6] = loadImage("8_of_clubs.jpg");
card[7] = loadImage("9_of_clubs.jpg");
card[8] = loadImage("10_of_clubs.jpg");
card[9] = loadImage("jack_of_clubs.jpg");
card[10] = loadImage("queen_of_clubs.jpg");
card[11] = loadImage("king_of_clubs.jpg");
card[12] = loadImage("ace_of_clubs.jpg");
card2[0] = loadImage("2_of_clubs.jpg");
card2[1] = loadImage("3_of_clubs.jpg");
card2[2] = loadImage("4_of_clubs.jpg");
card2[3] = loadImage("5_of_clubs.jpg");
card2[4] = loadImage("6_of_clubs.jpg");
card2[5] = loadImage("7_of_clubs.jpg");
card2[6] = loadImage("8_of_clubs.jpg");
card2[7] = loadImage("9_of_clubs.jpg");
card2[8] = loadImage("10_of_clubs.jpg");
card2[9] = loadImage("jack_of_clubs.jpg");
card2[10] = loadImage("queen_of_clubs.jpg");
card2[11] = loadImage("king_of_clubs.jpg");
card2[12] = loadImage("ace_of_clubs.jpg");
}
Using ArrayList would look more or less like:
// gonna use strings instead of images
// just to show the idea. I don't have all this images...
ArrayList<String> card = new ArrayList<String>();
ArrayList<String> card2;
int imageIndex, imageIndex2;
String display1, display2;
void setup() {
size(500, 500);
card.add("2_of_clubs.jpg");
card.add("3_of_clubs.jpg");
card.add("4_of_clubs.jpg");
card.add("5_of_clubs.jpg");
card.add("6_of_clubs.jpg");
card.add("7_of_clubs.jpg");
card.add("8_of_clubs.jpg");
card.add("9_of_clubs.jpg");
card.add("10_of_clubs.jpg");
card.add("jack_of_clubs.jpg");
card.add("queen_of_clubs.jpg");
card.add("king_of_clubs.jpg");
card.add("ace_of_clubs.jpg");
card2 = new ArrayList<String>(card);
imageIndex = int(random(card.size()));
imageIndex2 = int(random(card2.size()));
display1 = card.get(imageIndex);
display2 = card2.get(imageIndex2);
card.remove(imageIndex);
card2.remove(imageIndex2);
println("\ncard draw from card : "+ display1);
println("card draw from card2: "+ display2 + "\n");
}
void draw() {
}
void mousePressed() {
if (card.size() > 0) {
imageIndex = int(random(card.size()));
imageIndex2 = int(random(card2.size()));
display1 = card.get(imageIndex);
display2 = card2.get(imageIndex2);
card.remove(imageIndex);
card2.remove(imageIndex2);
println("\ncard draw from card : "+ display1);
println("card draw from card2: "+ display2 + "\n");
} else {
println("The deck is empty...");
}
}
if you have to use arrays then you would have to do something similar to the following code:
PImage[] removeCardFromDeck(PImage deck, int index){
PImage[] newdeck = new PImage[deck.length -1];
int count=0;
for(int i =0 ; i<newdeck.length;i++){
if(count==index) count++;
newdeck[i] = deck[count];
count++;
}
return newdeck;
}
but for this task it is better to use ArrayLists.

Resources