/*
* 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 !
}
});
}
} |