- 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
Painting in Qyoto II
In this part of the Qyoto C# programming tutorial we will continue with painting. We will present some more complex examples.
Donut Shape
The firs example creates a complex shape by rotating a bunch of ellipses.
using System; using QtCore; using QtGui; /** * ZetCode Qyoto C# tutorial * * This program draws a donut * shape. * * @author Jan Bodnar * website zetcode.com * last modified November 2012 */ public class QyotoApp : QMainWindow { public QyotoApp() { WindowTitle = "Donut"; PaintEvent += OnPaintEvent; Resize(350, 280); Move(300, 300); Show(); } private void OnPaintEvent(object sender, QEventArgs<QPaintEvent> e) { QPainter ptr = new QPainter(this); DrawDonut(ptr); ptr.End(); } void DrawDonut(QPainter ptr) { QColor col = new QColor(); col.SetNamedColor("#333333"); ptr.Pen = new QPen(col, 0.5); ptr.SetRenderHint(QPainter.RenderHint.Antialiasing); int h = Height; int w = Width; ptr.Translate(new QPoint(w/2, h/2)); for (double rot=0; rot < 360.0; rot+=5.0 ) { ptr.DrawEllipse(-125, -40, 250, 80); ptr.Rotate(5.0); } } [STAThread] public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } }
In this example, we create a donut. The shape resembles a cookie, hence the name donut.
QColor color = new QColor(); color.SetNamedColor("#333333");
We can use a hexadecimal notation to create a colour object.
int h = Height; int w = Width;
Here we determine the width and height of the window.
ptr.Translate(new QPoint(w/2, h/2));
We move the coordinate system to the middle of the window. This way we make the drawing mathematically easier.
for (double rot=0; rot < 360.0; rot+=5.0 ) { ptr.DrawEllipse(-125, -40, 250, 80); ptr.Rotate(5.0); }
We draw an ellipse object 72 times. Each time, we rotate the ellipse by 5 degrees. This will create our donut shape.

Grayscale image
In the following example, we will create a gray scale image.
using System; using QtGui; using QtCore; /** * ZetCode Qyoto C# tutorial * * In this example, we create a * grayscale image. * * @author Jan Bodnar * website zetcode.com * last modified November 2012 */ public class QyotoApp : QMainWindow { QImage sid; int w, h = 0; public QyotoApp() { WindowTitle = "Gray scale"; PaintEvent += OnPaintEvent; LoadImage(); Resize(320, 150); Move(300, 300); Show(); } private void OnPaintEvent(object sender, QEventArgs<QPaintEvent> e) { QPainter ptr = new QPainter(this); DrawImages(ptr); ptr.End(); } void DrawImages(QPainter ptr) { ptr.DrawImage(5, 15, sid); ptr.DrawImage(w + 10, 15, GrayScale(sid.Copy())); } void LoadImage() { sid = new QImage("smallsid.jpg"); w = sid.Width(); h = sid.Height(); } QImage GrayScale(QImage img) { for (int i=0; i < w; i++) { for (int j=0; j < h; j++) { uint c = img.Pixel(i, j); int gray = Global.qGray(c); int alpha = Global.qAlpha(c); img.SetPixel(i, j, Global.qRgba(gray, gray, gray, alpha)); } } return img; } public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } }
We have a colour JPG image. We draw it on the window. We create a copy of the image, convert it to gray scale and draw it on the window next to the original image.
void LoadImage() { sid = new QImage("smallsid.jpg"); w = sid.Width(); h = sid.Height(); }
In the LoadImage() method we load the image and get its width and height.
QImage GrayScale(QImage img) { for (int i=0; i < w; i++) { for (int j=0; j < h; j++) { uint c = img.Pixel(i, j); int gray = Global.qGray(c); int alpha = Global.qAlpha(c); img.SetPixel(i, j, Global.qRgba(gray, gray, gray, alpha)); } } return img; }
The GrayScale()
method transforms an image to gray scale and returns it. We go through all pixels of the image. The Pixel()
method returns a pixel in question. We use the Global.qGray()
method to get the gray value of the specific pixel. Similarly, we get the alpha value. Finally, we modify the pixel with the SetPixel()
method. We use the gray value for the red, green and blue parts of the colour.

