The analogies presented by Hamza islam are pretty good. But, I would like to present a couple of verses which I hope would help you understand, without pain.

For his sake there are angels following one another, before him and behind him, who guard him by Allah's commandment; surely Allah does not change the condition of a people until they change their own condition; and when Allah intends evil to a people, there is no averting it, and besides Him they have no protector. [Qur'an 13:11]

To fully understand the picture, you need to know the distinction between punishment and trials.

Here is an excellent article in IslamQA on the topic "Difference Between Punishments and Trials", which is somewhat related to this question.

If you believe the undeniable facts that good people go to paradise and bad go to hell, then, you must realize, although it is hazy, that the Creator loves you, and that is why he is causing you to suffer pain and grief, and he is happy, because he might later think of averting from throwing you in hell fire, because you already suffered for your mistakes.

The Prophet (peace be upon him) said: A Muslim male or female, remains subject to trials (in this world) in respect of self, children and property till he or she faces Allah, the Exalted, (on the Day of Judgment) in a state in which all his or her sins have been remitted. Al-Tirmidhi Hadith

And also note that, Allah doesn't cause a person bear more than what he can bear. So, be patient.

On no soul doth Allah Place a burden greater than it can bear. It gets every good that it earns, and it suffers every ill that it earns. (Pray:) "Our Lord! Condemn us not if we forget or fall into error; our Lord! Lay not on us a burden Like that which Thou didst lay on those before us; Our Lord! Lay not on us a burden greater than we have strength to bear. Blot out our sins, and grant us forgiveness. Have mercy on us. Thou art our Protector; Help us against those who stand against faith. [Qur'an 2:286]"

TL;DR If you did something wrong, you deserve punishment in this life, and that's for your welfare. If Allah loves you, he might try you. In both cases, you need to be patient.