FARMINGTON — Ryan J. Ouimet was tossing his handgun back and forth in his hands when he hit the trigger and fatally shot his friend from Cape Elizabeth, according to court documents.

Ouimet, 24, of Colts Neck, N.J., told police that he “accidentally hit the trigger and the gun went off striking Andrew Holland in the chest,” according to a Maine State Police detective’s affidavit, released Wednesday.

Holland, 23, died from the gunshot wound after being taken by ambulance to Franklin Memorial Hospital in Farmington.

Ouimet has been charged with manslaughter. He made his first court appearance Wednesday in Franklin Superior Court before Justice Michaela Murphy.

As Ouimet left the courthouse with his parents, he was asked if he had any message for Holland’s family.

“I love my friend Andy, and I can think of nothing else but them right now,” he said.

The shooting happened on May 31 in an apartment just blocks from University of Maine at Farmington, where the friends graduated with bachelor’s degrees in May.

Inside the courtroom, Holland’s family members sat silently, holding hands in a front-row bench directly behind Ouimet.

Holland’s mother, Lynne Holland, said the family may want to comment later, then left the courthouse and got into a car.

Ouimet was released from jail after the shooting on $10,000 bail. Justice Murphy affirmed the bail conditions, which prohibit Ouimet from leaving Maine.

Murphy denied a request by Ouimet’s attorney to allow him to leave Maine, unless an agreement can be reached for his supervision in New Jersey.

He faces as much as 30 years in prison on the manslaughter charge, said Murphy, who set Ouimet’s next court date for Aug. 19.

Another UMaine-Farmington graduate, Caroline Halloran, 23, of Acton, Mass., was in the apartment when Holland was shot, at about 10 p.m. on May 31.

According to the affidavit, Halloran told police that just before the shooting, “Andrew Holland texted her to say he was coming over to hang out with her and Ryan (Ouimet). Andrew Holland showed up and they smoked marijuana and listened to music for a half hour or so. Ryan (Ouimet) then removed the gun from a drawer and held it.”

Halloran, who rents the apartment at 117 High St., told police that Ouimet was putting the cartridge into the gun and popping it out repeatedly, and Holland was doing the same, according to the affidavit.

Halloran told police that Ouimet was tossing the gun between his hands when it fired, according to the affidavit.

Ouimet told police that he was holding the gun and dancing to music just before the shooting. He went to change the song, turned, and the gun went off and hit Holland in the chest, according to the affidavit.

In a recorded a 911 call, a man said he was handling the .380-caliber pistol when it went off and shot his friend in the apartment, according to the affidavit. The caller said he hadn’t known that it was loaded.

Halloran told police that she called 911 with Ouimet after the shooting.

Justice Murphy denied the request by defense attorney Michael Cunniff to let Ouimet return to New Jersey between court appearances. She said bail conditions would be “utterly unenforceable” at Ouimet’s parents’ home.

Ouimet’s is prohibited from possessing or using alcohol, drugs or weapons, among other conditions. Murphy said Ouimet has no criminal record.

The manslaughter charge will go before the next grand jury in Franklin Superior Court, which is a requirement for an indictment, according to Murphy, who said she wants to push for a trial in October.

Cunniff said in a phone interview Tuesday that Ouimet plans to plead not guilty. “I think the circumstances that are known are that it was an unintentional death and the result of a tragic accident,” Cunniff said.