Solved

Java to C++

Posted on 2007-03-29
11
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 166 total points
ID: 18821520
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
ID: 18821595
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
ID: 18821619
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shahrine99
ID: 18821716
wat is MFC?
0
 

Author Comment

by:shahrine99
ID: 18821720
never mind..this is more for like visual c++...visual c++ is the same as  c++ is it?
0
 

Author Comment

by:shahrine99
ID: 18821722
and yes it is for windows
0
 
LVL 4

Expert Comment

by:MacroLand
ID: 18821842
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:
Mayank S earned 168 total points
ID: 18826127
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
ID: 18828554
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
ID: 18832286
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
ID: 18839061
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPSendRequest with WinINet delays on first call 11 83
jsp login check 12 53
Glassfish admin console not working 1 59
Crystal Reports Licensing Questions 4 63
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

733 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