<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java SWT 繪圖 > 原文: [http://zetcode.com/gui/javaswt/painting/](http://zetcode.com/gui/javaswt/painting/) 在 Java SWT 教程的這一部分中,我們進行了一些繪制。 在實現`Drawable`接口的對象上執行繪制。 這包括`Control`,`Image`,`Display`設備或`Printer`設備。 `org.eclipse.swt.graphics.GC`是一個圖形上下文,其中封裝了可以執行的繪制操作。 使用`GC`有兩種常見方法; 通過使用`Drawable`實例作為構造器參數創建一個,或使用作為`paintEvent`回調的一部分提供的`GC`創建一個。 ## 色彩 在第一個示例中,我們處理顏色。 顏色是代表紅色,綠色和藍色(RGB)強度值的組合的對象。 在 Java SWT 中,有效的 RGB 值在 0 到 255 之間。 `ColoursEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program draws three rectangles. * The interiors are filled with * different colors. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class ColoursEx { private Shell shell; public ColoursEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.addListener(SWT.Paint, event -> drawRectangles(event)); shell.setText("Colours"); shell.setSize(360, 120); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawRectangles(Event e) { GC gc = e.gc; Color c1 = new Color(e.display, 50, 50, 200); gc.setBackground(c1); gc.fillRectangle(10, 15, 90, 60); Color c2 = new Color(e.display, 105, 90, 60); gc.setBackground(c2); gc.fillRectangle(130, 15, 90, 60); Color c3 = new Color(e.display, 33, 200, 100); gc.setBackground(c3); gc.fillRectangle(250, 15, 90, 60); c1.dispose(); c2.dispose(); c3.dispose(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); ColoursEx ex = new ColoursEx(display); display.dispose(); } } ``` 在我們的示例中,我們繪制了三個矩形,并用三種不同的顏色填充它們。 ```java shell.addListener(SWT.Paint, event -> drawRectangles(event)); ``` 我們為繪圖事件添加了繪圖監聽器。 ```java private void drawRectangles(Event e) { GC gc = e.gc; ... } ``` 生成繪圖事件時將調用`drawRectangles()`方法。 我們獲得了圖形上下文的句柄,該上下文是我們在其上執行繪制操作的對象。 ```java Color c1 = new Color(e.display, 50, 50, 200); ``` 我們創建一個顏色對象。 ```java gc.setBackground(c1); ``` `setBackground()`方法為繪圖文本和形狀的內部設置顏色。 ```java gc.fillRectangle(10, 15, 90, 60); ``` `fillRectangle()`用背景色填充指定的矩形。 ```java c1.dispose(); c2.dispose(); c3.dispose(); ``` 在繪圖結束時,釋放顏色資源。 ![Colours](https://img.kancloud.cn/25/71/2571266bb6b0abeedf66c1c6178717bf_356x117.jpg) 圖:顏色 ## 直線 `drawLine()`方法在可繪制對象上繪制一條線。 `setLineStyle()`方法指定線條的樣式。 以下是內置的 SWT 線型: * `SWT.LINE_DOT` * `SWT.LINE_DASH` * `SWT.LINE_DASHDOT` * `SWT.LINE_DASHDOTDOT` * `SWT.LINE_SOLID` 也可以使用`SWT.LINE_CUSTOM`選項創建自定義線條樣式。 `LineStylesEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program draws text on the window. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class LineStylesEx { public LineStylesEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display); shell.addListener(SWT.Paint, event -> drawLyrics(event)); shell.setText("Line styles"); shell.setSize(300, 330); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawLyrics(Event e) { GC gc = e.gc; gc.setLineWidth(2); gc.setLineStyle(SWT.LINE_DASHDOT); gc.drawLine(20, 40, 250, 40); gc.setLineStyle(SWT.LINE_DASH); gc.drawLine(20, 80, 250, 80); gc.setLineStyle(SWT.LINE_DASHDOTDOT); gc.drawLine(20, 120, 250, 120); gc.setLineStyle(SWT.LINE_SOLID); gc.drawLine(20, 160, 250, 160); gc.setLineStyle(SWT.LINE_DOT); gc.drawLine(20, 200, 250, 200); gc.setLineStyle(SWT.LINE_CUSTOM); gc.setLineDash(new int[] {1, 4, 5, 4}); gc.drawLine(20, 240, 250, 240); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); LineStylesEx ex = new LineStylesEx(display); display.dispose(); } } ``` 該示例繪制了五條標準樣式線和一種自定義樣式。 ```java gc.setLineWidth(2); ``` `setLineWidth()`設置繪制線時使用的寬度。 ```java gc.setLineStyle(SWT.LINE_DASHDOT); ``` `setLineStyle()`將線條樣式設置為`SWT.LINE_DASHDOT`。 該線由點劃線組成。 ```java gc.drawLine(20, 40, 250, 40); ``` `drawLine()`畫一條線。 參數是起點和終點的 x 和 y 坐標。 ```java gc.setLineStyle(SWT.LINE_CUSTOM); gc.setLineDash(new int[] {1, 4, 5, 4}); gc.drawLine(20, 240, 250, 240); ``` 這些線創建自定義線型樣式。 整數數組指定行間距和筆劃線的寬度。 在我們的示例中,圖案為 1 像素筆劃線,4 像素間隔,5 像素筆劃線和 4 像素間隔。 對整個行重復此模式。 ![Line styles](https://img.kancloud.cn/a4/78/a478356e5530c973615e02553fc2a31a_296x327.jpg) 圖:線型 ## 基本形狀 下一個示例將一些基本形狀繪制到窗口上。 `BasicShapesEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * In this program, we draw some * basic shapes. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class BasicShapesEx { private Shell shell; public BasicShapesEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.addListener(SWT.Paint, event -> drawShapes(event)); shell.setText("Basic shapes"); shell.setSize(430, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawShapes(Event e) { GC gc = e.gc; gc.setAntialias(SWT.ON); Color col = new Color(e.display, 150, 150, 150); gc.setBackground(col); gc.fillRectangle(20, 20, 120, 80); gc.fillRectangle(180, 20, 80, 80); gc.fillOval(290, 20, 120, 70); gc.fillOval(20, 150, 80, 80); gc.fillRoundRectangle(150, 150, 100, 80, 25, 25); gc.fillArc(280, 150, 100, 100, 0, 115); col.dispose(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); BasicShapesEx ex = new BasicShapesEx(display); display.dispose(); } } ``` 在此示例中,我們將創建一個矩形,一個正方形,一個橢圓形,一個圓形,一個圓角矩形和一個圓弧。 ```java gc.fillRectangle(20, 20, 120, 80); gc.fillRectangle(180, 20, 80, 80); gc.fillOval(290, 20, 120, 70); ``` 這些線繪制一個矩形,一個正方形和一個橢圓形。 ```java gc.fillOval(20, 150, 80, 80); ``` 在這里`fillOval()`方法畫一個圓。 ```java gc.fillRoundRectangle(150, 150, 100, 80, 25, 25); gc.fillArc(280, 150, 100, 100, 0, 115); ``` 這兩條線繪制了一個圓角的矩形和一個圓弧。 ![Basic shapes](https://img.kancloud.cn/29/19/2919387c855b04a704434819e90def51_426x297.jpg) 圖:基本形狀 ## 多邊形 多邊形是具有直邊的二維平面形狀。 `PolygonEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program draws a star. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class PolygonEx { private final int points[] = { 0, 85, 75, 75, 100, 10, 125, 75, 200, 85, 150, 125, 160, 190, 100, 150, 40, 190, 50, 125, 0, 85 }; public PolygonEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display); shell.addListener(SWT.Paint, event -> drawPolygon(event)); shell.setText("Polygon"); shell.setSize(280, 280); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawPolygon(Event e) { GC gc = e.gc; Color grayCol = new Color(e.display, 120, 120, 120); gc.setBackground(grayCol); gc.fillPolygon(points); grayCol.dispose(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); PolygonEx ex = new PolygonEx(display); display.dispose(); } } ``` 該示例繪制了一個起始對象。 ```java private final int points[] = { 0, 85, 75, 75, 100, 10, 125, 75, 200, 85, 150, 125, 160, 190, 100, 150, 40, 190, 50, 125, 0, 85 }; ``` 這些是多邊形的坐標。 該數組由成對的 x 和 y 坐標組成。 ```java Color grayCol = new Color(e.display, 120, 120, 120); ``` 多邊形以某種灰色繪制。 ```java gc.fillPolygon(points); ``` `fillPolygon()`填充封閉多邊形的內部,該多邊形由指定的整數坐標數組定義。 ![Polygon](https://img.kancloud.cn/ba/d0/bad0f6417750944a999b83b4f11af1cd_276x277.jpg) 圖:多邊形 ## 透明矩形 透明性是指能夠透視材料的質量。 了解透明度的最簡單方法是想象一塊玻璃或水。 從技術上講,光線可以穿過玻璃,這樣我們就可以看到玻璃后面的物體。 在計算機圖形學中,我們可以使用 alpha 合成來實現透明效果。 Alpha 合成是將圖像與背景組合以創建部分透明外觀的過程。 合成過程使用 Alpha 通道。 (wikipedia.org,answers.com) `TransparentRectanglesEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program draws ten rectangles with different * levels of transparency. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class TrasparentRectanglesEx { public TrasparentRectanglesEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.addListener(SWT.Paint, event -> drawRectangles(event)); shell.setText("Transparent rectangles"); shell.setSize(590, 120); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawRectangles(Event e) { GC gc = e.gc; Color blueCol = new Color(e.display, 0, 0, 255); gc.setBackground(blueCol); for (int i = 1; i < 11; i++) { gc.setAlpha(i * 25); gc.fillRectangle(50 * i, 20, 40, 40); } blueCol.dispose(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); TrasparentRectanglesEx ex = new TrasparentRectanglesEx(display); display.dispose(); } } ``` 在該示例中,我們繪制了十個透明度不同的矩形。 ```java gc.setAlpha(i * 25); ``` `setAlpha()`方法設置 alpha 透明度值。 ![Transparent rectangles](https://img.kancloud.cn/82/5d/825d1561cf65147b4dad31bd4ddb0560_586x117.jpg) 圖:透明矩形 ## 甜甜圈 在下面的示例中,我們通過旋轉一堆橢圓來創建復雜的形狀。 `DonutEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Transform; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program creates a donut shape. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class DonutEx { public DonutEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.addListener(SWT.Paint, event -> drawDonut(event)); shell.setText("Donut"); shell.setSize(430, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawDonut(Event e) { GC gc = e.gc; int w = e.width; int h = e.height; gc.setAntialias(SWT.ON); Transform tr = new Transform(e.display); tr.translate(w / 2, h / 2); gc.setTransform(tr); for (int rot = 0; rot < 36; rot++) { tr.rotate(5f); gc.setTransform(tr); gc.drawOval(-125, -40, 250, 80); } tr.dispose(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); DonutEx ex = new DonutEx(display); display.dispose(); } } ``` 在此示例中,我們創建一個甜甜圈。 形狀類似于曲奇,因此稱為甜甜圈。 ```java gc.setAntialias(SWT.ON); ``` 我們使用`setAntialias()`方法打開抗鋸齒功能,這可以使繪圖更平滑。 ```java Transform tr = new Transform(e.display); tr.translate(w / 2, h / 2); gc.setTransform(tr); ``` 我們將軸的中心移到窗口的中心。 ```java for (int rot = 0; rot < 36; rot++) { tr.rotate(5f); gc.setTransform(tr); gc.drawOval(-125, -40, 250, 80); } ``` 在`for`循環中,我們進行旋轉操作并繪制橢圓。 ## 繪制文字 在下一個示例中,我們在窗口上繪制一些文本。 `SoulmateEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program draws text * on the window. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class LyricsEx { public LyricsEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display); shell.addListener(SWT.Paint, event -> drawLyrics(event)); shell.setText("Soulmate"); shell.setSize(380, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void drawLyrics(Event e) { GC gc = e.gc; gc.setAntialias(SWT.ON); Font font = new Font(e.display, "Purisa", 10, SWT.NORMAL); Color col = new Color(e.display, 25, 25, 25); gc.setForeground(col); gc.setFont(font); gc.drawText("Most relationships seem so transitory", 20, 30); gc.drawText("They're good but not the permanent one", 20, 60); gc.drawText("Who doesn't long for someone to hold", 20, 120); gc.drawText("Who knows how to love without being told", 20, 150); gc.drawText("Somebody tell me why I'm on my own", 20, 180); gc.drawText("If there's a soulmate for everyone", 20, 210); col.dispose(); font.dispose(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); LyricsEx ex = new LyricsEx(display); display.dispose(); } } ``` 我們顯示 Natasha Bedingfields Soulmate 歌曲的部分歌詞。 ```java Font font = new Font(e.display, "Purisa", 10, SWT.NORMAL); ``` 在這里,我們指定使用的字體。 ```java gc.drawText("Most relationships seem so transitory", 20, 30); ``` `drawText()`方法將文本繪制到窗口上。 ![Soulmate](https://img.kancloud.cn/3a/e1/3ae17477a606b28cea5bfef06bf0354f_376x297.jpg) 圖:靈魂伴侶 在 Java SWT 教程的這一章中,我們做了一些繪圖。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看