| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.view; |
| 16 | |
| 17 | import java.awt.Color; |
| 18 | import java.awt.Dimension; |
| 19 | import java.awt.Graphics; |
| 20 | import java.awt.Graphics2D; |
| 21 | import java.awt.Rectangle; |
| 22 | import java.awt.image.BufferedImage; |
| 23 | |
| 24 | import javax.swing.JPanel; |
| 25 | import javax.swing.Scrollable; |
| 26 | import javax.swing.SwingConstants; |
| 27 | |
| 28 | import net.sourceforge.hiveboard.model.BoardImageModel; |
| 29 | import net.sourceforge.hiveboard.model.BoardImageModelListener; |
| 30 | |
| 31 | public class DrawingArea |
| 32 | extends JPanel |
| 33 | implements Scrollable, BoardImageModelListener |
| 34 | { |
| 35 | public DrawingArea(BoardImageModel model) |
| 36 | { |
| 37 | super(null, false); |
| 38 | setName("drawing-area"); |
| 39 | setOpaque(true); |
| 40 | setBackground(Color.WHITE); |
| 41 | setAutoscrolls(true); |
| 42 | _model = model; |
| 43 | _model.addBoardImageModelListener(this); |
| 44 | _size = new Dimension(_model.getWidth(), _model.getHeight()); |
| 45 | setMinimumSize(_size); |
| 46 | setPreferredSize(_size); |
| 47 | setMaximumSize(_size); |
| 48 | } |
| 49 | |
| 50 | public void contentRefreshed(Rectangle content) |
| 51 | { |
| 52 | if (content != null) |
| 53 | { |
| 54 | repaint(content); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | repaint(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public BoardImageModel getBoardImageModel() |
| 63 | { |
| 64 | return _model; |
| 65 | } |
| 66 | |
| 67 | @Override protected void paintComponent(Graphics g) |
| 68 | { |
| 69 | // Ensure background is painted |
| 70 | super.paintComponent(g); |
| 71 | // Then draw the image |
| 72 | BufferedImage image = _model.getImage(); |
| 73 | if (image != null) |
| 74 | { |
| 75 | g.drawImage(image, 0, 0, this); |
| 76 | // Draw additional hilites directly on the Graphics |
| 77 | _model.paintHilites((Graphics2D) g); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public Dimension getPreferredScrollableViewportSize() |
| 82 | { |
| 83 | return _size; |
| 84 | } |
| 85 | |
| 86 | @Override public Dimension getPreferredSize() |
| 87 | { |
| 88 | return _size; |
| 89 | } |
| 90 | |
| 91 | @Override public Dimension getMaximumSize() |
| 92 | { |
| 93 | return _size; |
| 94 | } |
| 95 | |
| 96 | public int getScrollableUnitIncrement( Rectangle visibleRect, |
| 97 | int orientation, |
| 98 | int direction) |
| 99 | { |
| 100 | return UNIT_SCROLL_PIXELS; |
| 101 | } |
| 102 | |
| 103 | public int getScrollableBlockIncrement( Rectangle visibleRect, |
| 104 | int orientation, |
| 105 | int direction) |
| 106 | { |
| 107 | if (orientation == SwingConstants.HORIZONTAL) |
| 108 | { |
| 109 | return visibleRect.width - UNIT_SCROLL_PIXELS; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | return visibleRect.height - UNIT_SCROLL_PIXELS; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public boolean getScrollableTracksViewportWidth() |
| 118 | { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | public boolean getScrollableTracksViewportHeight() |
| 123 | { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | private BoardImageModel _model; |
| 128 | private Dimension _size; |
| 129 | |
| 130 | static final private int UNIT_SCROLL_PIXELS = 8; |
| 131 | } |