1

I'm a cobol structured programmer making the leap to Java OOP. I've written an ambitious card program where a user plays against 3 computer opponents. It's working but there is no delay between the 3 computer opponents and the user. I'd like it to seem like each opponent is playing a card individually. The user plays and each opponent updates two ImageViews using a setImageDrawable. The screen does not update until it reaches OnClickListener for the user. I tried Thread.sleep after each computer play which should leave the screen updatable, but while there is a delay the screen doesn't refresh until OnClickListener. I don't have a LayoutInflater because, well there's nothing to inflate - all 40 of my ImageViews are crammed into the screen. I've tried a few emulators in Android Studio but they all have this affect. I've also tried invalidate on the fullscreen without success -- perhaps I should on the individual ImageViews?

I'd post some of the code but it's rather lengthy and sloppy and I hope I've described the problem well enough. Any ideas how to fix or can you suggest some features in Java I can explore? My internet is a bit spotty so there may be a delayed response or thank you to your questions or answers. ............................

Each computer opponent passes an Arraylist to a common method which selects a playing card and then changes the ImageView to show the playing card.

            Resources res = getResources();
        int resId = res.getIdentifier(cardSelect, "drawable", getPackageName());  // location of image
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), res.getIdentifier(cardSelect, "drawable", getPackageName()), null);
        ImageView insertImage = (ImageView) findViewById(R.id.p1CardView);
        insertImage.setImageDrawable(drawable);

It uses the same type of coding to update another image on the screen. After the third computer opponent plays, the user's playable cards are marked as clickable and then the program waits for OnClick. At this point the image changes from the computer playing sudden appear with the user marked card instead of when the setImageDrawable is done above. Hope that's helpful!

Thanks

MJK
  • 11
  • 2
  • Without some code it is difficult to tell what is going on. But are you running the swing updates on the `swing update thread`. See https://docs.oracle.com/javase/tutorial/uiswing/concurrency/ With swing, you have to run things in the correct thread. – Bruce Martin Sep 24 '16 at 23:42
  • No separate threads designated in the program. The Thread.sleep is just something that I copied from the web which does indeed send the program to sleep. Should I look into dividing my program into different threads? Thanks! – MJK Sep 25 '16 at 00:03
  • Can you write an overview of your code. One thing I would emphasize is with Swing it will run through your code then sit there doing nothing until an event like `OnClickListener` is triggered. If you have Mainframe CICS/IMS Dc background think of Swing as a bit like CICS/IMS DC you send a screen, your program dies. then when the user hits enter/PF key the appropriate program is run. – Bruce Martin Sep 25 '16 at 02:30
  • In this answer http://stackoverflow.com/questions/9172174/what-are-the-advantages-of-pseudo-conversational-vs-conversational-cics-programm/9217206#9217206 I tried to explain CICS to non mainframes not sure if it will help. – Bruce Martin Sep 25 '16 at 02:36
  • Thanks Bruce! Each Computer opponent passes an ArrayList of their playing cards to a method that determines a card to play then displays it on their discard ImageView with – MJK Sep 25 '16 at 03:15
  • Edited OP with some coding – MJK Sep 25 '16 at 04:21
  • I have not worked with images in java but I suspect you are missing a repaint, reValidate (container) etc. `insertImage` has a refreshImage method which worth a try. reValidate is more drastic than repaint. – Bruce Martin Sep 25 '16 at 23:16
  • Thanks Bruce. I'll investigate those! – MJK Sep 28 '16 at 07:43

0 Answers0