📄 darea.java
字号:
if( may < min_rect.y + min_rect.height ) throw
new IllegalArgumentException(
"Maximal y-value axes intersects minmal rectangle.");
max_y = new Double( may );
}
/**
* method resets the maximal y-value
*/
public void releaseMaxY(){
max_y = null;
}
/**
* method returns the maximal y-value which can be displayed in the DArea
*
* @return the maximal y-value
*/
public double getMaxY(){
if( max_y != null ) return max_y.doubleValue();
return 0;
}
/**
* paints the DArea by a Graphics object
*
* @param g the java.awt.Graphics object
*/
public void paint( Graphics g ){
if( under_construction ) System.out.println("DArea.paint(Graphics)");
if( auto_focus ) {
container.restore();
visible_rect = (DRectangle)container.getRectangle().clone();
}
if( visible_rect.isEmpty() ) visible_rect = (DRectangle)min_rect.clone();
super.paint( g );
measures.setGraphics( g );
if( grid.isVisible() && !grid_to_front ) paintGrid( measures );
container.paint( measures );
if( grid.isVisible() && grid_to_front ) paintGrid( measures );
}
/**
* repaints a part of the visible area
*
* @param r the rectangle to repaint
*/
public void repaint( DRectangle r ){
if( under_construction ) System.out.println("DArea.repaint(DRectangle)"+r);
if( r == null ) throw
new IllegalArgumentException("Cannot repaint a null DRectangle");
if( r.isAll() || auto_focus ) repaint();
else{
Point p1 = measures.getPoint( r.x, r.y ),
p2 = measures.getPoint( r.x + r.width, r.y + r.height);
if( p1 == null || p2 == null ) repaint();
else {
DBorder b = getDBorder();
repaint( p1.x - b.left,
p2.y - b.top,
p2.x - p1.x + 1 + b.left + b.right,
p1.y - p2.y + 1 + b.top + b.bottom );
}
}
}
/**
* adds a new component to the area
*
* @param e the new DElement
*/
public void addDElement( DElement e ){
container.addDElement( e );
}
/**
* removes a certain element from the area
*
* @param e the element to remove
*/
public boolean removeDElement( DElement e ){
return container.removeDElement( e );
}
/**
* removes all elements from the area
*/
public void removeAllDElements()
{
visible_rect = (DRectangle)min_rect.clone();
container.removeAllDElements();
}
/**
* returnes all DElements in the container
*
* @return the elements of the container
*/
public DElement[] getDElements(){
return container.getDElements();
}
/**
* method returns true, if the given element is contained in the container
*
* @param e the element
* @return if it is contained
*/
public boolean contains( DElement e ){
return container.contains( e );
}
/**
* sets the grid visible or not
*
* @param aFlag visible or not
*/
public void setGridVisible( boolean aFlag ){
if( under_construction ) System.out.println("DArea.setGridVisisble: "+aFlag);
grid.rectangle = getDRectangle();
grid.setVisible( aFlag );
}
/**
* returns if the grid is visible
* <code>true</code> if the grid is visible or <code>false</code> if not
*
* @return true or false
*/
public boolean isGridVisible(){
return grid.isVisible();
}
/**
* sets the grid to the front
* that means that the grid is painted as last element
* default value is <code>false</code>
*
* @param aFlag grid t front or not
*/
public void setGridToFront( boolean aFlag ){
boolean old = grid_to_front;
grid_to_front = aFlag;
if( old != aFlag && grid.isVisible() ) repaint();
}
/**
* sets the grid's horizontal and vertical distance
* that means that the grid's lines will have these distances
* in area coordinates
*
* @param hor_dist the horizontal distance
* @param ver_dist the vertical distance
*/
public void setGrid( double hor_dist, double ver_dist ){
grid = new DGrid( visible_rect, hor_dist, ver_dist );
grid.setDParent( this );
auto_grid = false;
repaint();
}
/**
* sets the auto grid on or off
* if it's on, the grid's distances (@see #setGrid(double, double))
* are automatically calculated
*
* @param b auto grid on or not
*/
public void setAutoGrid( boolean b ){
if( b ) {
grid.rectangle = getDRectangle();
grid.setVisible( true );
}
if( b == auto_grid ) return;
auto_grid = b;
repaint();
}
/**
* returns if the auto grid is switched on
*
* @return true if the grid is on, else false
*/
public boolean hasAutoGrid(){ return auto_grid; }
/**
* sets the color of the grid
*
* @param java.awt.Color
*/
public void setGridColor( Color color ){
grid.setColor( color );
}
/**
* sets the maximal number of grid lines
* default value is 10
*
* @param no maximal number of grid lines
*/
public void setMaxGrid( int no ){
if( no < 1 ) return;
int old = max_grid;
max_grid = no;
if( old != no ) repaint();
}
/**
* prints the area and it's content
* @see java.awt.print.Printable and
* @see java.awt.print.PrintJob
*
* @param g the Graphics object
* @param pf the @see java.awt.print.PageFormat
* @param pi the page index
*
* @return int @see java.awt.print.Printable
*/
public int print( Graphics g, PageFormat pf, int pi ){
if( under_construction ) System.out.println("DArea.print(...)");
if( pi > 0 ) return Printable.NO_SUCH_PAGE;
Border sb = getBorder();
if( !(sb instanceof ScaledBorder) ) sb = null;
else ( (ScaledBorder)sb ).show_outer_border = false;
PagePrinter printer = new PagePrinter( this, g, pf );
int ret = printer.print();
if( sb != null ) ( (ScaledBorder)sb ).show_outer_border = true;
return ret;
}
/**
* sets a new scale function to the x-axis
* That means that not the standard linear scale is shown but the image
* of the linear scale under the given function
*
* @param x_s the scale function for the x-axis
*/
public void setXScale( DFunction x_s ){
if( x_s == null && measures.x_scale == null ) return;
measures.x_scale = x_s;
repaint();
}
/**
* sets a new scale function to the y-axis
* That means that not the standard linear scale is shown but the image
* of the linear scale under the given function
*
* @param y_s the scale function for the y-axis
*/
public void setYScale( DFunction y_s ){
if( y_s == null && measures.y_scale == null ) return;
measures.y_scale = y_s;
repaint();
}
/**
* returns the measures of the DArea
* The DMeasures object calculates the different coodinates and contains a
* Graphics object to paint the DElements of the area
*
* @return the measures of the DArea
*/
public DMeasures getDMeasures(){
return measures;
}
/**
* method 'adds' a certain border around the contained rectangle
* that means that it takes the old border
* and takes the maxima of the old and the new values
*
* @param clip the java.awt.Insets object of the new clip
*/
public void addDBorder( DBorder b ){
dborder.insert(b);
}
/**
* method returns the current border around the rectangle
*
* @return the border
*/
public DBorder getDBorder(){
return dborder;
}
public void restoreBorder(){
dborder = container.getDBorder();
if( under_construction ) System.out.println("DArea.restoreBorder -> "+dborder);
}
/**
* method paints the grid
* how the method paints the grid depends on whether the area is wrapped in a
* <code>ScaledBorder</code> or not and on the auto_grid option
*/
private void paintGrid( DMeasures m ){
if( under_construction ) System.out.println("DArea.paintGrid(DMeasures)");
grid.rectangle = getDRectangle();
if( auto_grid ){
Border b = getBorder();
if( b instanceof ScaledBorder ){
ScaledBorder sb = (ScaledBorder)b;
paintGrid( sb, m );
return;
}
else{
grid.hor_dist = ScaledBorder.aBitBigger( grid.rectangle.width / max_grid );
grid.ver_dist = ScaledBorder.aBitBigger( grid.rectangle.height / max_grid );
}
}
grid.paint( m );
}
/**
* paints the grid when auto_grid is selected and the area is surrounded
* by an instance of ScaledBorder
*
* @param sb the ScaledBorder around the area
* @param m the measures of the area
*/
private void paintGrid(ScaledBorder sb, DMeasures m){
if( under_construction ) System.out.println("DArea.paintGrid(ScaledBorder, DMeasures)");
Dimension d = getSize();
FontMetrics fm = m.getGraphics().getFontMetrics();
grid.hor_dist = sb.getSrcdX(fm, d);
grid.ver_dist = sb.getSrcdY(fm, d);
if( m.x_scale == null && m.y_scale == null ) grid.paint( m );
else{// selber malen
Graphics g = m.g;
g.setColor( grid.getColor() );
DRectangle rect = getDRectangle(),
src_rect = m.getSourceOf( rect );
int x = (int)(src_rect.x / grid.hor_dist),
y = (int)(src_rect.y / grid.ver_dist);
if( x * grid.hor_dist < src_rect.x ) x++;
if( y * grid.ver_dist < src_rect.y ) y++;
DPoint min = new DPoint( rect.x, rect.y ),
max = new DPoint( min.x + rect.width, min.y + rect.height );
double pos;
for( ; (pos = x * grid.hor_dist) < src_rect.x + src_rect.width; x++ ){
if( m.x_scale != null ) pos = m.x_scale.getImageOf( pos );
Point p1 = m.getPoint( pos, min.y ),
p2 = m.getPoint( pos, max.y );
g.drawLine( p1.x, p1.y, p2.x, p2.y );
}
for( ; (pos = y * grid.ver_dist) < src_rect.y + src_rect.height; y++ ){
if( m.y_scale != null ) pos = m.y_scale.getImageOf( pos );
Point p1 = m.getPoint( min.x, pos ),
p2 = m.getPoint( max.x, pos );
g.drawLine( p1.x, p1.y, p2.x, p2.y );
}
}
}
}
/****************************************************************************
* END OF FILE
****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -