The NYPD said a man described by officials and witnesses as having threatened several people with a handgun in a Brooklyn public housing complex Sunday morning was shot to death by two police officers after engaging in a gunfight with the pair.

The gunman was identified as 42-year-old Jermaine Johnson of Park Ave. in Clinton Hill, Brooklyn.

The shooting took place in a courtyard of the Cooper Park Houses between 389 Morgan Ave. and 20 Debevoise Ave., police said.

Officers fired 12 to 15 shots at Johnson, hitting him twice, at least once in the torso, police said.

Johnson was taken to Woodhull Medical Center, where he was pronounced dead, police said.

Two 911 calls came in before the shooting, one reporting a robbery on Morgan Avenue and a second about a man with a gun around the corner, police said.

When the two responding officers arrived on the scene, they entered the courtyard and saw a man with a gun fitting the description they’d been given, police said.

It was not clear what led to the shooting, but Assistant Chief Jeffrey Maddrey, commanding officer of the NYPD’s Brooklyn North Precinct, said Johnson fired at least five gunshots at the officers, and a .40 caliber handgun was recovered.

The two officers were taken to Elmhurst Hospital Center for observation.

“He was involved in two robberies prior to encountering our police officers,” he said. “We do believe that he brandished his weapon both times and that he did have his firearm pointed at civilians.”

Several residents who either witnessed or heard gunfire said that before officers shot him, Johnson had knocked on several doors and pistol-whipped a man in the complex.

Shakeya McLaughlin, 31, said she was sleeping when the sound of a gunshot woke her.

“I heard it in my sleep . . . ,” she said. “I heard ‘get down, get down.’ ”

She said she then looked out her living room window and saw the officers.

“I just saw cops behind a tree with their guns out,” McLaughlin said. “I didn’t know what was going on. . . . The guy was armed.”

Inside her fourth-floor unit, Patricia Adams, 63, said she heard a commotion outside.

Adams said she looked outside and saw Johnson come up to a man she recognized and then “knocked him to the ground. He said, ‘I’ll kill you, I’ll kill you. Take me to your apartment.’ ”

He forced the man into a building and was screaming very loudly, Adams said.

“He truly looked like he was high on something,” Adams said.

Moments later, she said, the two came back outside and Johnson “kicked the guy again, knocked him to the ground.”

They were walking toward a parking lot before the encounter with the police officers, Adams said.

“He just pointed the gun,” she said of Johnson. “Then it was like bang, bang, bang, bang.”

Resident Celestine Payne, 68, said she was surprised by the violent morning.

“The police, they patrol,” she added. “They are welcome.”