VENTURA, Calif. — A teen who fatally shot a gay classmate in the back of the head during a computer lab nearly four years ago was sentenced Monday to 21 years in state prison, capping an emotional case that focused attention on how schools deal with sexual identity.

Brandon McInerney, 17, didn’t speak at the hearing, but his lawyer said his client was sorry for killing Larry King, 15.

“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,” Scott Wippert said.

McInerney will report to prison next month, after he turns 18. He pleaded guilty to second-degree murder, voluntary manslaughter and unlawful use of a firearm after jurors deadlocked during his trial as an adult on a first-degree murder charge.

Several jurors said afterward that they didn’t think McInerney should have been tried as an adult.

McInerney had just turned 14 when he shot King in front of shocked classmates at E.O. Green Junior High School in Oxnard on Feb. 12, 2008. Prosecutors alleged it was a hate crime driven by homophobic rage because King wore girls’ clothing and flirted with McInerney.

The killing became a flashpoint for gay rights groups that said it was further evidence that children often pay a high price when they come out. Comic Ellen DeGeneres, a lesbian, weighed in on her talk show and said gays shouldn’t be treated as second-class citizens.

Due to pretrial publicity, the trial was moved from Ventura County to Los Angeles County.

King’s father, Greg King, read a letter lambasting everyone from jurors, who he called incompetent, to the media, for not focusing enough on how school leaders “bungled” the handling of his son’s situation.

School administrators were criticized for not doing enough in the weeks leading up to the killing to quell a simmering feud between the two boys and for allowing King to wear heels and makeup to school.

Instead of protecting him from his “poor impulse control,” King’s father said, “they enabled and encouraged him to become more and more provocative,” putting him in an unsafe position.

School district officials said they were upholding federal law by protecting Larry King’s right to express his sexual orientation.

Greg King saved his strongest statements for McInerney, who he said his family couldn’t forgive.

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

Prosecutors said the plotted killing was first-degree murder and that McInerney should be punished as an adult.

Defense attorneys, who unsuccessfully argued to keep the case in juvenile court, said McInerney reached an emotional breaking point after King’s advances.