For generating addresses from extended public key for watch-only wallet, First of all you need to first create the watch-only wallet by that extended public key. and then you can simply derive the addresses from that one by one.
Sample code is shown below:
// Specify the Network Parameters for mainnet or testnet
NetworkParameters params = NetworkParameters.prodNet();
// Provide the public key from which you want to derive addresses
String xPub = "xpub6Cw8YA6Mko3xfkYpMQDZjGjgDTWUrJr87NBSiDPXqcmcSJTgxLXm3VCw3iQs4iC5ZrwpY3M21a43DZmiMzDXWzzhF1n7yxSXDnEHjJN6jwK";
// Create watching wallet, with the help of Wallet class
Wallet wallet = Wallet.fromWatchingKeyB58(params, xPub, DeterministicHierarchy.BIP32_STANDARDISATION_TIME_SECS);
// Print the very first derived address from provided public key
System.out.println("Receiving Address : " + wallet.currentReceiveAddress());
The Output will be :
You can get the public key from https://iancoleman.io/bip39/ and its all particular derived addresses. You can compare your first generated address from there, to make sure that you are getting correct addresses.
If you want to fetch first 20 or 30 Addresses then you can use given line in a loop. Provide loop limit upto 20 or 30 (which you want).
// Run this statement in a loop, where i is the loop variable.
System.out.println(i + " : " + wallet.freshReceiveAddress());
You can again compare these addresses from https://iancoleman.io/bip39/