Reflection
In the next example we show a reflected image. The effect makes an illusion as if the image was reflected in water.
using System; using QtGui; using QtCore; /** * ZetCode Qyoto C# tutorial * * In this example we create a reflected image. * * @author Jan Bodnar * website zetcode.com * last modified November 2012 */ public class QyotoApp : QMainWindow { QImage img; QImage reflected_img; int iw, ih = 0; double initial_opacity = 0.7; double opacity = 0.7; double step = 0; const int GAP = 30; public QyotoApp() { WindowTitle = "Reflection"; PaintEvent += OnPaintEvent; InitExample(); Resize(300, 400); Move(150, 150); Show(); } private void OnPaintEvent(object sender, QEventArgs<QPaintEvent> e) { QPainter ptr = new QPainter(this); DrawImages(ptr); ptr.End(); } void InitExample() { img = new QImage("slanec.png"); if (img.IsNull()) { Console.WriteLine("Error loading image"); } iw = img.Width(); ih = img.Height(); step = opacity / ih; reflected_img = new QImage(iw, ih, QImage.Format.Format_RGB32); CreateReflectedImage(); } void CreateReflectedImage() { QPainter fptr = new QPainter(reflected_img); int i = 0; double opacity = 0.7; while (i < ih) { i++; opacity = opacity - step; fptr.Opacity = initial_opacity-opacity; fptr.DrawImage(0, i, img, 0, i, -1, 1); } fptr.End(); } void DrawImages(QPainter ptr) { int w = Width; int h = Height; ptr.FillRect(0, 0, w, h, Qt.GlobalColor.black); ptr.SetRenderHint(QPainter.RenderHint.Antialiasing); QRect r = new QRect(25, 15, iw, ih); ptr.DrawImage(r, img); ptr.Translate(0, 2 * ih + GAP); ptr.Scale(1, -1); ptr.DrawImage(25, 0, reflected_img); } public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } }
We load an image from the current working directory. We create another empty image of the same size. We copy the original image to the new empty image line by line, applying the gradually increasing transparency.
img = new QImage("slanec.png"); if (img.IsNull()) { Console.WriteLine("Error loading image"); }
We load a PNG image and do some error checking.
iw = img.Width(); ih = img.Height(); step = opacity / ih;
We get the image width and height. The step variable controls the intensity of the fading out of second image.
reflected_img = new QImage(iw, ih, QImage.Format.Format_RGB32);
A new empty image is created. It has the size of the original image.
void CreateReflectedImage() { QPainter fptr = new QPainter(reflected_img); ...
In the CreateReflectedImage() method, we draw on the empty image.
while (i < ih) { i++; opacity = opacity - step; fptr.Opacity = initial_opacity-opacity; fptr.DrawImage(0, i, img, 0, i, -1, 1); }
We copy the original image to the new image. Line by line. The opacity decreases by step each loop.
QRect r = new QRect(25, 15, iw, ih); ptr.DrawImage(r, img);
The first image is drawn on the window.
ptr.Translate(0, 2 * ih + GAP); ptr.Scale(1, -1); ptr.DrawImage(25, 0, reflected_img);
Here we move the second image down, some space below the original one. The Scale()
method flips the image upside down. Note that the translation is twice the image height. This is necessary, because the scaling operation not only flips the image, but also moves the image up. To understand this, simply take a photograph, place it on the table and flip it.

Waiting effect
In this example, 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.
using System; using QtGui; using QtCore; /** * ZetCode Qyoto C# tutorial * * This program draws basic shapes * available in Qyoto. * * @author Jan Bodnar * website zetcode.com * last modified November 2012 */ public class QyotoApp : QMainWindow { int count = 0; 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 QyotoApp() { WindowTitle = "Waiting"; PaintEvent += OnPaintEvent; InitExample(); Resize(300, 200); Move(300, 300); Show(); } private void OnPaintEvent(object sender, QEventArgs<QPaintEvent> e) { QPainter ptr = new QPainter(this); DrawLines(ptr); ptr.End(); } void InitExample() { count = 0; StartTimer(105); } void DrawLines(QPainter ptr) { QPen pen = new QPen(); pen.Width = 3; pen.CapStyle = PenCapStyle.RoundCap; int w = Width; int h = Height; ptr.Translate(w/2, h/2); ptr.Pen = pen; int len = trs.GetLength(0); for (int i=0; i < len; i++) { ptr.Opacity = trs[count%8, i]; ptr.DrawLine(0, -10, 0, -40); ptr.Rotate(45); } } protected override void OnTimerEvent(QTimerEvent e) { count++; Repaint(); } public static int Main(String[] args) { new QApplication(args); new QyotoApp(); return QApplication.Exec(); } }
We draw eight lines with eight different alpha values.
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 }, ...
This is an array of transparency values. There are 8 rows, each for one position. Each of the 8 lines will continuously use these values.
count = 0; StartTimer(105);
Here we initiate the count value and start a timer.
QPen pen = new QPen(); pen.Width = 3; pen.CapStyle = PenCapStyle.RoundCap;
We make the lines a bit thicker, so that they are more visible. We draw the lines with rounded caps. Lines with rounded caps look better.
for (int i=0; i < len; i++) { ptr.Opacity = trs[count%8, i]; ptr.DrawLine(0, -10, 0, -40); ptr.Rotate(45); }
In this loop, we set an opacity value. We draw the line and rotate it. This creates an illusion of a moving and fading line.
protected override void OnTimerEvent(QTimerEvent e) { count++; Repaint(); }
Each time the timer event is called, we increase the count value and repaint the window area.

In this part of the Qyoto C# programming tutorial, we finished talking about painting in Qyoto.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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