Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2009, 06:27 AM
Senior Member
 
Join Date: Aug 2009
Location: Pittsburgh, PA
Posts: 266
Rep Power: 2
zweibieren is on a distinguished road
Smile 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.

Code:
/*
 * Simple sample Java-Swing program framework.
 * @author zweibieren
 *
 * 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.
 */

package com.physpics.tools;    // you should use your own package name

import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import javax.swing.*;

/** Simple Sample. Writes a box with my name in it.
 * Each new JComponent must have its own name.
 */
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));
	}

	// to do drawing on a JPanel, provide a paintComponent method
	// the contents here are just a sample
	@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);
	}

	/**
	 * 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.
	 */
	static public class TestFrame  extends javax.swing.JFrame {
		public TestFrame(JComponent contents) {
			setDefaultCloseOperation(EXIT_ON_CLOSE);

			// putting the window at a location on the screen
			//		setLocationRelativeTo(null);   //	center the window on the screen
			// OR
			setLocation(100,20);   // put window in upper left

			// With a borderLayout I can display other Components alongside the main one.
			Often I write my own LayoutManager
			BorderLayout layout = new BorderLayout();
			getContentPane().setLayout(layout);
			add("Center", contents);
			pack();   // do layout processing
		}
	}

	/**
	 * I have a main() method in every JComponent source file.
	 * @param args Command line arguments.  Usually empty.
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				// create an object to test
				SamplePanel picture = new SamplePanel();    
				// create the TestFrame to display the object
				TestFrame outer = new TestFrame(picture);
				outer.setVisible(true);   // et voila !
			}
		});
	}
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
demo, jcomponent, jframe, sample

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JAR Building Problem GhosT New To Java 12 09-27-2008 03:21 PM
building a house dc2acgsr99 Java Applets 4 03-07-2008 11:18 PM
Building a document from a DOM Java Tip Java Tips 0 01-03-2008 09:22 AM
Problem with String Building Albert New To Java 0 07-09-2007 07:20 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:39 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org