Saturday 22 February 2014

A Night on the Town - Olde Mitres and Banks

For a change, I went up into London to have a drink with some erstwhile collegues. We started off at The Olde Mitre, one of the oldest pubs in London:


It's built round the back of a church and is a typical London drinking establishment, with a good selection of beers. I had a half of something called "Dark Star Original", a sort of Porter\Mild.


Very nice. After we'd all met up (the pub is down an alley, so very easy to miss), we moved on down to The Old Bank of England (both of them are Fuller's pubs), a complete contrast, and probably the largest pub I've ever been in:


Very grand. I finished the night there and, after fond farewells, wended my way back to BasVegas.

Thursday 20 February 2014

The Lincoln Lawyer

Back to the movies, this is an adaption of the bestseller by Michael Connelly starring current hot property Matthew McConaughey. Defence Lawyer Mickey Haller (McConaughey), used to dealing with orinary criminals, is suddenly hired to defend Beverly Hills playboy and heir Louis Roulet (Ryan Phillippe).


A very good plot and good script, combined with McConaughey's southern style makes this a superior film and an excellent Friday night pizza movie. McConaughey is excellent, as is the supporting cast, although they are somewhat underused, especially William H. Macy and Bryan Cranston, although it's good to see John Leguizamo and Michael Peña in something, as well as Marisa Tomei as Haller's ex-wife. Recommended.

Tuesday 18 February 2014

Creating a Custom Java AWT Component – Part 2: Resizing and Segment Control

The whole point of the control is to display a number, so inputting a number is just a case of setting a display value:

private int displayNumber = 8;

public int getDisplayNumber() {
    return displayNumber;
}

public void setDisplayNumber(int displayNumber) {
    this.displayNumber = displayNumber % 10;
    repaint();
}

A simple enough bean compatible getter and setter, but notice that in the setter, I've modulo 10, to restrict the input to single digits. I've also called the repaint method to re-display the component.

Now looking at the paint method, I only display a segment if it's used for a particular number. To determine this, I use the Arrays.binarySearch method on an array of the relevant numbers. For example, on the bottom left segment, which is only used by 2, 6, 8 and zero:

//bottom left
if(Arrays.binarySearch(new int[] {0,2,6,8}, displayNumber) > -1) {
    polygon = createSegment(createSegmentPoints(0, segmentHeight * 2 + segmentWidth, segmentHeight, segmentWidth));
    g2.fill(polygon);
}

Perhaps this is a bit over the top and there's a better way of doing it, but it saves all those OR clauses. This shows the segments and numbers when they are active:

Segment Number
Top 0,2,3,5,6,7,8,9
Top Left 0,4,5,6,8,9
Top Right 0,1,2,3,4,7,8,9
Middle 2,3,4,5,6,8,9
Bottom Left 0,2,6,8
Bottom Right 0,1,3,4,5,6,7,8,9
Bottom 0,2,3,5,6,8

All well and good so far. The only bit left to do is calculating the size of the segments in relation to the size of the panel. If you look at the previous diagram of the component:

SegmentArrangements

you can see that the height is three segment widths and two segment lengths. The width is two segment widths and one length. Using this as a basis, you can than say:

Ws = 2.Wp - Hp

Where Ws is the width of the segment, Wp is the width of the panel and Hp is the height of the panel. Correspondingly:

Ls = 2.Hp - 3.Wp

Where Ls is the length of the segment. You can see that for certain height and width ratios, the width and length of the segments are negative. Therefore:

2.Wp > Hp

and:

3.Wp < 2.Hp

Also:

Ws < Ls

Doing all the maths, we get that 50% < Wp < 60% of Hp. 55% sounds about right. Plugging this into the paint function:

int width = this.getHeight();
width *= 0.55;
int height = this.getHeight();
//set the width as a proportion of the height
this.setSize(width, height);
        
int segmentWidth = 2 * width - height;
int segmentLength = 2 * height - 3 * width;

