VENTURA, California — A California teenager was sentenced on Monday to 21 years in state prison for killing a gay student during a computer lab class, capping an emotional and tumultuous case that drew widespread attention and raised questions about how schools should deal with sexual identity issues.

Superior Court Judge Charles Campbell sentenced Brandon McInerney, 17, based on a plea agreement reached with prosecutors that will send him to prison starting next month after he becomes an adult. McInerney, dressed in a white T-shirt and blue pants, didn't speak at the hearing, but his lawyer Scott Wippert said his clients was sorry for killing 15-year-old Larry King.

"He feels deeply remorseful and stated repeatedly if he could go back and take back what he did he would do it in a heartbeat, Wippert said.

McInerney pleaded guilty to second-degree murder, as well as one count of voluntary manslaughter and use of a firearm that spared him a retrial. A mistrial was declared in September when jurors couldn't reach a unanimous decision on the degree of guilt. Several jurors said after McInerney's trial that he shouldn't have been tried as an adult.

Prosecutors had argued that the shooting in front of stunned classmates was first-degree murder and that McInerney should be punished as an adult.

Unlike other incidents of teen violence, the McInerney case had an unusual twist: prosecutors contended the teen, who had just turned 14, shot King at E.O. Green Junior High School in a fit of homophobic rage because he was offended by the victim's feminine clothing and his unwanted sexual advances.

Comic Ellen DeGeneres, a lesbian, weighed in on her talk show shortly after the shooting and said gays shouldn't be treated as second-class citizens. Because of pretrial publicity, the trial was moved from Ventura County to Los Angeles.

Defense attorneys, who unsuccessfully argued to keep the case in juvenile court, said the killing was voluntary manslaughter because McInerney had reached an emotional breaking point after King made repeated, unwanted sexual advances toward him and other boys. They said he snapped when he heard that King wanted to change his first name to Latisha, and that he was beaten by his father.

School administrators were criticized for not doing enough in the weeks leading up to the killing at the Oxnard school to quell a simmering feud between the two teens and for allowing King to wear heels and makeup. School district officials have cited federal law providing the right of students to express their sexual orientation.

King's father, Greg King, also blamed the school district for not doing more to tone down their son's flamboyant behavior, adding that complaints were made to school officials before the shooting. He added that his family couldn't forgive McInerney.

"You took upon yourself to be a bully and to hate a smaller kid, wanting to be the big man on campus,'" King's father, Greg King, said to McInerney during the sentencing on behalf of his wife. "'You have left a big hole in my heart where Larry was and it can never be filled.'"

King's family and Deputy District Attorney Maeve Fox wore buttons with the teen's face on it, while some of McInerney's supporters wore powder blue wristbands that read "Save Brandon." Some teachers and jurors also attended the hearing.

During the trial, prosecutors portrayed McInerney as a teen who couldn't control his anger and was influenced by white supremacy ideology. Jurors rejected their claim that the killing was a hate crime.

After serving nearly four years since King's slaying, with the additional 21 years McInerney will be released just before his 39th birthday.