\documentclass[12pt]{article}% Donald Arseneau
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{mathpazo}
\usepackage{xcolor}
\usepackage{verbatim}

\newlength\sidebar
\newlength\envrule
\newlength\envborder
\setlength\sidebar{1.5mm}
\setlength\envrule{0.4pt}
\setlength\envborder{2.5mm}

\definecolor{exampleborder}{rgb}{0,0,.7}
\definecolor{examplebg}{rgb}{.9,.9,1}
\definecolor{statementborder}{rgb}{.9,0,0}
\definecolor{statementbg}{rgb}{1,.9,.9}
\newsavebox\envbox
\newcounter{example}
\newenvironment{example}[1][EXAMPLE]{%
  \par 
  \refstepcounter{example}%
  \SpecialEnv{#1}{exampleborder}{examplebg}{}{\theexample}%
}{%
  \endSpecialEnv
}
\newenvironment{statement}[1][]{% Default statement has no title
  \par
  \SpecialEnv{#1}{statementborder}{statementbg}{statementborder}{}%
}{%
  \endSpecialEnv
}

\def\Empty{}

% #1 title (if any)
% #2 sidebar (and title bg) color
% #3 background color
% #4 border color (or null for no border)
% #5 Counter, if any.
\newenvironment{SpecialEnv}[5]{%
  \par
  \def\EnvSideC{#2}% To use later (in end)
  \def\EnvBackgroundC{#3}%
  \def\EnvFrameC{#4}%  
  \flushleft
  \setlength\leftskip{-\sidebar}%
  \addtolength\leftskip{-\envborder}%
  \noindent \nobreak
  % Check if title is null:
  \ifx\delimiter#1\delimiter\else
  % If a title is specified, then typeset it in reverse color
   \colorbox{\EnvSideC}{%
     \hspace{-\leftskip}% usually positive
     \hspace{-\fboxsep}%
     \footnotesize\sffamily\bfseries\textcolor{white}{#1}%
     \hspace{\envborder}}%
   \par\nobreak
   \setlength\parskip{-0.2pt}% Tiny overlap to counter pixel round-off errors
   \nointerlineskip 
  \fi
  % Make side-bar
  \textcolor{\EnvSideC}{\vrule width\sidebar}%
  % collect body in \envbox:
  \begin{lrbox}\envbox 
  \begin{minipage}{\hsize}%
  % insert counter, if any:
  \ifx\delimiter#5\delimiter\else#5.\enspace\fi
  \ignorespaces
}{\par
  \end{minipage}\end{lrbox}%
  % body is collected. Add background color
  \setlength\fboxsep\envborder
  \ifx\EnvFrameC\Empty % no frame
    \colorbox{\EnvBackgroundC}{\usebox\envbox}%
  \else % frame
    \setlength\fboxrule\envrule
    \addtolength\fboxsep{-\envrule}%
    \fcolorbox{\EnvFrameC}{\EnvBackgroundC}{\usebox\envbox}%
  \fi
  \nobreak \hspace{-2\envborder}\null
  \endflushleft
}


\begin{document}

The distance formula gives us a method for working out the length of the
straight line between any two points. It is based on Pythagoras's Theorem.
\begin{example}
Calculate the distance between the points $(-7, -3)$ and $(16,-3)$.
\end{example}
Some text goes here.
\begin{statement}
Lines are parallel $\iff$ they have the same gradient.
\end{statement}
Discuss
\begin{example}[EXAMPLES]
A is the point ($-2,4$) and B ($3,1$). 
Calculate the length of the line AB.
\end{example}
The answer here.
\begin{example}[] % Null means no title -- continued!
Calculate the distance between the points $(\frac12,-\frac{15}{4})$ 
and $(-1,-1)$.\label{foo}
\end{example}
Solution to example~\ref{foo} here.

\bigskip\small
\verbatiminput{\jobname}
\end{document}