Solved

Java to C++

Posted on 2007-03-29
11
288 Views
Last Modified: 2010-04-01
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;
  }
 
}  


0
Comment
Question by:shahrine99
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 166 total points
Comment Utility
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,
DeepuAbrahamK
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
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
0
 
LVL 4

Assisted Solution

by:MacroLand
MacroLand earned 166 total points
Comment Utility
I definitely agree with DeepuAbrahamK. Converting this program into MFC will be easier however still you need to do some coding.

Regards,

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.

0
 

Author Comment

by:shahrine99
Comment Utility
wat is MFC?
0
 

Author Comment

by:shahrine99
Comment Utility
never mind..this is more for like visual c++...visual c++ is the same as  c++ is it?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:shahrine99
Comment Utility
and yes it is for windows
0
 
LVL 4

Expert Comment

by:MacroLand
Comment Utility
Visual C++ not same but similar to C++ except that it gives inherent support to MFC (Microsoft Foundation Class)

0
 
LVL 30

Accepted Solution

by:
mayankeagle earned 168 total points
Comment Utility
Doesn't solve your problem but interestingly the same look and feel:

http://www.codeproject.com/miscctrl/swing.asp
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
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
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
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.
0
 

Author Comment

by:shahrine99
Comment Utility
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
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now