drawPolygon不断从开始画线(鼠标pressed)位置电流(的mouseDragged)位置 [英] drawPolygon keeps drawing lines from starting (mousePressed) location to current (mouseDragged) location

查看:281
本文介绍了drawPolygon不断从开始画线(鼠标pressed)位置电流(的mouseDragged)位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想要动态绘制多边形从当我点击鼠标,直到我停止拖动和释放开始。相反的,对于这个问题的目的,一个方形轮廓正在绘制当我点击,拖拽下来,然后右键跨越,再向上的话,整个左边,这是发生了什么: http://imgur.com/t8ZN3Pp

有什么建议?

注:

model.addPolygon()创建了一个多边形的起点,并将其添加到名为多边形一个ArrayList

model.addPolygonPoint()增加了指向被存储在多边形

此产生多边形

通过多边形我烤漆功能迭代油漆

 公共无效鼠标pressed(的MouseEvent E){
    oldX = e.getX();
    oldY = e.getY();
    model.addPolygon(oldX,oldY);
}公共无效的mouseDragged(的MouseEvent E){
    currentX = e.getX();
    currentY = e.getY();
    model.addPolyPoint(currentX,currentY);
    重绘();
}



。然后paintComponent方法:

 为( - 的ListIterator LT;多边形> ITER =
                model.polys.listIterator(model.polys.size());
                iter.has previous();){
            graphics2D.draw(ITER previous()多。);

完整的paintComponent:

 公共无效的paintComponent(图形G){
    super.paintComponent方法(G);
    如果(像== NULL){
        图像=的createImage(的getSize()宽的getSize()的高度。);
        的Graphics2D =(Graphics2D的)image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    }
    g.drawImage(图像,0,0,NULL);    对于(的ListIterator<&息肉GT; ITER =
            model.polys.listIterator(model.polys.size());
            iter.has previous();){
        graphics2D.draw(ITER previous()多。);        }
    }


解决方案

 进口的javax.swing *。
进口java.awt中的*。
进口java.awt.event.MouseAdapter;
进口java.awt.event.MouseEvent中;
进口java.awt.event.MouseMotionAdapter;公共类测试{    私有静态诠释lastX;
    私有静态诠释lastY;    私有静态诠释镆铘;
    私有静态诠释么;    私有静态最后的BasicStroke行程=的新BasicStroke(2.0F);    私有静态最后一点[]外形=新的Point [] {            新点(10,10),
            新点(10,40),
            新的点(60,90)
            新的点(50,50)    };    公共静态无效的主要(最终字串[] args){        最终的JFrame帧=新的JFrame(多边形运动);
        最终的JPanel面板=新JPanel(){
            @覆盖
            公共无效的paintComponent(图形最终G1){
            最终的Graphics2D G =(Graphics2D的)G1;
            g.setColor(Color.RED);
            g.translate(镆铘,MODY);
            g.setStroke(中风);
            的for(int i = 0; I< SHAPE.length;我++){
                g.drawLine(SHAPE [I] .X,SHAPE [I] .Y,SHAPE [第(i + 1)%SHAPE.length] .X,SHAPE [第(i + 1)%SHAPE.length] .Y);
            }
        }
    };
    pane.addMouseMotionListener(新MouseMotionAdapter(){
        @覆盖
        公共无效的mouseDragged(的MouseEvent E){
            镆铘+ = e.getX() - lastX;
            么+ = e.getY() - lastY;
            lastX = e.getX();
            lastY = e.getY();
            frame.repaint();
        }
    });
    pane.addMouseListener(新MouseAdapter(){
        @覆盖
        公共无效鼠标pressed(的MouseEvent E){
            lastX = e.getX();
            lastY = e.getY();
        }
    });        pane.set preferredSize(新尺寸(200,200));
        frame.add(面板);
        frame.pack();
        frame.setVisible(真);    }}

正如你所看到的,我用做定义点的基本形状。它是这样做的最有效的办法,除非你想改变基本形状(这里是静态的)。在这种情况下,你会发现一点上被​​截获鼠标和修改的那一个。无论哪种方式,没有添加或点的除去是必要的。我使用术语 lastX 而不是 oldX 的只是preference。

本的BasicStroke相当可选,同样拥有铸造到的Graphics2D 对象。

行:

  g.drawLine(SHAPE [I] .X,SHAPE [I] .Y,SHAPE [第(i + 1)%SHAPE.length] .X,SHAPE [(I + 1)%SHAPE.length] .Y);

应该做一些有意义,如果你已经尝试过这个事情。它通过所有的点遍历,画了一条线的电流( SHAPE [I] )到下( SHAPE [第(i + 1)及;。SHAPE.length

这是逻辑背后的原因,是说你有4点,我们在这里做。通过他们的最后一次迭代中,您将获得 I = 3 。由于这一点,只包含4个指标(0-3)阵,我们必须得到该值回落到零。为简单起见我使用%SHAPE.length 所以不会是一个需要特殊情况。

我也选择了使用适配器看到,因为当时只有2所需要的7个可能的方法。

如果您有任何问题随时问我这个问题。

〜传说

So, I'm trying to dynamically draw a Polygon starting from when I click the mouse until I stop dragging and release. Instead of, for the purpose of this question, a square outline being drawn when I click, drag down, then right-across, then up, then left-across, this is what happens: http://imgur.com/t8ZN3Pp

Any suggestions?

Notes:

model.addPolygon() creates a Polygon with starting points and adds it to an ArrayList called 'polys'

model.addPolygonPoint() adds points to this created polygon that is stored in 'polys'

my paint function iterates through polys to paint

public void mousePressed(MouseEvent e) {                
    oldX = e.getX();
    oldY = e.getY();
    model.addPolygon(oldX, oldY);     
}

public void mouseDragged(MouseEvent e) {
    currentX = e.getX();
    currentY = e.getY();
    model.addPolyPoint(currentX, currentY);
    repaint();
}

. . . then in paintComponent:

   for (ListIterator<Polys> iter = 
                model.polys.listIterator(model.polys.size()); 
                iter.hasPrevious();){
            graphics2D.draw(iter.previous().poly);

Full paintComponent:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image == null) {
        image = createImage(getSize().width, getSize().height);
        graphics2D = (Graphics2D) image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    }
    g.drawImage(image, 0, 0, null);

    for (ListIterator<Polys> iter = 
            model.polys.listIterator(model.polys.size()); 
            iter.hasPrevious();){
        graphics2D.draw(iter.previous().poly);

        }   
    }

解决方案

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class Testing {

    private static int lastX;
    private static int lastY;

    private static int modX;
    private static int modY;

    private static final BasicStroke STROKE = new BasicStroke(2.0F);

    private static final Point[] SHAPE = new Point[]{

            new Point(10, 10),
            new Point(10, 40),
            new Point(60, 90),
            new Point(50, 50)

    };

    public static void main(final String[] args) {

        final JFrame frame = new JFrame("Polygon Movement");
        final JPanel pane = new JPanel() {
            @Override
            public void paintComponent(final Graphics g1) {
            final Graphics2D g = (Graphics2D) g1;
            g.setColor(Color.RED);
            g.translate(modX, modY);
            g.setStroke(STROKE);
            for (int i = 0; i < SHAPE.length; i++) {
                g.drawLine(SHAPE[i].x, SHAPE[i].y, SHAPE[(i + 1) % SHAPE.length].x, SHAPE[(i + 1) % SHAPE.length].y);
            }
        }
    };
    pane.addMouseMotionListener(new MouseMotionAdapter() {
        @Override
        public void mouseDragged(MouseEvent e) {
            modX += e.getX() - lastX;
            modY += e.getY() - lastY;
            lastX = e.getX();
            lastY = e.getY();
            frame.repaint();
        }
    });
    pane.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            lastX = e.getX();
            lastY = e.getY();
        }
    });

        pane.setPreferredSize(new Dimension(200, 200));
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);

    }

}

As you can see, I make a basic shape with defined points. It is the most effective way to do this, unless you wish to change the basic shape (here it is static). In that case, you find the point the mouse it 'grabbing' and modify that one. Either way, no adding or removing of points is needed. I use the terms lastX instead of oldX just by preference.

The BasicStroke is quite optional, same with casting to a Graphics2D object.

The line:

g.drawLine(SHAPE[i].x, SHAPE[i].y, SHAPE[(i + 1) % SHAPE.length].x, SHAPE[(i + 1) % SHAPE.length].y);

Should make some sense if you've tried this thing before. It iterates through all the points, drawing a line from the current (SHAPE[i]) to the next (SHAPE[(i + 1) & SHAPE.length).

The reason behind that logic, is that say you have 4 points, as we do here. The last iteration through them, you will be given i = 3. Due to this and the array only containing 4 indexes (0-3), we must get that value back down to zero. For simplicity I use the % SHAPE.length so there wouldn't be a need for special cases.

I also opted to use adapters seeing as there were only 2 methods needed of the 7 possible ones.

If you have any questions feel free to ask me about this.

~Legend

这篇关于drawPolygon不断从开始画线(鼠标pressed)位置电流(的mouseDragged)位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