Can U.S. lawyers be ordered by a judge to defend someone?

Here’s the situation:
Successful law firm with a heavy case load is ordered by a judge to defend someone who obviously has no money.

Can a judge do this? Does it actually happen? How is the law firm compensated?

I’m not a lawyer, so take this response with a grain of salt.

What jurisdiction are we talking about? Most places have public defenders, whose only job is to represent criminal defendants who can’t afford to hire a private attorney. These public defenders are paid by the government, but generally not paid very well.

In some jurisdictions, like Texas (or parts of Texas?), there are no full-time public defenders. The defendant is still entitled to a lawyer and so a judge just appoints a member of the bar to represent him. Whether the lawyer gets paid for these cases and how much varies from one jurisdiction to another.