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?
No comments:
Post a Comment