Help With MouseListeners


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help With MouseListeners

  1. #1
    Join Date
    Sep 2004
    Posts
    103

    Question Help With MouseListeners

    Okay I'm stumped, I can't figure out how to draw Lines? When I click on one of the poylgons I want to get the Color Of the POlygon, Then I want to be able to draw a line with that Color on the Canvas. I've been stuck for 3 days, Can't figure it out, ANy Help appreciated, thank you.

    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.Color;

    public class polygon extends Canvas implements MouseListener {

    private int [] s = {25,50,75};
    private int [] t = {50,25,50};
    private int [] ss = {100, 125, 150};
    private int [] tt = {50, 25, 50};
    private int [] sss = {175, 200, 225};
    private int [] ttt = {50, 25, 50};

    private Polygon p = new Polygon(s,t,3);
    private Polygon q = new Polygon(ss,tt,3);
    private Polygon r = new Polygon(sss,ttt,3);

    private Button save = new Button();
    private Button open = new Button();

    private int x;
    private int y;
    private int oldx;
    private int oldy;

    private Color c;

    public static void main(String [] args) {
    polygon polygon = new polygon();
    }

    public polygon() {
    Frame theframe = new Frame();
    theframe.setSize(500, 500);
    theframe.add(this,BorderLayout.CENTER);
    theframe.setVisible(true);
    addMouseListener(this);
    }
    public void drawLine(Graphics g, Color c){
    g.drawLine(oldx, oldy,10,10);
    }
    public void paint (Graphics g) {
    g.setColor(Color.blue);
    g.fillPolygon(p);
    g.setColor(Color.RED);
    g.fillPolygon(q);
    g.setColor(Color.GREEN);
    g.fillPolygon(r);
    drawLine(g,c);
    }
    public void mousePressed(MouseEvent e){
    x = e.getX();
    y = e.getY();
    oldx = x;
    oldy = y;
    }
    public void mouseReleased(MouseEvent e){
    x = e.getX();
    y = e.getY();
    }
    public void mouseClicked(MouseEvent e) {
    if (p.contains(e.getX(),e.getY())) {
    c = Color.blue;
    repaint();
    }
    if (q.contains(e.getX(),e.getY())){
    c = Color.red;
    repaint();
    }
    if (r.contains(e.getX(),e.getY())){
    c = Color.green;
    repaint();
    }
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    223
    here ya go this should help ya:


    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.Color;

    public class polygon extends Canvas implements MouseListener {

    private int [] s = {25,50,75};
    private int [] t = {50,25,50};
    private int [] ss = {100, 125, 150};
    private int [] tt = {50, 25, 50};
    private int [] sss = {175, 200, 225};
    private int [] ttt = {50, 25, 50};

    private Polygon p = new Polygon(s,t,3);
    private Polygon q = new Polygon(ss,tt,3);
    private Polygon r = new Polygon(sss,ttt,3);

    private Button save = new Button();
    private Button open = new Button();

    private int x = 0;
    private int y = 0;
    private int oldx = 0;
    private int oldy = 0;

    private Color c;
    private String colour = "BLACK";

    private int clicks =0;

    public static void main(String [] args) {
    polygon polygon = new polygon();
    }

    public polygon() {
    Frame theframe = new Frame();
    theframe.setSize(500, 500);
    theframe.add(this,BorderLayout.CENTER);
    theframe.setVisible(true);
    addMouseListener(this);
    }

    public void paint (Graphics g) {
    g.setColor(Color.blue);
    g.fillPolygon(p);
    g.setColor(Color.RED);
    g.fillPolygon(q);
    g.setColor(Color.GREEN);
    g.fillPolygon(r);

    if(colour.equals("BLUE")){
    g.setColor(Color.BLUE);
    }
    if(colour.equals("RED")){
    g.setColor(Color.RED);
    }
    if(colour.equals("GREEN")){
    g.setColor(Color.GREEN);
    }
    if(((clicks % 2) == 0) && (clicks != 0)){
    g.drawLine(oldx, oldy,x,y);
    }
    }
    public void mousePressed(MouseEvent e){
    oldx = x;
    oldy = y;
    x = e.getX();
    y = e.getY();
    clicks++;
    if((x > 25) && (x < 75) && (y < 50) && (y > 25)){
    colour = "BLUE";
    clicks = 0;
    }
    if((x > 100) && (x < 150) && (y < 50) && (y > 25)){
    colour = "RED";
    clicks = 0;
    }
    if((x > 175) && (x < 225) && (y < 50) && (y > 25)){
    colour = "GREEN";
    clicks = 0;
    }

    repaint();
    }
    public void mouseReleased(MouseEvent e){}
    public void mouseClicked(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    }
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Sep 2004
    Posts
    103

    Thanks for the help Kram

    That code helps out a great deal thank you. I would like to know though that after you draw a line, And go to draw a new line the old line dissapears, Is there any way to keep the lines there on the screen. To keep the lines on the screen? Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center