“Not only horribly tragic,” his defense attorney summed up in court , “but horribly stupid.”

Minutes later, a Montgomery County judge sentenced Erick Ramirez to 20 years in prison for fatally shooting Jonathan Lopez from Ramirez’s front porch in Silver Spring seven months ago.

The hearing Wednesday marked the end of a case that began with Ramirez and Lopez — both 24 — angrily texting each other as a woman they both knew was picking up her 4-year-old child at Ramirez’s home.

A shot came next.

Erick Ramirez (Montgomery County Police)

Nine days after that exchange, detectives received a bit of luck when a woman walking her dog spotted Ramirez’s weapon and plucked it with a plastic bag.

Ramirez offered up a bold explanation about the shooting: He said he was only trying to fire a warning shot but was a poor marksman.

“This did not have to occur,” prosecutor Ryan Wechsler said. “This has ruined more than one life.”

Ramirez originally was charged with first-degree murder, punishable by up to life in prison. In court, he pleaded guilty in a plea deal to three charges: involuntary manslaughter, use of a firearm in a felony and reckless endangerment.

The taunts and texts that led up to the fatal shooting occurred on a Monday night — March 16 — when Lopez, his girlfriend and a male friend drove to Ramirez’s home on Piney Branch Road, about one mile east of downtown Silver Spring.

The girlfriend was there to pick up a 4-year-old child she and Ramirez had together. While she was in Ramirez’s house, Lopez and Ramirez exchanged text messages.

The messaging continued after the girlfriend, the child, Lopez and the friend of Lopez’s drove off.

“Yea come back,” Ramirez wrote.

“Aight,” Lopez responded, “we COMING.”

He then asked his girlfriend to stop the car — just two blocks from Ramirez’s home. Lopez and his male friend walked toward the house. Ramirez grabbed a gun and was waiting for them.

“He shot from his front porch,” Wechsler said Wednesday, “and the shot hit the victim in the wrist, went through his wrist, to his abdomen.”

Lopez ran a short distance before collapsing on a sidewalk. Police and paramedics were called. Lopez died at a hospital the next morning.

Ramirez, meanwhile, ditched the gun in bushes about six blocks away, made his way to a bus stop, went to a friend’s place and showed up for work at a dry cleaner. Montgomery detectives tracked him down and questioned him. Ramirez told them he had become scared “and he meant to fire a warning shot,” Wechsler said.

But he wouldn’t take the detectives to the bush where he had tossed the gun. Growing ever more concerned — particularly with so many children in the area — police spent days searching for the weapon with the help of dogs.

They also had a message distributed on a neighborhood e-mail group, warning residents that they hadn’t found the gun.

One of the residents who saw that message was a 60-year-old modern dance instructor who walks her 14-year-old dog Piper — a poodle-shih tzu mix — several times a day.

On the afternoon of March 25, she followed as Piper walked to an opening about a foot wide between a bush and parked car. Piper sniffed next to a white piece of paper. The dog’s owner looked down andsaw a metallic object. The woman thought it could have been a gun, a toy or a garden hose attachment.

In an interview, she acknowledged she knew little about guns and had never handled one. Because of the criminal case, she asked not to be named to protect her identity.

She recalled taking out her dog’s poop bag — an unsoiled, plastic sleeve from a newspaper — and used it as a glove to pick up the object. It was, clearly, a gun.

She took it home, called the police and was alarmed when a responding officer removed bullets.

“We’re lucky it was found the way it was,” Wechsler said in court, adding that Ramirez’s reckless endangerment charge didn’t involve the shooting but his leaving a gun in a residential neighborhood.

Lopez’s mother tried to speak in court but could get out only a few words.

“No one is going to return my son back to me,” she said.

Ramirez did not speak Wednesday.

Ramirez’s attorney, Andrew Jezic, said his client had held a series of jobs — on the cleaning crew at a medical center, at a bakery and at the dry cleaner. Jezic said his client never intended to hurt anyone.

If he had been a better shot, Jezic said, the warning round would have not gone near Lopez, and Ramirez wouldn’t have faced such significant charges.

“Erick was legitimately scared,” Jezic said. “He is unfortunately here because he is a horrible shot.”