Results 1 to 7 of 7
  1. #1
    alsade is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default First GUI exercise

    I am trying to draw a line using a for loop but for some reason i just cant figure out why, what im gettin instead its the last dot(pixel).

    here is my code:

    Java Code:
    package com.mycompany;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MousePanel extends JPanel implements MouseListener{
    	
        int pointX, pointY, oldX, oldY;
    
        public MousePanel(){
            super();
            addMouseListener(this);   
        }
        
        public void mouseClicked(MouseEvent mouse){
        	
      
            // Tell the panel that we need to redraw things.
            	oldX=pointX;
                oldY=pointY;
            // Get the location of the current mouse click.
                pointX = mouse.getX();
                pointY = mouse.getY();
            // Tell the panel that we need to redraw things.
                
                for (int i=0 ; i<50 ; i++)
                {
                	pointX ++;
                	repaint();
                
                }
                System.out.println("x:"+pointX+", y:"+pointY);
     
            }
        
        public void paintComponent(Graphics g){
        	g.fillOval(pointX, pointY, 5, 5);
        }
        
        public void mouseEntered(MouseEvent mouse){ }   
        public void mouseExited(MouseEvent mouse){ }
        public void mousePressed(MouseEvent mouse){ }
        public void mouseReleased(MouseEvent mouse){ }
    
        public static void main(String arg[]){
            JFrame frame = new JFrame("MousePanel");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(640,400);
    
            MousePanel panel = new MousePanel();
            frame.setContentPane(panel);
            frame.setVisible(true);
        }
    }
    the
    Java Code:
    for (int i=0 ; i<50 ; i++)
                {
                	pointX ++;
                	repaint();
                
                }
    its my try to print a line from the x and y of the first mouse click.
    can some please tell me what am i doing wrong?

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: First GUI exercise

    It won't help you to to tell the component fifty times to repaint itself. If you want to get some kind of animation going on you need to investigate how to use a Swing timer.

    If you want to draw a line, then why not use the method... that draws a line? I don't know why you think fillOval() is the way to go.

    Graphics (Java Platform SE 7 )

    Your paintComponent is also missing a call to super.paintComponent() so the panel gets a chance to draw itself:

    Java Code:
    public void paintComponent(Graphics g){
            super.paintComponent(g);
           
            // do your own drawing stuff here
        }
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: First GUI exercise

    The fact that you think calling repaint() 50 times is something you can do tells me you don't know yet about the Event Dispatch Thread. It is worth your while to learn about that sooner rather than later or you are going to be making many more wrong assumptions:

    The Event Dispatch Thread (The Java™ Tutorials > Creating a GUI With JFC/Swing > Concurrency in Swing)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    alsade is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: First GUI exercise

    though im newB in java i do know the drawline function but this task its part of h.w assignment in which im not allowed to use drawline.
    still looking for an explanation to help me understand better what am i missing in my code...thanks.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: First GUI exercise

    Ah, its homework. You have to be honest about such details, don't hide information.

    If you're not going to read the links I gave you and research the keywords I dropped on you, then you're just waiting in stead of looking.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    alsade is offline Member
    Join Date
    Mar 2014
    Posts
    6
    Rep Power
    0

    Default Re: First GUI exercise

    I Dont understand what different does it make if its h.w or not. if i didnt want to learn i could just take one of the endless code examples in the web that draws line.
    im here because i wanna learn and understand better the language, not because im looking for someone to make my h.w and therefor i find ur answer's inappropriate and arrogant.
    thank u for your time,
    good day.

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,098
    Rep Power
    6

    Default Re: First GUI exercise

    Oh dear, another person with a temper. Goodbye hot head.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 5
    Last Post: 03-19-2013, 11:30 AM
  2. Have I done this exercise right?
    By ccie007 in forum New To Java
    Replies: 7
    Last Post: 09-28-2010, 05:54 PM
  3. I/O exercise
    By Feldom in forum New To Java
    Replies: 1
    Last Post: 10-28-2007, 04:48 PM
  4. help with exercise
    By e_as're in forum New To Java
    Replies: 3
    Last Post: 09-25-2007, 10:14 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
  •