Testing all this is simple enough. Put it all in a Frame, with a little timer to tick through the numbers:

private LEDPanel led;

public TestFrame(String title){
    //call the superclass constructor with the specified title
    super(title);

    //add the LED panel for testing
    led = new LEDPanel();
    add(led);
    led.setBackground(Color.black);
    led.setForeground(Color.red);
    led.setDisplayNumber(5);

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            led.setDisplayNumber(led.getDisplayNumber() + 1);
        }
    }, 2000, 1000);

    //add the exit listener and set the size
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    setSize(200, 300);
    //show the frame
    setVisible(true);
}

Now it's all working tickety-boo, I thought I'd go over the refectoring of the code in the next part.

Creating a Custom Java AWT Component - Part 1: Getting Started

As part of the course I'm on, I'm creating a custom AWT component. It's a derivative of the Panel component and has a number of shapes drawn on its surface using the Graphics2D package.

Figure8

To draw the graphic, I created two methods, createSegment and createSegmentPoints. First createSegment:

private GeneralPath createSegment(int[][] points){
    GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, points.length);
    polygon.moveTo(points[0][0], points[0][1]);

    for(int index = 1; index < points.length; index++) {
        polygon.lineTo(points[index][0], points[index][1]);
    }
    polygon.closePath();
    return polygon;
}

This method creates and returns a GeneralPath object, a polygon drawn (using the method lineTo) by a set of points, starting then finally completed with a call to the method closePath. The createSegmentPoints method creates an array of six points:

private int[][] createSegmentPoints(int originX, int originY, int width, int height) {
    int[][] points = new int[6][2];
    if(width >= height) {
        points[0] = new int[]{originX + height/2, originY};
        points[1] = new int[]{originX + width - height/2, originY};
        points[2] = new int[]{originX + width, originY + height/2};
        points[3] = new int[]{originX + width - height/2, originY + height};
        points[4] = new int[]{originX + height/2, originY + height};
        points[5] = new int[]{originX, originY + height/2};
    } else {
        points[0] = new int[]{originX + width/2, originY};
        points[1] = new int[]{originX + width, originY + width/2};
        points[2] = new int[]{originX + width, originY + height - width/2};
        points[3] = new int[]{originX + width/2, originY + height};
        points[4] = new int[]{originX, originY + height - width/2};
        points[5] = new int[]{originX, originY + width/2};
    }
    return points;
}

It uses the ration of height and width to determine if the segment is horizontal or vertical and then figures out the points from there.

Segment


SegmentEnd

Each segment is then rendered to the surface of the Panel in the paint method:

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    int height = 20;
    int width = 100;
    GeneralPath polygon;
    //top
    polygon = createSegment(createSegmentPoints(height, 0, width, height));
    g2.setPaint(Color.red);
    g2.fill(polygon);
    ...
}


SegmentArrangements

There is a problem with flickering on resize as it repaints and this could be solved using double-buffering, which I'll write about later.

Thursday 13 February 2014

2 Guns and The Yards

I seem to have been watching a lot of films with Mark Wahlberg in them recently. Previously there was the cops-n-crims drama We Own The Night, and now there's these two films. Firstly, The Yards, through the LoveFilm Instants streaming service.

Fresh out of jail, having taken the rap for his friends, Leo (Wahlberg) tries to get a job at the local train repair yard and gets taken under the wing of corrupt fixer Willie Gutierrez (Joaquin Phoenix) and his boss, and Leo's uncle, Frank Olchin (James Caan).


Made by the same director as We Own The Night, and with the same lead actors, the results are pretty much the same. Phoenix dominates as the corrupt and amoral Gutierrez, increasingly desperate as his plans go awry. Caan gives as good as he gets and brings a certain gravitas that only a man who's played opposite Pacino in the Godfather can. Charlize Theron is a surprise though, almost unrecognisable in what must have been, like Wahlberg, one of her first films. Wahlberg also looks young and is outclassed by the rest of the cast, but this was early in his career and you can see, looking at his later films, that he's improved, probably by knowing what roles he can't do as much as what he can.

