- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Drawing with Cairo II
In this part of the Java Gnome programming tutorial, we will continue drawing with the Cairo library.
Donut
In the following example we create a complex shape by rotating a bunch of ellipses.
donut.java
package com.zetcode; import org.freedesktop.cairo.Context; import org.freedesktop.cairo.Matrix; import org.gnome.gdk.Event; import org.gnome.gdk.EventExpose; import org.gnome.gtk.DrawingArea; import org.gnome.gtk.Gtk; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program draws a Donut * shape on the drawing area. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GDonut extends Window implements Widget.ExposeEvent { public GDonut() { setTitle("Donut"); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setDefaultSize(300, 260); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { DrawingArea darea = new DrawingArea(); darea.connect(this); add(darea); } public boolean onExposeEvent(Widget widget, EventExpose eventExpose) { final Context cr; cr = new Context(widget.getWindow()); drawDonut(cr); return false; } public void drawDonut(Context cr) { int width = this.getWindow().getWidth(); int height = this.getWindow().getHeight(); cr.setLineWidth(0.5); cr.translate(width/2, height/2); cr.arc( 0, 0, 120, 0, 2 * Math.PI); cr.stroke(); cr.save(); for ( int i = 0; i < 36; i++) { Matrix mat = new Matrix(); mat.rotate(i*Math.PI/36); mat.scale(0.3, 1); cr.transform(mat); cr.arc(0, 0, 120, 0, 2 * Math.PI); cr.restore(); cr.stroke(); cr.save(); } } public static void main(String[] args) { Gtk.init(args); new GDonut(); Gtk.main(); } }
In this example, we create a donut. The shape resembles a cookie, hence the name donut.
cr.translate(width/2, height/2); cr.arc( 0, 0, 120, 0, 2 * Math.PI); cr.stroke();
In the beginning there is an ellipse.
Matrix mat = new Matrix(); mat.rotate(i*Math.PI/36); mat.scale(0.3, 1); cr.transform(mat); cr.arc(0, 0, 120, 0, 2 * Math.PI); cr.restore(); cr.stroke(); cr.save();
After several rotations, there is a donut.

Gradients
In computer graphics, gradient is a smooth blending of shades from light to dark or from one color to another. In 2D drawing programs and paint programs, gradients are used to create colorful backgrounds and special effects as well as to simulate lights and shadows. (answers.com)
gradients.java
package com.zetcode; import org.freedesktop.cairo.Context; import org.freedesktop.cairo.LinearPattern; import org.gnome.gdk.Color; import org.gnome.gdk.EventExpose; import org.gnome.gtk.DrawingArea; import org.gnome.gtk.Gtk; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program draws gradients. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GGradients extends Window implements Widget.ExposeEvent { public GGradients() { setTitle("Gradients"); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setDefaultSize(340, 390); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { DrawingArea darea = new DrawingArea(); darea.connect(this); add(darea); } public boolean onExposeEvent(Widget widget, EventExpose eventExpose) { final Context cr; cr = new Context(widget.getWindow()); drawGradients(cr); return false; } public void drawGradients(Context cr) { LinearPattern lg1 = new LinearPattern(0.0, 0.0, 350.0, 350.0); int count = 1; for (double j=0.1; j<1.0; j+= 0.1) { if (count % 2 != 0) { lg1.addColorStopRGB(j, 0, 0, 0); } else { lg1.addColorStopRGB(j, 1, 0, 0); } count++; } cr.rectangle(20, 20, 300, 100); cr.setSource(lg1); cr.fill(); LinearPattern lg2 = new LinearPattern(0.0, 0.0, 350.0, 0); count = 1; for (double i=0.05; i<0.95; i+= 0.025) { if (count % 2 != 0) { lg2.addColorStopRGB(i, 0, 0, 0); } else { lg2.addColorStopRGB(i, 0, 0, 1); } count++; } cr.rectangle(20, 140, 300, 100); cr.setSource(lg2); cr.fill(); LinearPattern lg3 = new LinearPattern(20.0, 260.0, 20.0, 360.0); lg3.addColorStopRGB(0.1, 0, 0, 0 ); lg3.addColorStopRGB(0.5, 1, 1, 0); lg3.addColorStopRGB(0.9, 0, 0, 0 ); cr.rectangle(20, 260, 300, 100); cr.setSource(lg3); cr.fill(); } public static void main(String[] args) { Gtk.init(args); new GGradients(); Gtk.main(); } }
In our example, we draw three rectangles with three different gradients.
LinearPattern lg1 = new LinearPattern(0.0, 0.0, 350.0, 350.0);
Here we create a linear gradient pattern. The parameters specify the line, along which we draw the gradient. In our case it is a diagonal line.
LinearPattern lg3 = new LinearPattern(20.0, 260.0, 20.0, 360.0); lg3.addColorStopRGB(0.1, 0, 0, 0 ); lg3.addColorStopRGB(0.5, 1, 1, 0); lg3.addColorStopRGB(0.9, 0, 0, 0 );
We define colour stops to produce our gradient pattern. In this case, the gradient is a blending of black and yellow colours. By adding two black and one yellow stops, we create a horizontal gradient pattern. What do these stops actually mean? In our case, we begin with black colour, which will stop at 1/10 of the size. Then we begin to gradually paint in yellow, which will culminate at the centre of the shape. The yellow colour stops at 9/10 of the size, where we begin painting in black again, until the end.

Star
In the following example, we create a moving star. The star moves, rotates and grows/shrinks.
star.java
package com.zetcode; import java.util.Timer; import java.util.TimerTask; import org.freedesktop.cairo.Context; import org.freedesktop.cairo.Matrix; import org.gnome.gdk.Event; import org.gnome.gdk.EventExpose; import org.gnome.gtk.DrawingArea; import org.gnome.gtk.Gtk; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program shows an animated star. Rotate, * translate and scale operations are * applied on the star. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GStar extends Window implements Widget.ExposeEvent { private static Timer timer; private int count; private double angle = 0; private double scale = 1; private double delta = 0.01; double points[][] = { { 0, 85 }, { 75, 75 }, { 100, 10 }, { 125, 75 }, { 200, 85 }, { 150, 125 }, { 160, 190 }, { 100, 150 }, { 40, 190 }, { 50, 125 }, { 0, 85 } }; public GStar() { setTitle("Star"); timer = new Timer(); timer.scheduleAtFixedRate(new ScheduleTask(), 100, 20); count = 0; initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { timer.cancel(); Gtk.mainQuit(); return false; } }); setSizeRequest(350, 250); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { DrawingArea darea = new DrawingArea(); darea.connect(this); add(darea); } public void drawStar(Context cr) { int width = this.getWindow().getWidth(); int height = this.getWindow().getHeight(); cr.setSource(0, 0.44, 0.7); cr.setLineWidth(1); Matrix mat = new Matrix(); mat.translate(width/2, height/2); mat.rotate(angle); mat.scale(scale, scale); cr.transform(mat); for ( int i = 0; i < 10; i++ ) { cr.lineTo(points[i][0], points[i][1]); } cr.fill(); cr.stroke(); if ( scale < 0.01 ) { delta = -delta; } else if (scale > 0.99) { delta = -delta; } scale += delta; angle += 0.01; } public boolean onExposeEvent(Widget widget, EventExpose eventExpose) { Context cr = new Context(widget.getWindow()); drawStar(cr); return false; } class ScheduleTask extends TimerTask { public void run() { count++; queueDraw(); } } public static void main(String[] args) { Gtk.init(args); new GStar(); Gtk.main(); } }
We apply translate, scale and rotate operations on a star shape.
public boolean onDeleteEvent(Widget source, Event event) { timer.cancel(); Gtk.mainQuit(); return false; }
For a clean exit, we must not forget to stop the timer. Timer
is and object of the Java util library and is not stopped by the Gtk.mainQuit()
method.
double points[][] = { { 0, 85 }, { 75, 75 }, { 100, 10 }, { 125, 75 }, { 200, 85 }, { 150, 125 }, { 160, 190 }, { 100, 150 }, { 40, 190 }, { 50, 125 }, { 0, 85 } };
These points are used to build the star shape.
Matrix mat = new Matrix(); mat.translate(width/2, height/2); mat.rotate(angle); mat.scale(scale, scale); cr.transform(mat);
Here we apply the translate, rotate, and scale operations on the star shape.
for ( int i = 0; i < 10; i++ ) { cr.lineTo(points[i][0], points[i][1]); }
Here we draw the star.
Waiting
In this examle, we use transparency effect to create a waiting demo. We will draw 8 lines that will gradually fade out creating an illusion that a line is moving. Such effects are often used to inform users, that a lengthy task is going on behind the scenes. An example is streaming video over the internet.
waiting.java
package com.zetcode; import java.util.Timer; import java.util.TimerTask; import org.freedesktop.cairo.Context; import org.gnome.gdk.Event; import org.gnome.gdk.EventExpose; import org.gnome.gtk.DrawingArea; import org.gnome.gtk.Gtk; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program creates a waiting * effect. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GWaiting extends Window implements Widget.ExposeEvent { private static Timer timer; private int count; private final double[][] trs = { { 0.0, 0.15, 0.30, 0.5, 0.65, 0.80, 0.9, 1.0 }, { 1.0, 0.0, 0.15, 0.30, 0.5, 0.65, 0.8, 0.9 }, { 0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65, 0.8 }, { 0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5, 0.65 }, { 0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3, 0.5 }, { 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15, 0.3 }, { 0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, 0.15 }, { 0.15, 0.3, 0.5, 0.65, 0.8, 0.9, 1.0, 0.0, } }; public GWaiting() { setPosition(WindowPosition.CENTER); timer = new Timer(); timer.scheduleAtFixedRate(new ScheduleTask(), 100, 80); count = 0; initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { timer.cancel(); Gtk.mainQuit(); return false; } }); resize(250, 150); setTitle("Waiting"); showAll(); } public void initUI() { DrawingArea darea = new DrawingArea(); add(darea); darea.connect(this); } public boolean onExposeEvent(Widget widget, EventExpose eventExpose) { Context cr = new Context(widget.getWindow()); drawWaiting(cr); return false; } private void drawWaiting(Context cr) { int w = this.getWidth(); int h = this.getHeight(); cr.translate(w/2, h/2); for (int i = 0; i < 8; i++) { cr.setLineWidth(3); cr.setSource(0, 0, 0, trs[count%8][i]); cr.moveTo(0, -10); cr.lineTo(0, -40); cr.rotate(Math.PI/4f); cr.stroke(); } } class ScheduleTask extends TimerTask { public void run() { count++; queueDraw(); } } public static void main(String[] args) { Gtk.init(args); new GWaiting(); Gtk.main(); } }
We draw eight lines with eight different alpha values.
private final double[][] trs = { { 0.0, 0.15, 0.30, 0.5, 0.65, 0.80, 0.9, 1.0 }, .... };
This is a two dimensional array of transparency values used in this demo. There are 8 rows, each for one state. Each of the 8 lines will continuosly use these values.
cr.setLineWidth(3);
We make the lines a bit thicker, so that they are better visible.
cr.setSource(0, 0, 0, trs[count%8][i]);
Here we define the transparency value for a line.
cr.moveTo(0, -10); cr.lineTo(0, -40); cr.rotate(Math.PI/4f); cr.stroke();
These code lines will draw each of the eight lines.

In this chapter of the Java Gnome programming library, we did some more advanced drawing with the Cairo library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论