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 4 of 4

Thread: MVC & String

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post MVC & String

    Hello all;
    first of all i've created an Applet like;

    import java.applet.*;
    import java.awt.*;  
    import java.awt.event.*;
     
    public class uc extends Applet
    implements ActionListener, MouseListener {
    int x[]=new int[3], y[]=new int[3], turn = 0;
    boolean reset;
     
    public void init () {      
         addMouseListener (this);
         }
     
    public void paint (Graphics g) {
         if (reset) {
              g.setColor (Color.white);
              g.fillRect (0,0,getSize().width,getSize().height);
              reset = false;
              g.setColor (Color.black);
         }
         else {
              if (turn>0)
              g.drawOval (x[turn-1], y[turn-1],1,1);
              if (turn==3) {
                    triangle (g,x[0],y[0],x[1],y[1],x[2],y[2]);
                    turn = 0;
              }
         }
    }
     
    public void triangle (Graphics g, int x1, int y1,
         int x2, int y2, int x3, int y3) {
         g.drawLine (x1, y1, x2, y2);
         g.drawLine (x2, y2, x3, y3);
         g.drawLine (x3, y3, x1, y1);
    }
     
    public void mouseClicked (MouseEvent evt) {
         x[turn] = evt.getX();
         y[turn] = evt.getY();
         turn++;
         update(getGraphics());
    }
     
     
    public void mousePressed (MouseEvent evt) {}
    public void mouseReleased (MouseEvent evt) {}
    public void mouseEntered (MouseEvent evt) {}
    public void mouseExited (MouseEvent evt) {}
     
    public void update(Graphics g) {
         paint (g);
    }
     
    public void actionPerformed (ActionEvent ae) {
         turn = 0;
         reset = true;
         update(getGraphics());
    }
    }

    and after that teacher asked to make it using by Model View Controller & String; as like a beginer i cant start to work on it because i didnt understand how to start. Can anyone help about it ?
    Best regards..


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MVC & String

    any idea?

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: MVC & String

    First create classes that have no GUI code in them, but that contain the logic behind your current application. Write it in such a way that these classes could be used in a GUI or a console application. Note that you've got some program logic in your paint method where you reset turn, something that should never be done. The paint method (or paintComponent for Swing applications) should be for painting only and should contain no program logic.

    --- Update ---

    Please don't private message folks with questions. Let's keep all questions and answers in the forum so that all might share in the problem and its solution.

    --- Update ---

    You don't seem to be responding, but I'll give you the benefit of the doubt and post your current code and question here:

    Quote Originally Posted by aNGaJe
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;
     
    public class TriangleComponent extends JComponent
    {   private int[] positionX;
        private int[] positionY;
        private int clicks; 
        public TriangleComponent()
        { JFrame f=new JFrame();
            f.getContentPane().add(this);
           f.setSize(640, 480);
          f.setVisible(true);
          clicks = 0;
          MouseListener listener = new MouseTriListener();
          addMouseListener(listener);
          positionX = new int[3];
          positionY = new int[3];
        }
     
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
            Point2D.Double r1 = new Point2D.Double(positionX[0], positionY[0]);
            Point2D.Double r2 = new Point2D.Double(positionX[1], positionY[1]);
            Point2D.Double r3 = new Point2D.Double(positionX[2], positionY[2]);
     
            Line2D.Double post1 = new Line2D.Double(r1, r2);
            Line2D.Double post2 = new Line2D.Double(r2, r3);
            Line2D.Double post3 = new Line2D.Double(r3, r1);
     
            g2.draw(post1);
            g2.draw(post2);
            g2.draw(post3);
        }
     
            class MouseTriListener implements MouseListener
            {
                public void mousePressed (MouseEvent event)
                {
                    positionX[clicks] = event.getX();
                    positionY[clicks] = event.getY();
                    clicks++;
                    if (clicks % 3 ==0)
                    {
                        repaint();
                        clicks = 0;
                    }
                }
     
                public void mouseReleased(MouseEvent event) {}
                public void mouseClicked(MouseEvent event) {}
                public void mouseEntered(MouseEvent event) {}
                public void mouseExited(MouseEvent event) {}
            }
              public static void main(String[] args)
                {
                 new TriangleComponent();
                }
     
     
    }
    what do you think about now ? is it inclulding MVC ? used String as like noth. but.. idk
    Best Regards.


    --- Update ---

    With regards to your question, as far as I understand it, to be MVC compliant, your program must have more than one class, at least one being for the view, at least one for control and at least one for model. You need to break things down.

  4. The Following User Says Thank You to curmudgeon For This Useful Post:

    javabarcode (January 27th, 2013)

  5. #4

    Default Re: MVC & String

    I am beginner of Java and thanks again.
    Java barcode developer

Similar Threads

  1. Replies: 1
    Last Post: April 11th, 2012, 06:03 AM
  2. Drag & drop MVC: deleting an object
    By Alice in forum Object Oriented Programming
    Replies: 0
    Last Post: December 9th, 2010, 11:09 PM
  3. What can go wrong if you replace && with & in the following code:
    By scott01 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2010, 07:47 AM
  4. MVC
    By systech44 in forum Web Frameworks
    Replies: 1
    Last Post: December 9th, 2009, 03:46 AM