如何用鼠标绘制多边形(三角形和五边形)?

时间:2015-12-19 01:14:22

标签: java awt polygon

如果我想使用鼠标绘制多边形,使用getX()和getY(),我如何找到xpoints []和ypoints []?

目前我的代码是:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Poligonos extends Figura{
    public void Poligonos (int[] xPoints, int[] yPoints, int nPoints){
        //private int[] xPoints = {(x1/2), x1, (x1+(x1/2))}  // {(getX()/2), getX(), (getX()+(getX()/2))};
        //private int[] yPoints = {( y1 + y1 ), y1 ,( y1 + y1 )};

    }
    @Override   
    public void desenha(Graphics g) {
        g.setColor(cor);
        g.drawPolygon(  xPoints, yPoints, 3);
    }
    @Override
    public void setCoordenadas(int x1, int y1, int x2, int y2) {
        p.x = Math.min(x1, x2);
        p.y = Math.min(y1, y2);

        int xPoints[] = {(p.x /2), p.x , ( p.x +( p.x /2))};  // {(getX()/2), getX(), (getX()+(getX()/2))};
        int yPoints[] = {( p.y + p.y ), p.y ,( p.y + p.y )};
    }
}

getX()和getY()部分是:

    @Override
    public void mousePressed(MouseEvent e) {
           x1 = e.getX();
           y1 = e.getY();
       }

       @Override
       public void mouseDragged(MouseEvent e) {
           x2 = e.getX();
           y2 = e.getY();
           r.setCoordenadas(x1, y1, x2, y2);
           pEdicao.repaint();
       }

我该如何使这项工作?我只想用鼠标绘制五边形和三角形。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

您可以进行设置,例如

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.reloadData()
    listRecordings()
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")!
    cell.textLabel!.text = recordings[indexPath.row].lastPathComponent
    return cell
}

func listRecordings() {

    let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    do {
        let urls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsDirectory, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles)
        self.recordings = urls.filter( { (name: NSURL) -> Bool in
            return name.lastPathComponent!.hasSuffix("caf")
        })

    } catch let error as NSError {
        print(error.localizedDescription)
    } catch {
        print("something went wrong")
    } 
}