2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 package org
.intellij
.images
.ui
;
21 import com
.intellij
.util
.ui
.UIUtil
;
22 import org
.intellij
.images
.editor
.ImageDocument
;
25 import javax
.swing
.plaf
.ComponentUI
;
27 import java
.awt
.image
.BufferedImage
;
30 * UI for {@link ImageComponent}.
32 * @author <a href="mailto:aefimov.box@gmail.com">Alexey Efimov</a>
34 public class ImageComponentUI
extends ComponentUI
{
35 private static final ImageComponentUI ui
= new ImageComponentUI();
37 public void paint(Graphics g
, JComponent c
) {
38 ImageComponent ic
= (ImageComponent
)c
;
40 ImageDocument document
= ic
.getDocument();
41 BufferedImage image
= document
.getValue();
45 Dimension size
= ic
.getCanvasSize();
46 Graphics igc
= g
.create(2, 2, size
.width
, size
.height
);
48 // Transparency chessboard
49 if (ic
.isTransparencyChessboardVisible()) {
50 paintChessboard(igc
, ic
);
56 if (ic
.isGridVisible()) {
65 private void paintBorder(Graphics g
, ImageComponent ic
) {
66 Dimension size
= ic
.getSize();
67 g
.setColor(ic
.getTransparencyChessboardBlackColor());
68 g
.drawRect(0, 0, size
.width
- 1, size
.height
- 1);
71 private void paintChessboard(Graphics g
, ImageComponent ic
) {
72 Dimension size
= ic
.getCanvasSize();
74 int cellSize
= ic
.getTransparencyChessboardCellSize();
75 int patternSize
= 2 * cellSize
;
76 BufferedImage pattern
= new BufferedImage(patternSize
, patternSize
, BufferedImage
.TYPE_INT_ARGB
);
77 Graphics imageGraphics
= pattern
.getGraphics();
78 imageGraphics
.setColor(ic
.getTransparencyChessboardWhiteColor());
79 imageGraphics
.fillRect(0, 0, patternSize
, patternSize
);
80 imageGraphics
.setColor(ic
.getTransparencyChessboardBlackColor());
81 imageGraphics
.fillRect(0, cellSize
, cellSize
, cellSize
);
82 imageGraphics
.fillRect(cellSize
, 0, cellSize
, cellSize
);
84 ((Graphics2D
)g
).setPaint(new TexturePaint(pattern
, new Rectangle(0, 0, patternSize
, patternSize
)));
85 g
.fillRect(0, 0, size
.width
, size
.height
);
88 private void paintImage(Graphics g
, ImageComponent ic
) {
89 ImageDocument document
= ic
.getDocument();
90 Dimension size
= ic
.getCanvasSize();
91 g
.drawImage(document
.getRenderer(), 0, 0, size
.width
, size
.height
, ic
);
94 private void paintGrid(Graphics g
, ImageComponent ic
) {
95 Dimension size
= ic
.getCanvasSize();
96 BufferedImage image
= ic
.getDocument().getValue();
97 int imageWidth
= image
.getWidth();
98 int imageHeight
= image
.getHeight();
99 double zoomX
= (double)size
.width
/ (double)imageWidth
;
100 double zoomY
= (double)size
.height
/ (double)imageHeight
;
101 double zoomFactor
= (zoomX
+ zoomY
) / 2.0d
;
102 if (zoomFactor
>= ic
.getGridLineZoomFactor()) {
103 g
.setColor(ic
.getGridLineColor());
104 int ls
= ic
.getGridLineSpan();
105 for (int dx
= ls
; dx
< imageWidth
; dx
+= ls
) {
106 UIUtil
.drawLine(g
, (int)((double)dx
* zoomX
), 0, (int)((double)dx
* zoomX
), size
.height
);
108 for (int dy
= ls
; dy
< imageHeight
; dy
+= ls
) {
109 UIUtil
.drawLine(g
, 0, (int)((double)dy
* zoomY
), size
.width
, (int)((double)dy
* zoomY
));
114 @SuppressWarnings({"UnusedDeclaration"})
115 public static ComponentUI
createUI(JComponent c
) {