Ramblings Of a Madman

Yeah, like i would know a good description…

Posts for Tag: software development

JAVA vertical gradient background panel

Recently i had to create a gradient background for one of the panels in our [POS application](http://inforviegas.pt/wp-content/uploads/2010/09/VOS.jpg), so i though i'd share the code.

You need to pass the top and bottom colors of the gradient and also make sure that any panels inside this one are transparent (i.e. setOpaque(false)), otherwise they will paint above it.

import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints;

import javax.swing.JPanel;

public class VerticalGradientBackgroundPanel extends JPanel {

private Color topColor; private Color bottomColor;

public VerticalGradientBackgroundPanel(Color topColor, Color bottomColor) {

this.topColor = topColor; this.bottomColor = bottomColor; }

@Override protected void paintComponent(Graphics grphcs) {

super.paintComponent(grphcs);

Graphics2D g2d = (Graphics2D) grphcs;

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); GradientPaint gp = new GradientPaint(0, 0, topColor, 0, getHeight(), bottomColor); g2d.setPaint(gp); g2d.fillRect(0, 0, getWidth(), getHeight()); } }

JAVA JScrollPane change scrollbar width or height

It's actually pretty simple. Just do something like this, adjusting to the horizontal scrollbar and/or the height that you want:

yourScrollPcane.getVerticalScrollBar().setPreferredSize(new Dimension(85, Integer.MAX_VALUE));

Date and time in ruby

Time.now will give you the current date (and time), like so:

Time.now 2012-11-22 22:35:01 +0000

You can also request all the date parts individually:

Time.now.hour 22 Time.now.min 35 Time.now.sec 01 Time.now.day 22 Time.now.month 11 Time.now.year 2012

Cool (and easy), right? If you want to change a date, you can add seconds to it:

t = Time.now 2012-11-22 22:35:01 +0000 t2 = t + 10 # 10 Seconds 2012-11-22 22:35:11 +0000 t3 = t + 10*60 # 10 minutes 2012-11-22 22:45:01 +0000 t4 = t + 10*60*60 # 10 hours 2012-11-23 08:35:01 +0000

If you happen to be developing a Ruby on Rails application then it gets even easier:

t = Time.now 2012-11-22 22:35:01 +0000 t2 = t + 1.hour 2012-11-22 23:35:01 +0000

That's right, in Rails numerals get some extra methods injected into them, like hour, hours, minute, minutes, second, seconds, day, days, and so on... And people still ask me why i love Ruby (and Rails)... :)

A collection of useful Ruby/Rails gems

Since lately i've been working a lot with Ruby (Android and Java as well, but that's a different story), i figured i posted a couple of interesting gems that will make your life easier, especially but not only for rails.

Slim

Slim is a templating engine (like ERB or HAML) which pursues simplicity. It helps you write an insane amount less HTML and Rails code. You will not believe your eyes. An example:

In HTML

<body> <h1>Markup examples</h1> <div id="content"> <p>This example shows you how a basic Slim file looks like.</p> <%= yield %> </div> </body>

The same in Slim

body   h1 Markup examples   #content     p This example shows you how a basic Slim file looks like.     == yield

Now tell me which one you prefer to write ;)

Stamp

A fantastic tool to format dates based on human readable formats. Why use Date.today.strftime("%Y/%m/%d") when you can use Date.today.stamp("2012/12/13")? Sweet, right?

Bugsnag

Bugsnag is web site exception grabbing on steroids. It basically monitors your website alerting you of any exceptions and providing the relevant information needed so you don't have to debug your production server.

Cool thing is, there are "bindings" for multiple languages and frameworks. You can find the one for Ruby/Rails here. 

Honey Badger

Honey badger has a really simple purpose: To make sure you know which server you are in (say, testing, dev, production) by displaying a small "badge" with the name of your choosing.

If you are like me you can easily think you are debugging something in testing when you are really looking at production. Ok, maybe that's just me, but the badge does look cool, right? :)

God

Now, i'm not one to call the name of the Lord in vain (i am!), but God is something i can no longer do without. It's a process monitoring framework written in ruby that keeps an eye on your server's, say nginx, apache or mysql and makes sure they are always up.

Further checks can be done, like for example restart the server when memory usage goes above X or CPU usage goes above Y. It is also capable of notifying you by email, jabber, twitter, you name it, when things happen. It's a true life saver :)

Hope you find these gems useful, as i did, and if you have some that i might find interesting, please do share via twitter :)

Introducing Simple Rails Blog

Yes, a new blog. I know what you are thinking: "Who the hell cares?". But read me out.

A while back i looked at wordpress and thought "i don't really need all this stuff, do i?". Turns out i didn't. And if you are at least a little bit like me you will agree that sometimes less is more. After searching for some alternatives i ended up with two solutions: A company hosted blog (tumblr, blogger, and others) or hand made. I chose the latter. 

So let me introduce you to my new open-source Rails blog. Developed with simplicity in mind, it allows you to write and (easily, even automatically) tag posts. That's it.

Full source code is available at bitbucket. If you are a Rails developer (or a plain developer with an open mind) you will find it trivial to adapt it to your needs.

Enjoy, Pedro