//***************************************************************************** // Copyright (c) 1998 INRIA. // http://www.inria.fr/croap/aioli/modules/figue/web/credits.html#COPYRIGHT //***************************************************************************** package figue.tutorial; import java.awt.GridLayout; import java.awt.Font; import java.awt.Component; import java.awt.Graphics; import java.awt.FontMetrics; import java.awt.event.MouseEvent; import java.awt.AWTEvent; import figue.*; import figue.box.*; import figue.path.*; import figue.resource.*; public final class Tutorial_15 extends TutorialApplet { /** * For code identification with unix what command. * @level internal */ public final static String VERSION_ID = "@(#) $Source: /net/croap/CVSROOT/figue/java/tutorial/Tutorial_15.java,v $ $revision$ $Date: 1998/11/02 15:55:51 $ Copyright 1998 INRIA."; protected final void initTutorial() { setLayout(new GridLayout(1,1)); _facade = new AWTFacade(getParameters()); add(_facade.getAwtComponent()); setDefaultKeyListener(_facade); initResources(); buildBox(); } public final void destroy() { _facade.disposeWhenInactive(); _facade = null; super.destroy(); } private final void buildBox() { final GlyphInterface theColumn = new Vertical(); theColumn.addChild(newLine(0.0,"top")); final GlyphInterface theNewContext = new ChangeGraphicalContextNamed("style"); theColumn.addChild(theNewContext); theNewContext.addChild(newLine(0.5,"middle")); theColumn.addChild(newLine(1.0,"bottom")); PathInterface thePath = new Path(); thePath.addOperation(new InsertGlyph(theColumn,0)); _facade.buildInit(thePath); _facade.buildComplete(); } private GlyphInterface newLine(double anAlignment, String aText) { GlyphInterface theLine = new Horizontal(10); theLine.addChild(Atom.newAtom("Alignment")); theLine.addChild(Atom.newAtom("on")); LightweightInterface theLightweight = new MyLightweight((float)0.0,(float)anAlignment); theLine.addChild(new LightweightComponentAdapter(theLightweight)); theLine.addChild(Atom.newAtom(aText)); return theLine; } private final void initResources() { try { _facade.doGetFont(Constants.DEFAULT_FONT,FontFamily.TIMES_ROMAN,FontStyle.PLAIN,15,false); _facade.doGetColor(Constants.DEFAULT_COLOR,BasicColor.BLUE); _facade.doGetColor(Constants.DEFAULT_BACKGROUND,BasicColor.WHITE); _facade.doGetFont("italic",FontFamily.TIMES_ROMAN,FontStyle.BOLD_ITALIC,30,false); _facade.doGetColor("green",BasicColor.GREEN); _facade.doGetStyle("style", _facade.getFont("italic"), _facade.getColor("green"), null); } catch ( WrongResourceException anException ) { ; } initStandardResources(_facade); } private final FacadeInitializer getParameters() { FacadeInitializer theParameters = new FacadeInitializer(); theParameters.setFormattingWidth(675); setAppletParameters(theParameters); return theParameters; } private AWTFacade _facade; class MyLightweight extends Component implements LightweightInterface { public MyLightweight(float alignmentX, float alignmentY) { _alignmentX = alignmentX; _alignmentY = alignmentY; _pressedFlag = false; _parent = null; _label = GROW_LABEL; _counter = 0; _increment = 1; setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR)); enableEvents(AWTEvent.MOUSE_EVENT_MASK); } public Object clone() throws CloneNotSupportedException { final MyLightweight theResult = (MyLightweight) super.clone(); return theResult; } public void repaint() { if ( _parent != null ) { _parent.refresh(); } } public void invalidate() { if ( _parent != null ) { _parent.invalidate(); } } public void setParent(LightweightComponentAdapter aGlyph) { _parent = aGlyph; } public float getAlignmentX() { return _alignmentX; } public float getAlignmentY() { return _alignmentY; } public void paint(Graphics g) { update(g); } public void update(Graphics g) { int s = Math.min(getSize().width - 1, getSize().height - 1); if(_pressedFlag) { g.setColor(java.awt.Color.red); } else { g.setColor(getForeground()); } g.fillArc(0, 0, s, s, 0, 360); g.drawArc(0, 0, s, s, 0, 360); Font f = getFont(); if(f != null) { FontMetrics fm = getFontMetrics(getFont()); g.setColor(getBackground()); g.drawString(_label, s/2 - fm.stringWidth(_label)/2, s/2 + fm.getMaxDescent()); } } public java.awt.Dimension getPreferredSize() { Font f = getFont(); if(f != null) { FontMetrics fm = getFontMetrics(getFont()); final int x = 40 + (_counter * 5); int max = Math.max(fm.stringWidth(_label) + x, fm.getHeight() + x); return new java.awt.Dimension(max, max); } else { return new java.awt.Dimension(100, 100); } } public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(100, 100); } public void processMouseEvent(MouseEvent e) { Graphics g; switch(e.getID()) { case MouseEvent.MOUSE_PRESSED: _pressedFlag = true; repaint(); break; case MouseEvent.MOUSE_RELEASED: if(_pressedFlag == true) { _pressedFlag = false; repaint(); } break; case MouseEvent.MOUSE_CLICKED: _counter+=_increment; if ( _counter < 0 ) { _increment = +1; _label = MyLightweight.GROW_LABEL; } else { if ( _counter > 10 ) { _increment = -1; _label = MyLightweight.SHRINK_LABEL; } } invalidate(); break; case MouseEvent.MOUSE_ENTERED: break; case MouseEvent.MOUSE_EXITED: if(_pressedFlag == true) { _pressedFlag = false; repaint(); } break; } super.processMouseEvent(e); } private float _alignmentX; private float _alignmentY; private String _label; protected boolean _pressedFlag; private int _counter; private int _increment; private LightweightComponentAdapter _parent; private final String GROW_LABEL = " grow "; private final String SHRINK_LABEL = "shrink"; } }