Java to C++

Can someone please convert this program to C++ please?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

public class Naz extends JFrame {

  //make the screen visible
  public static void main(String[] args) {
    Naz naz = new Naz();
    naz.setSize(600, 400);

  NazPane2 nazPane;

  public Naz() {
    super("Naz"); // window title
    // Handle window close
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {

    // this is the frame
    Container contentPane = this.getContentPane();
    contentPane.setLayout(new BorderLayout());  
    // Create the pane so I can scibble on it.
   // I set the background to be white
    nazPane = new NazPane2();
    nazPane.setBorder(new BevelBorder(BevelBorder.LOWERED));
    contentPane.add(nazPane, BorderLayout.CENTER);
    // this is for the menu bar
    JMenuBar menubar = new JMenuBar(); // Create a menubar
    this.setJMenuBar(menubar); // Display it in the JFrame
    // giving names to click on for the menubar
    JMenu filemenu = new JMenu("File");
    JMenu colormenu = new JMenu("Color");
    // this is for the action object which I found the code from the
    Action clear = new ClearAction();
    Action quit = new QuitAction();
    Action black = new ColorAction(;
    Action red = new ColorAction(;
   Action blue = new ColorAction(;
    Action select = new SelectColorAction();
    // I don't understand this part..but it is for the toolbar
   // filemenu.add(save);
    // This create the toolbar
    JToolBar toolbar = new JToolBar();
   // toolbar.add(save);    
    contentPane.add(toolbar, BorderLayout.NORTH);
    // another toolbar for the leftside
    JToolBar palette = new JToolBar();
    contentPane.add(palette, BorderLayout.WEST);
  //if I want to clear the screen
  class ClearAction extends AbstractAction {
    public ClearAction() {
    public void actionPerformed(ActionEvent e) {
  class QuitAction extends AbstractAction {
    public QuitAction() {
    public void actionPerformed(ActionEvent e) {
      int response = JOptionPane.showConfirmDialog(Naz.this,
          "Really Quit?");
      if (response == JOptionPane.YES_OPTION)
  //for drawing
  class ColorAction extends AbstractAction {
    Color color;
    public ColorAction(Color color) {
      this.color = color;
      putValue(Action.SMALL_ICON, new ColorIcon(color));
    public void actionPerformed(ActionEvent e) {
  //draw with specific color
  static class ColorIcon implements Icon {
    Color color;
   public ColorIcon(Color color) {
      this.color = color;
    // size of the icon
    public int getIconHeight() {
      return 16;
    public int getIconWidth() {
      return 16;
    // draws the icon
    public void paintIcon(Component c, Graphics g, int x, int y) {
      g.fillRect(x, y, 16, 16);
  //choose color
  class SelectColorAction extends AbstractAction {
    public SelectColorAction() {
      super("Select Color...");
    public void actionPerformed(ActionEvent e) {
      Color color = JColorChooser.showDialog(Naz.this,
          "Select Drawing Color", nazPane.getColor());
      if (color != null)
class NazPane2 extends JPanel {
  public NazPane2() {
    // size
    setPreferredSize(new Dimension(450, 200));
    // this is for the mouse
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        moveto(e.getX(), e.getY()); // Move to click position
    // same as above
    addMouseMotionListener(new MouseMotionAdapter() {

      public void mouseDragged(MouseEvent e) {
        lineto(e.getX(), e.getY()); // Draw to mouse position
    // Add a keyboard event handler to clear the screen on key 'C'
    addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_C)
  //for position of the mouse
  protected int last_x, last_y;
  public void moveto(int x, int y) {
    last_x = x;
    last_y = y;  
  public void lineto(int x, int y) {
    Graphics g = getGraphics(); // Get the object to draw with
    g.setColor(color); // Tell it what color to use
    g.drawLine(last_x, last_y, x, y); // Tell it what to draw
    moveto(x, y); // Save the current point
  //clear drawing area and redraw if requested
  public void clear() {
  //keep the drawing color
  Color color =;
 public void setColor(Color color) {
    this.color = color;
  public Color getColor() {
    return color;

Who is Participating?
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
Doesn't solve your problem but interestingly the same look and feel:
Deepu AbrahamConnect With a Mentor R & D Engineering ManagerCommented:
On which OS are you trying this? If you are using windows, try MFC which will be much easier to build the GUI and the message handler.
Best Regards,
Deepu AbrahamR & D Engineering ManagerCommented:
Your Java code is using SWING, so you may need to tweek a lot to make the same look and feel..
You should get a start here:

Best Regards,
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

MacroLandConnect With a Mentor Commented:
I definitely agree with DeepuAbrahamK. Converting this program into MFC will be easier however still you need to do some coding.


P.S: We can be of more help if you ask the parts that you can not handle instead of posting all the Java code.

shahrine99Author Commented:
wat is MFC?
shahrine99Author Commented:
never mind..this is more for like visual c++...visual c++ is the same as  c++ is it?
shahrine99Author Commented:
and yes it is for windows
Visual C++ not same but similar to C++ except that it gives inherent support to MFC (Microsoft Foundation Class)

Deepu AbrahamR & D Engineering ManagerCommented:
For VC++ the underlaying language is c++. Using c++ with certain windows provided libraries makes it " VC++" as Microsoft likes to call it. Normally we can develop windows application using win32 sdk api's. Which is a bit cumbersome task I would say (Once you get used to MFC you will say that !). But MFC, which has got very well wrapped classes for win32 API's, Which makes it easier for development.

You should give it a shot and then come up with some questions if you have.

Best Regards,
If you want something in the same vein, look at the scribble tutorial at,

There are lots of differences. The scribble tutorial is a multi document interface (MDI). However, it is a classic tutorial for Windows programming, so it is probably better to work from this than something closer to your specification.
shahrine99Author Commented:
it has to be related to x windows for linux platform so i can call the window..draw on the window and report mouse movement
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.