Hi ... I have finally managed to draw and stretch a triangle on screen as well as Increase its size on screen.
I have drawn a Right angle Triangle on screen or so it appears by the constraints set out to draw it.
Then I have enabled dragging only when the user clicks on the Left Cordinates of the BASE.
And stretching any side is possible only when the user clicks on the TOP or the RIght Hand side cordinates of the base.
The problem is that as and when i stretch the triangle or drag the triangle for that matter it redraws itself a number of times i.e. i can c the entire triangle moving on screen and hence it overlaps and redraws itself on screen.
Besides finding the exact cordinates of the triangle to DRAG it i.e. 300,300 for the base is difficult coz as of now thats the way i could work it out.
The code for my program is below and i would appreciate if any one could sort my problems out and tellme how to drag the triangle when the user just clicks within the area of the triangle and stretches it in a way that it does not redraw the entire triangle..
Thanks...
This is my main Class
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics.*;
import java.awt.geom.Point2D;
;
/**
*
* @author Kripa Bhojwani
*/
public class Geometry extends javax.swing.JFrame {
/** Creates new form Geometry */
public Geometry() {
JFrame Geometry = new JFrame("Geometry");
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
formMouseDragged(evt);
}
});
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
pack();
}
private void formMouseDragged(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
TriangleModel tri = new TriangleModel(300,150,450,300,300,300);
View view = new View(tri); // view takes in model
JFrame Geometry = new JFrame("Geometry");
Geometry.setSize(500,500);
Geometry.setContentPane(view);
Geometry.setVisible(true);
Geometry.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// new Geometry().show();
}
// Variables declaration - do not modify
// End of variables declaration
}
This is my triangleModel class --- the Model
/*
* TriangleModel.java
*
* Created on 02 December 2004, 20:27
*/
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.awt.Graphics;
import java.awt.Point;
/**
*
* @author Kripa Bhojwani
*/
public class TriangleModel extends Observable {
private int cx, cy, x1,x2,x3, y1,y2,y3;
private int _transx;
private int _transy;
/** Creates a new instance of TriangleModel */
public TriangleModel(int x1, int y1, int x2, int y2, int x3, int y3) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
setChanged();
notifyObservers();
}
public void setTop(int x1, int y1){
this.y1= y1;
setChanged();
notifyObservers();
}
public void setRight(int x2, int y2){
this.x2 = x2;
setChanged();
notifyObservers();
}
public void setLeft(int x3, int y3){
_transx = x3 - this.x3;
_transy = y3 - this.y3;
this.x3 += _transx;
this.y3 += _transy;
this.y2 += _transy;
this.x2 += _transx;
this.x1 += _transx;
this.y1 += _transy;
setChanged();
notifyObservers();
}
public Point getTop(){
Point p = new Point(x1,y1);
return p;
}
public Point getRight(){
Point p1 = new Point(x2,y2);
return p1;
}
public Point getLeft(){
Point p3 = new Point(x3,y3);
return p3;
}
}
This is the Controller and View
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.event.*;
import java.awt.Graphics2D;
/**
*
* @author Kripa Bhojwani
*/
public class View extends javax.swing.JPanel implements Observer, MouseMotionListener, MouseListener{
private TriangleModel model;
private Polygon triangle;
private boolean pressed = false;
private boolean pressT = false;
private boolean pressR = false;
/** Creates new form View */
public View(TriangleModel model) {
this.model = model;
model.addObserver(this);
addMouseListener(this);
addMouseMotionListener(this);
// initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
setLayout(new java.awt.BorderLayout());
}
public void paintComponent(Graphics gfx) {
Graphics2D g = (Graphics2D) gfx;
// g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
//RenderingHints.VALUE_ANTIALIAS_ON);
// Polygon triangle = new Polygon(3);
//
Point tc = model.getTop();
Point lc = model.getLeft();
Point rc = model.getRight();
// triangle.add(new Point2D.Double(tc.getX(), tc.getY()));
// triangle.add(new Point2D.Double(lc.getX(), lc.getY()));
// triangle.add(new Point2D.Double(rc.getX(), rc.getY()));
Point2D.Double p1 = new Point2D.Double(tc.getX(),tc.getY());
Point2D.Double p2 = new Point2D.Double(lc.getX(),lc.getY());
Point2D.Double p3 = new Point2D.Double(rc.getX(),rc.getY());
Line2D.Double line = new Line2D.Double(p1, p2);
Line2D.Double line1 = new Line2D.Double(p2, p3);
Line2D.Double line2 = new Line2D.Double(p1, p3);
// g.setPaint(Color.BLACK);
// triangle.draw(g);
g.draw(line);
g.draw(line2);
g.draw(line1);
}
public void update(Observable o, Object arg) {
repaint();
}
public void mouseClicked(java.awt.event.MouseEvent e) {
}
public void mouseDragged(java.awt.event.MouseEvent e) {
if(pressed == true){
model.setLeft(e.getX() , e.getY());
}
else if(pressT == true){
model.setTop(e.getX() , e.getY());
}
else if (pressR == true){
model.setRight(e.getX(), e.getY());
}
else {
pressed = false;
pressT= false;
}
}
public void mouseEntered(java.awt.event.MouseEvent e) {
}
public void mouseExited(java.awt.event.MouseEvent e) {
}
public void mouseMoved(java.awt.event.MouseEvent e) {
System.out.println(""+e.getX() + e.getY());
}
public void mousePressed(java.awt.event.MouseEvent e) {
if (model.getLeft().getX()== e.getX() && model.getLeft().getY()== e.getY()){
pressed = true;
}
else if (model.getTop().getX()==e.getX() && model.getTop().getY()==e.getY()){
pressT = true;
}
else if(model.getRight().getX() == e.getX() && model.getRight().getY()==e.getY()){
pressR = true;
}
else {
pressed =false;
pressT = false;
pressR = false;
}
}
public void mouseReleased(java.awt.event.MouseEvent e) {
if(pressed == true){
model.setLeft(e.getX(),e.getY());
}
else if (pressT ==true ){
model.setTop(e.getX(), e.getY());
}
else if(pressR ==true){
model.setTop(e.getX(),e.getY());
}
else{
pressed = false;
pressT = false;
pressR = false;
}
}
// Variables declaration - do not modify
// End of variables declaration
}
Thanks again