Professional Documents
Culture Documents
Jun01 Daniel
Jun01 Daniel
default values.
In general, these implementations of
javax.media.jai.OperationDescriptor have names of the form
<opName>Descriptor. The corresponding operation name that is
passed in as the first parameter for JAI.create() is <opName>.
There are more than one hundred possible operations. In the
remainder of this tip, you will apply a convolution to a jpeg
image, and save the transformed image as a tiff file.
The February 10, 2004 tip "Styling Digital Images with
ConvolveOp"
(http://java.sun.com/developer/JDCTechTips/2004/tt0210.html#1)
showed how to blur or sharpen an image using a convolution.
If you followed that tip, you created a 3-by-3 kernel for
a convolution. The convoluton was based on a normalized linear
combination of the pixel itself, the four pixels with which it
shares an edge, and the four pixels with which it shares
a corner. You will do the same thing here, but you'll use the
corresponding JAI class javax.media.jai.KernelJAI, as follows:
import javax.media.jai.KernelJAI;
public class My3x3JAIKernels {
private static final float[] IDENTITY = {0, 0, 0,
0, 1, 0,
0, 0, 0};
private static final float[] EDGE =
{0, 1, 0,
1, 0, 1,
0, 1, 0};
{1, 0, 1,
0, 0, 0,
1, 0, 1};
javax.swing.JFrame;
javax.swing.JPanel;
javax.media.jai.PlanarImage;
javax.media.jai.JAI;
javax.media.jai.KernelJAI;
java.awt.image.BufferedImage;
java.awt.image.renderable.ParameterBlock;
java.awt.Graphics;
}
Compile and run JAIConvolve using values that show off the
different effects. For example, to blur the image use:
java JAIConvolve 1 0 0
To view an "etching" of the image pass in the parameters -1 -1 8.
Your local directory should also now contain the file target.tiff
which is a tiff you can share with others that captures your
convolution.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - CALENDAR UTILITIES IN JDESKTOP NETWORK COMPONENTS
The April 5, 2005 tip "Introduction to Tables with JDesktop
Network Components (JDNC)"
(http://java.sun.com/developer/JDCTechTips/2005/tt0405.html#2)
showed how to use JDNC to read tab-separated data from a file
and display it in a table. In this tip, you will use the Java
APIs for JDNC to create simple calendar widgets. You can use
these widgets to pick dates and to display events in a month
view. There are many rich offerings in JDNC, this tip gives you
a quick look at how the APIs make it easier to perform common
tasks.
To run the examples in this tip, download the latest JDNC
release from the jdnc download page
(https://jdnc.dev.java.net/servlets/ProjectDocumentList). The
latest version is currently 0.7 and requires J2SE 5.0. Download
and expand the file jdnc-0_7-bin.zip. Create a directory for
the examples in this tip, and copy into it the three jar files
from jdnc-0_7/lib (jdnc-0_7-all, jdnc-runner, and jlfgr-1_0).
You will also need to add to your classpath the three jar files
you just copied.
Let's start with a very simple thing you can do with JDNC, that
is add a component to be used for picking dates. You can do that
with the following single line of code:
frame.addComponent(new JXDatePicker());
The method addComponent() should be a hint that the variable
frame, which refers to the top level container, is not a JFrame.
In fact, you are using an org.jdesktop.swing.JXFrame, which
extends a JFrame by adding some functionality. The following
example program, DatePicker, creates a JXFrame object and adds
a newly created JXDatePicker object to it:
import org.jdesktop.swing.JXDatePicker;
import org.jdesktop.swing.JXFrame;
public class DatePicker {
public static void main(String[] args) {
JXFrame frame = new JXFrame("Date");
frame.addComponent(new JXDatePicker());
frame.setResizable(false);
frame.setVisible(true);
}
}
Compile and run DatePicker. You will see a small frame containing
today's date.
The text in this component is editable. You can
also press the button on the right side of the frame and a menu
containing the current month appears. Today's date is selected
and is framed. Click on a different day. The picker disappears
and the newly selected date is in the frame. Click on the button
again and the month view again appears. This time the selected
date has a grey background but the today's date is framed with
the square. You can use the arrows to move forward or backwards a
month at a time.
The month view that is visible when you click on the button in
the date picker is also available as a stand alone component in
the org.jdesktop.swing.calendar package. You can use the
JXMonthView class to display any calendar month and to allow the
user to select a range of days. The no argument constructor of
JMXMonthView is used to instantiate a JXMonthView that displays
the current month. The current date is framed and no date is
selected.
You can customize the look of the calendar by changing the
colors. This includes the color that appears behind the name of
the month, the background of the body of the calendar, the color
of the frame around today's date, and the color that appears for
selected days. Other customizations allow you to specify the
first day of the week as well as the character used to represent
each day of the week.
Also, through the setSelectionMode() method, you to specify
a mode that configures what the user can select. A mode of
NO_SELECTION means that the user cannot select any days in the
calendar. The mode default is SINGLE_SELECTION, it allows the
user to select one value at a time. If SINGLE_SELECTION is
chosen, only one date will appear to be selected, even if a user
programatically selects a range of days. MULTIPLE_SELECTION
allows a user to select a range of consecutive days by clicking
on one day and dragging to others. WEEK_SELECTION restricts this
choice by changing selections longer than seven days to exactly
a week from the day clicked.
The following example program, MonthView, creates a new
JXMonthView. The program then sets the color for selected days
to red and the frame for today's date to green. The program
allows multiple days to be selected.
import org.jdesktop.swing.JXFrame;
import org.jdesktop.swing.calendar.JXMonthView;
import java.awt.Color;
public class MonthView extends JXFrame {
public MonthView(){
setResizable(false);
org.jdesktop.swing.JXFrame;
org.jdesktop.swing.calendar.JXMonthView;
org.jdesktop.swing.calendar.DateSpan;
org.jdesktop.swing.calendar.DateUtils;
import java.awt.Color;
import java.util.GregorianCalendar;
import java.util.Calendar;
public class RangeOfDates extends JXFrame {
private long todaysDate;
private JXMonthView monthView;
RangeOfDates() {
setResizable(false);
setUpMonthView();
markDateSpan();
flagDates();
addComponent(monthView);
}
private void setUpMonthView() {
todaysDate = (new GregorianCalendar(2005,
6, 15)).getTimeInMillis();
monthView = new JXMonthView(todaysDate);
monthView.setFirstDayOfWeek(Calendar.MONDAY);
}
private void markDateSpan() {
long startDate = DateUtils.previousWeek(todaysDate);
long endDate = DateUtils.previousDay(todaysDate);
monthView.setSelectedBackground(Color.LIGHT_GRAY);
monthView.setSelectionMode(
JXMonthView.MULTIPLE_SELECTION);
monthView.setSelectedDateSpan(new DateSpan(startDate,
endDate));
}
private void flagDates() {
long[] flaggedDates = {
DateUtils.getStartOfMonth(todaysDate),
todaysDate,
DateUtils.getEndOfMonth(todaysDate)
};
monthView.setFlaggedDates(flaggedDates);
}
public static void main(String[] args) {
new RangeOfDates().setVisible(true);
}
}