Cavity-state preparation using adiabatic transfer