Results 1 to 2 of 2
  1. #1
    mootxico is offline Member
    Join Date
    Mar 2009
    Posts
    1
    Rep Power
    0

    Default Java beginner, need some help on a simple tutorial? :D

    Hi there,

    I just started taking a Java course this semester, and I had some trouble in doing this tutorial.

    Here's the question:
    hxxp://img150.imageshack.us/img150/7483/ss091.jpg

    I managed to do the interface as the question wanted, but I'm kinda clueless as to how one makes a new pop-up that draws the pattern in the desired color. I know how to perform the drawing part, and I think I get the idea how it's done (just need to find a way to somehow link the input to the coordinates in the drawing).

    Here's what I came out with (messy, yes, but do pardon me and my fledging 3-weeks worth of knowledge in the language):

    The main class (or is it called function?):
    Java Code:
    package tutorial4;
    
    public class Main {
        
        /** Creates a new instance of Main */
        public Main() {
        }
        
        public static void main(String[] args) 
        {
            ColorWindow cw = new ColorWindow (); 
            
            
        }
        
    }

    the class function:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JApplet;
            
    public class ColorWindow extends JFrame
    {
        private JLabel sizeLabel, thicknessLabel, xpositionLabel, ypositionLabel;
        
        private JTextField sizeField, thicknessField, xpositionField, ypositionField;
        
        
        private JButton cyanButton;
        private JButton magentaButton;
        private JButton yellowButton;
        private JButton redButton;
        private JButton greenButton;
        private JButton blueButton;
        private JPanel panel; 
        private final int WINDOW_WIDTH= 600;
        private final int WINDOW_HEIGHT= 100;
    
           
        public ColorWindow() {
            setTitle ("Plus Designer");
            setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
            setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            
            sizeLabel = new JLabel ("size:");
            sizeField = new JTextField(6);
            
            thicknessLabel = new JLabel ("Thickness:");
            thicknessField = new JTextField(6);
            
            xpositionLabel = new JLabel ("X-position:");
            xpositionField = new JTextField(6);
            
            ypositionLabel = new JLabel ("Y-position:");
            ypositionField = new JTextField(6);
        
         
            
            
            cyanButton = new JButton ("Cyan");
            magentaButton = new JButton ("Magenta");
            yellowButton = new JButton ("Yellow");
            redButton = new JButton ("Red");
            greenButton = new JButton ("Green");
            blueButton = new JButton ("Blue");
        
       
            
            
           cyanButton.addActionListener(new CyanButtonListener());
    /*        magentaButton.addActionListener(new MagentaButtonListener());
            yellowButton.addActionListener(new YellowButtonListener());
            redButton.addActionListener(new RedButtonListener());
            greenmagentaButton.addActionListener(new GreenButtonListener());
            blueButton.addActionListener(new BlueButtonListener());
      
     */  
       
          
            panel = new JPanel (); 
            
            panel.add(sizeLabel);
            panel.add(sizeField);
            
            panel.add(thicknessLabel);
            panel.add(thicknessField);
            
            panel.add(xpositionLabel);
            panel.add(xpositionField );
            
            panel.add(ypositionLabel );
            panel.add(ypositionField );  
            
            panel.add(cyanButton);
            panel.add(magentaButton);
            panel.add(yellowButton);
            panel.add(redButton);
            panel.add(greenButton);
            panel.add(blueButton);
            
            
    
            
            add (panel);
            setVisible (true); 
        }
        
        
            
                        private class CyanButtonListener implements ActionListener 
                        { 
                            public void actionPerformed(ActionEvent e) 
                            { 
                              //JOptionPane.showMessageDialog(null, "hi");
    
                                 Draw draw = new Draw();
    
                            }
    
                        }
    
                
    public class Draw extends JApplet 
    {
    public void paint (Graphics page)
            {
              //  getContentPane().setBackground(Color.white);
                 setBackground (Color.cyan);
                 page.fillRect (0, 175, 300, 50);  
                
            
            }
            }
            
    }      //end
    Some help please? Thanks for your time :D

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I would first of all try to tie the color Strings with the Color by either using an enum or a small class that holds a Color and its String. I'd then create an array of these guys, the reason being, then you can create arrays of buttons and simplify your coding by using a single for loop to create all your buttons and attach a single action listener.

    Next, I'd look at the Sun tutorial on How to Make Frames and on How to Make Dialogs (my preference in this situation), and use this information to guide you in creating your new window.

    After reading these, why not give it a try yourself, then come on back if you are still having difficulties.

    Best of luck.

Similar Threads

  1. Java Beginner needs help!!
    By Polyy in forum New To Java
    Replies: 4
    Last Post: 11-23-2008, 03:11 AM
  2. Best Tutorial for Java GUI ?
    By Ms.Ranjan in forum New To Java
    Replies: 1
    Last Post: 09-13-2008, 02:22 AM
  3. Java 5 Tutorial
    By Ganeshag777 in forum New To Java
    Replies: 1
    Last Post: 08-18-2008, 07:38 PM
  4. Replies: 2
    Last Post: 05-04-2008, 06:27 PM
  5. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 10:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •