-3

Here is an example of the sort of C program one could write in the old days:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

void main()
{
    int gd=DETECT, gm;

    initgraph(&gd, &gm, "c:\\turboc\\bgi");
    circle(200,100,150);

    getch();
    closegraph();
}

I think this was turbo C under MSDOS. It gives you a drawing on the screen and can be easily extended to do speedy animated graphics such as those found in xscreensaver hacks.

How would I write the equivalent in gcc on ubuntu? Can it be done in Java?

John Lawrence Aspden
  • 15,995
  • 11
  • 61
  • 100
  • 3
    What do you mean equivalent in gcc? – drum May 06 '14 at 21:39
  • I mean, can I just draw something easily, or do I have to spend three pages of code negotiating for permission with X? – John Lawrence Aspden May 06 '14 at 21:40
  • @JohnLawrenceAspden What did you find from your research about `graphics.h` on Linux? – admdrew May 06 '14 at 21:42
  • Here is a demonstration of JavaFX. You have to install JavaFX first but once it is running you can see and play with the code for demo. http://www.oracle.com/technetwork/java/javase/overview/javafx-samples-2158687.html – Peter Lawrey May 06 '14 at 21:44
  • 2
    Just a remark: BGI graphics was anything but speedy. It was all right for low-resolution graphics but even then it was far slower than the same graphics routines written in assembly. – xxbbcc May 06 '14 at 21:48
  • if you want to draw on a window, in java you can use the Graphics class that help you drawing object on a surface, for the console i think you need some os depend library. http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html – WileTheCoyot May 06 '14 at 21:50

3 Answers3

3
#include<X11/Xlib.h>
#include<stdlib.h>

/* gcc -std=gnu99 -o circle circle.c -lX11 */

int main (int argc, char *argv[])
{

  /* connect to the X server and make a window */
  Display *dpy = XOpenDisplay (getenv ("DISPLAY"));
  Window w = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy),
                                  100, 100, 640, 480, 1,
                                  BlackPixel (dpy, DefaultScreen (dpy)),
                                  WhitePixel (dpy, DefaultScreen (dpy)));

  /* raise it and wait */
  XSelectInput (dpy, w, StructureNotifyMask);
  XMapRaised (dpy, w);
  for(XEvent e; ( e.type != MapNotify );
      XWindowEvent (dpy, w, StructureNotifyMask, &e));

  /* create a graphics context for drawing in the window */
  GC g = XCreateGC (dpy, w, 0, NULL);

  /* draw a circle */
  XDrawArc(dpy,w,g,200,100,150,150,0,360*64);
  XFlush(dpy);

  /*wait for key press*/
  XSelectInput (dpy, w, KeyReleaseMask);
  for(XEvent e; ( e.type != KeyRelease ); 
      XWindowEvent (dpy, w, KeyReleaseMask, &e));

  /*clean up*/
  XDestroyWindow( dpy, w );
  XCloseDisplay (dpy);
}
John Lawrence Aspden
  • 15,995
  • 11
  • 61
  • 100
2

Ok, few words about basics:

  1. The example you provided uses library called BGI - Borland Graphics Interface - very old stuff from MS DOS era
  2. GCC itself is just a compiler - you must search for a library that supports drawing
  3. On Linux we use several GUI toolkits, but only Gtk and Qt are relevant these days.
  4. If you want low-level graphics library you may look at Allegro (http://alleg.sourceforge.net/) or SDL (http://www.libsdl.org/)

But seriously, I think you're looking at wrong direction. You should focus on modern event-driven GUI programming using modern toolkits (Gtk, Qt), modern languages (C++, C#, Java, Python, etc) and OpenGL for "special effects".

ezaquarii
  • 1,832
  • 11
  • 15
1

You need to understand that on Linux graphics is generally done thru X11 (perhaps Wayland could become a competitor in the future).

Then you should use some X11 toolkit. If you want it in C, consider GTK or libSDL. But if you know C++, I would recommend Qt (read about its graphics abilities).

You can find some short Qt or Gtk or SDL example programs, in about a hundred lines.

Java has at least Swing.

Notice that Linux is intrinsically a multi-tasking system. So you want to run several graphical programs. In other words, you want several windows (and a window or desktop manager). So, you need an event loop, and you need to take care of resized and/or overlapping windows. Hence the complexity is much bigger than in the TurboC days of the previous century!

Alternatively, consider making your application a specialized HTTP server (and code the graphics in HTML5), e.g. using libonion as a C HTTP server library.

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479