Link to home
Start Free TrialLog in
Avatar of shahrine99
shahrine99

asked on

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);
    naz.setVisible(true);
  }

  NazPane2 nazPane;

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

    // this is the frame
    Container contentPane = this.getContentPane();
 
    //layout
    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));
    nazPane.setBackground(Color.white);
    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");
    menubar.add(filemenu);
    menubar.add(colormenu);
 
    // this is for the action object which I found the code from the
   //internet
    Action clear = new ClearAction();
    Action quit = new QuitAction();
    Action black = new ColorAction(Color.black);
    Action red = new ColorAction(Color.red);
   Action blue = new ColorAction(Color.blue);
    Action select = new SelectColorAction();
   
    // I don't understand this part..but it is for the toolbar
    filemenu.add(clear);
    filemenu.add(quit);
   // filemenu.add(save);
    colormenu.add(black);
    colormenu.add(red);
    colormenu.add(blue);
    colormenu.add(select);
   
    // This create the toolbar
    JToolBar toolbar = new JToolBar();
    toolbar.add(clear);
    toolbar.add(select);
    toolbar.add(quit);
   // toolbar.add(save);    
    contentPane.add(toolbar, BorderLayout.NORTH);
   
    // another toolbar for the leftside
    JToolBar palette = new JToolBar();
   palette.add(black);
    palette.add(red);
    palette.add(blue);
    palette.setOrientation(SwingConstants.VERTICAL);
    contentPane.add(palette, BorderLayout.WEST);
  }
   
  //if I want to clear the screen
  class ClearAction extends AbstractAction {
    public ClearAction() {
      super("Clear");  
    }
   
    public void actionPerformed(ActionEvent e) {
      nazPane.clear();
    }
  }
 
  class QuitAction extends AbstractAction {
    public QuitAction() {
      super("Quit");  
    }
   
    public void actionPerformed(ActionEvent e) {
     
      int response = JOptionPane.showConfirmDialog(Naz.this,
          "Really Quit?");
      if (response == JOptionPane.YES_OPTION)
        System.exit(0);
    }
  }
     
  //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) {
      nazPane.setColor(color);
    }
  }
     
  //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.setColor(color);
      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)
        nazPane.setColor(color);
    }
  }
}
     
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
        requestFocus();
      }
    });
   
    // 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)
          clear();
      }
    });
  }
       
  //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() {
    repaint();
  }
   
  //keep the drawing color
  Color color = Color.black;
 public void setColor(Color color) {
    this.color = color;
  }
 
   
  public Color getColor() {
    return color;
  }
 
}  


SOLUTION
Avatar of Deepu Abraham
Deepu Abraham
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
http://www.samspublishing.com/library/content.asp?b=Visual_C_PlusPlus&seqNum=60&rl=1

Best Regards,
DeepuAbrahamK
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shahrine99
shahrine99

ASKER

wat is MFC?
never mind..this is more for like visual c++...visual c++ is the same as  c++ is it?
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)

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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,
DeepuAbrahamK
If you want something in the same vein, look at the scribble tutorial at http://msdn.microsoft.com/library/en-us/vctutor98/HTML/_gs_scribble_tutorial.asp,

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.
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