Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Paint program adding classes to main method class

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Location
    ?????????? XD
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Paint program adding classes to main method class

    Trying to create a simple paint program. I'm trying to make objects from other classes and incorporate them to my method class. So far the program runs on the actions performed by the mouse. It will "draw" whatever the user wants when the mouse is clicked(2nd class). I'm trying to get the 3rd class to be come part of my main method. I was recommended to create different objects, for example; one class for shapes, other for colors. If you run the main method + 2nd class. You should be able to see a menubar with an exit "option", but I want to be able to create a class or just make all the shapes and colors within my mainmethod.

    Which is a better choice? I think different classes will be a more "organized" approach, but I don't know how I can add it to my mainmethod class. Can you show a simple example of how to add a class [ which has the option to create a circle from the menubar]. Or, just make 3rd class work on my main method.

    Hope this helps understand what I'm trying to do. I'd really appreciate your help.
    PHP Code:
    // main method class


    import javax.swing.*;
    import java.awt.BorderLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.event.*;

    public class 
    Painter{

    public static 
    void main (String[] args)
    {

    JFrame application = new JFrame ("Paint Program");


    PaintPanel paintPanel = new PaintPanel ();
    application.add (paintPanelBorderLayout.CENTER);
    application.setDefaultCloseOperationJFrame.EXIT_ON_CLOSE);


        
    JMenuBar menubar = new JMenuBar();
        
    application.setJMenuBar(menubar);
        
    JMenu file = new JMenu("File");
        
    menubar.add(file);
        
    JMenuItem exit = new JMenuItem("Exit");
        
    file.add(exit);

        
    JMenu help = new JMenu("Help");
        
    menubar.add(help);
        
    JMenuItem about = new JMenuItem("About");
        
    help.add(about);

        class 
    exitaction implements ActionListener{
            public 
    void actionPerformed(ActionEvent e){
            
    System.exit(0);
            }
                }

     exit.
    addActionListener(new exitaction() );
     

     
    application.setSize(800,400);
    application.setVisible(true);

    }

    }


    // 2nd class 



    import java.awt.Point;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import javax.swing.JPanel;

    public class 
    PaintPanel extends JPanel {

    private 
    int pointCount 0;

    private 
    Point[] points = new Point[10000];

    public 
    PaintPanel()
    {
    addMouseMotionListener(

    new 
    MouseMotionAdapter()
    {
    public 
    void mouseDragged (MouseEvent event)
    {
    if (
    pointCount <points.length)
    {
    points pointCount ] = event.getPoint();
    pointCount++;
    repaint();
    }
    }
    }
    );


    }

       
    public 
    void paintComponent (Graphics g)
    {

    super.paintComponent (g);

    for (
    int i=0pointCounti++)
    g.fillOval (points [i].xpoints[i].y44);


    }

    }

    // [COLOR="yellow"]3rd class[/COLOR]

    import java.awt.FlowLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JScrollPane;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.ListSelectionModel;

    public class 
    ListFrame extends JFrame {

        private 
    JList colorJList;
        private static final 
    String[] colorNames = {"Black""Blue""Cyan",
        
    "Dark Gray""Gray""Green""Light Gray""Magenta",
        
    "Orange""Pink""Red""White""Yellow"};
        private static final 
    Color[] colors = {Color.BLACKColor.BLUE,
        
    Color.CYANColor.DARK_GRAYColor.GRAYColor.GREEN,
        
    Color.LIGHT_GRAYColor.MAGENTAColor.ORANGEColor.PINK,
        
    Color.REDColor.WHITEColor.YELLOW };

        public 
    ListFrame(){

            
    super("List Test");
            
    setLayout(new FlowLayout() );

            
    colorJList = new JList(colorNames);
            
    colorJList.setVisibleRowCount(5);

            
    colorJList.setSelectionModeListSelectionModel.SINGLE_SELECTION);
            
    add(new JScrollPane(colorJList));

            
    colorJList.addListSelectionListener(
                    new 
    ListSelectionListener(){

                public 
    void valueChangedListSelectionEvent event ){

                    
    getContentPane().setBackground(
                            
    colors[colorJList.getSelectedIndex() ]);
                }

            });

        }


    Last edited by Maxfmc; April 15th, 2011 at 07:28 PM.


Similar Threads

  1. Creating a scaleUp main method in a new class
    By Brainz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2010, 08:58 AM
  2. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM
  3. CashRegister class- adding a method getItemCount
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: January 21st, 2010, 08:29 PM
  4. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  5. adding main method to a code
    By IDK12 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 31st, 2009, 08:52 PM

Tags for this Thread