Results 1 to 1 of 1
- 11-03-2009, 06:27 AM #1
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 285
- Rep Power
- 12
A Framework for building/testing components
Some recent posts have had problems organizing the outline of a program.
To add yet another option, I'll include below the sort of framework
I use for building/testing JComponents. Each source file has its own main()
method and an enclosed class called TestFrame. The main method creates
an instance of the JComponent and then an instance of TestFrame displaying
that JComponent. Finally it starts the ball rolling by calling the TestFrame's
setVisibility() method.
Java Code:/* * Simple sample Java-Swing program framework. * @author zweibieren * [COLOR="DarkGreen"] * When I build a new JComponent, I like to be able to test it all by itself. * To do this, I add a main() method and a TestFrame object to the source file. * The JPanel could have several JComponents laid out within it, * but here the SamplePanel object itself does all the drawing. * * When this file is compiled and executed it displays a window containing the SamplePanel object. * * To use this to build and test widgets, remove everything in SamplePanel but TestFrame and main(). * Then write your new object in place of the SamplePanel object.[/COLOR] */ package com.physpics.tools; [COLOR="DarkGreen"]// you should use your own package name[/COLOR] import java.awt.*; import java.awt.geom.RoundRectangle2D; import javax.swing.*; /** Simple Sample. Writes a box with my name in it. [COLOR="DarkGreen"] * Each new JComponent must have its own name. [/COLOR] */ public class SamplePanel extends JPanel { // some constants for appearance static final Color royalPurple = new Color(0x6B3FA0); // http://en.wikipedia.org/wiki/Purple static final Stroke width5 = new BasicStroke(5); static final Font font14 = new Font("Serif", Font.BOLD, 14); // constants for layout static final int baseline = 30; static final int boxHeight = 25; static final int boxWidth = 85; int boxLeft = 20; // for positioning the drawing public SamplePanel() { /[COLOR="DarkGreen"]/ if this component does its own drawing, it needs to say how big it wants to be // this can be omitted if this component only encloses other JComponents // (via a layout manager and add("child1", otherJComponent)) setPreferredSize(new Dimension(boxWidth+20, 50)); } [COLOR="DarkGreen"] // to do drawing on a JPanel, provide a paintComponent method // the contents here are just a sample[/COLOR] @Override public void paintComponent(Graphics g) { // center the image by setting boxLeft int panelW = getWidth(); // width of the area allot to the 'this' object boxLeft = (panelW - boxWidth) /2; // write "Zweibieren" g.setColor(Color.BLUE); g.setFont(font14); g.drawString("Zweibieren", boxLeft+10, baseline); // draw a single pixel under the string g.setColor(Color.ORANGE); g.drawLine(boxLeft+10, baseline+3, boxWidth+7, baseline+3); g.drawLine(boxLeft+10, baseline+4, boxWidth+7, baseline+4); // draw a rounded rectangle with lines 5 pixels wide Graphics2D g2 = (Graphics2D) g; // in Swing g is always a Graphics2D //RoundRectangle2D.Double(double x, double y, double w, double h, double arcw, double arch) RoundRectangle2D.Double r2d2 = new RoundRectangle2D.Double(boxLeft, baseline-16, boxWidth, boxHeight, 10,10); g2.setStroke(width5); g2.setColor(royalPurple); g2.draw(r2d2); } /** [COLOR="DarkGreen"] * Frame for testing any JComponent. * I have an instance of this class within every JComponent class. * I usually tailor its contents to the outer class. * Sometimes I add listeners to print debugging output.[/COLOR] */ static public class TestFrame extends javax.swing.JFrame { public TestFrame(JComponent contents) { setDefaultCloseOperation(EXIT_ON_CLOSE); [COLOR="DarkGreen"]// putting the window at a location on the screen[/COLOR] // setLocationRelativeTo(null); // center the window on the screen // OR setLocation(100,20); // put window in upper left [COLOR="DarkGreen"]// With a borderLayout I can display other Components alongside the main one. Often I write my own LayoutManager[/COLOR] BorderLayout layout = new BorderLayout(); getContentPane().setLayout(layout); add("Center", contents); pack(); [COLOR="DarkGreen"]// do layout processing[/COLOR] } } /** [COLOR="DarkGreen"]* I have a main() method in every JComponent source file.[/COLOR] * @param args Command line arguments. Usually empty. */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { [COLOR="DarkGreen"]// create an object to test[/COLOR] SamplePanel picture = new SamplePanel(); [COLOR="DarkGreen"]// create the TestFrame to display the object[/COLOR] TestFrame outer = new TestFrame(picture); outer.setVisible(true); // et voila ! } }); } }
Similar Threads
-
Problem with String Building
By Albert in forum New To JavaReplies: 2Last Post: 04-30-2012, 12:49 AM -
JAR Building Problem
By GhosT in forum New To JavaReplies: 12Last Post: 09-27-2008, 03:21 PM -
building a house
By dc2acgsr99 in forum Java AppletsReplies: 4Last Post: 03-07-2008, 11:18 PM -
Building a document from a DOM
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:22 AM
Bookmarks