Speaking of which, I watched one of his latest films, 2 Guns with Denzel Washington. Two agents, Bobby Trench (Washington) and Mike "Stig" Stigman (Wahlberg), unaware of each others true role, rob a bank to implicate a Mexican drugs baron Papi Greco (Edward James Olmos) and end up stealing $43 million belonging to the CIA.


A more-than-decent action film, with a fair amount of genial banter between the two leads. The plot is above average as well, with enough double crosses to make sure you don't take it for granted. The only thing which lets the whole thing down is the support, with Olmos looking like he can't wait for the cheque to arrive and Bill Paxton trying to be a mean CIA agent. Paula Patton is also underused as the love interest, but this is basically a buddy movie with a few twists and lots of bangs and crashes. Wahlberg takes to his role rather well, but is somewhat outclassed by Washington, as you'd expect (he's better than Sly, though). Overall, pretty good and a superior pizza movie.

Friday 7 February 2014

We Own The Night

A crime thriller from a few years ago. In the late 1980's, night club owner Bobby Green (Joaquin Phoenix) gets embroiled in a struggle between Russian drug dealers and the cops, represented by his brother (Mark Wahlberg) and his father (Robert Duvall).


A good story, if a bit by the numbers, and somewhat unevenly paced. Phoenix is good, but the supporting cast is underused, especially Duvall and Eva Mendes as Green's girlfriend. However, it makes for a better than average pizza movie.

Wreck-it Ralph

An homage to the arcade games of our youth. Wreck-it Ralph, a villain in an old arcade game, realises how unappreciated he is by the other characters in the game and decides to earn their respect by winning a medal.


An entertaining little film, with one or two twists to make the trip worthwhile. It does seem a bit confused at who it's aimed at, though. The plot and characters are really for children (12-year olds at most), but who of that age plays arcade games?

Still, the real joy is seeing arcade characters such as Q-Bert and Pacman "off-duty" in 3D. Recommended, but better for children, of whatever age.

Monday 3 February 2014

Bllt T Th Hd

(Sorry, that should be "Bullet to the Head": watching Sly for any length of time does that to you). From my LoveFilm subscription, this is a Crime\Buddy movie\whatever. Double-crossed by his employers, hitman "Bobo" Bonomo (seriously) seeks revenge with the help of a cop (Sung Kang).


Actually, if you can switch off the part of your brain that thinks Sly ought to be able to act a little by now, it's not a half-bad pizza movie. Sly is more than ably supported by the rest of the cast, especially Jason Momoa as the villain showing Sly how it's done, and Sarah Shahi as his daughter, but Christian Slater is wasted as a corrupt lawyer. Switch that part of your brain back on, though, and you can see it should have been someone else's movie.

Saturday 1 February 2014

The Lone Ranger

Through my LoveFilm subscription, after his brother is killed in an ambush, and left for dead, John Reid (Armie Hammer) seeks revenge with the help of Tonto (Johnny Depp), a Commanche outcast.


A superior pizza movie, Depp is a likeable and sympathetic Tonto, unrecognisable in old age, and is well supported, especially by William Fitchner giving it both barrels as the despicable Butch Cavendish, but also by Hammer, as the rube transformed into a western hero, Tom Wilkinson and Helena Bonham Carter. The film isn't too overwheled by the special effects and the plot zips along, so why it wasn't a box-office success I don't know. Maybe it was a Depp too far for some people. As the reporter at the end of "The Man Who Shot Liberty Valance" says, "This is the West, sir: when the legend becomes fact, print the legend!".

The Lego Movie

Seems like Lego have made their own movie and tied in the latest min-figs series:


A barista and an executive-type with amazing hair.

The movie seems a little weird: