绘制线在java(图形2D) [英] Drawing lines in java (Graphics 2D)

查看:186
本文介绍了绘制线在java(图形2D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Eclipse上做一个小程序。程序如下所示:当我在框架上的面板上单击第一次时,必须绘制一个关于我的鼠标监听器的Y位置的线。线条占据面板的所有宽度。在第二次点击时,必须再次绘制另一行,关于我点击的位置。之后,我会在两条线之间放一个小圆圈,并用它制作一点动画。
但现在我有一个问题。当我点击面板时,绘制一条线,但是如果我单击另一个时间,第一行消失,第二行取而代之...
这是painComponent和我的mousr侦听器的代码。这是怎么回事 ?

  public Lines(){
addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){

posY = e.getY();
posX = e.getX();
nbClic = e.getClickCount();
repaint();
}

});
setBackground(Color.black);
}

@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g;
g2d.setColor(Color.blue);

if(nbClic> = 1){
line1 = new Line2D.Double(0,posY,getWidth(),posY);
g2d.draw(line1);
repaint();
}
if(nbClic> = 2){
g2d.setColor(Color.YELLOW);
line2 = new Line2D.Double(0,posY,getWidth(),posY);
g2d.draw(line2);
}
repaint();
}


解决方案

绘画是一个事件,整个组件。您不能依赖于过去的事件,因为每次重绘发生时都会被删除。



您需要保留像 List 每次创建新行时,都将其添加到列表

 列表与LT;整数> yClicks = new ArrayList<>(); 

... {
addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
yClicks.add(e .getY());
repaint();
}
});
}

@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d =(Graphics2D)g.create(); (int y:yClicks)

{
g2d.draw(new Line2D.Double(0,y,getWidth(),y));
}

g2d.dispose();
}

另外:




  • 不要在 paintComponent 内调用 repaint !这将导致无休止的重绘循环。

  • paintComponent 是一种受保护的方法,除非有令人信服的理由它是公开的。

  • 请注意将 Graphics 对象的状态更改为 paintComponent 因为它在其他地方使用。通常我们创建一个当我们完成后被处置的本地副本。


I am trying to do a little program on Eclipse. The program goes like this: when I click for the 1st time on thr Panel on the frame, a line has to be drawn regarding the Y position of my mouse listener.The line takes all the width of the panel. On the 2nd click, another line has to be drawn, again regarding the Y position of where I clicked. After, I'll put a little circle between the 2 lines and make a little animation with it. But now, I have a problem. When I click on the panel, a line is drawn, but if i click another time, the first line disappears and the 2nd line takes it place... This is the code of the painComponent and my mousr listener. What is wrong with it ?

public Lines() {
    addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {

            posY=e.getY();
            posX=e.getX();
            nbClic=e.getClickCount();
            repaint();
        }

    });
    setBackground(Color.black);
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setColor(Color.blue);

    if(nbClic>=1){
        line1=new Line2D.Double(0, posY, getWidth(), posY);
        g2d.draw(line1);
        repaint();
    }
    if(nbClic>=2){
        g2d.setColor(Color.YELLOW);
        line2=new Line2D.Double(0, posY, getWidth(), posY);
        g2d.draw(line2);
    }
    repaint();
} 

解决方案

Painting is an event that draws the entire component. You can't depend on past events because they are erased each time a repaint happens.

You would need to keep something like a List and each time you create a new line, you add it to the List.

List<Integer> yClicks = new ArrayList<>();

... {
    addMouseListener(new MouseAdapter() {
       @Override
        public void mouseClicked(MouseEvent e) {
            yClicks.add(e.getY());
            repaint();
        }
    });
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D)g.create();

    for(int y : yClicks) {
        g2d.draw(new Line2D.Double(0, y, getWidth(), y));
    }

    g2d.dispose();
}

Also:

  • Never call repaint inside paintComponent! This will cause an endless cycle of repaints.
  • paintComponent is a protected method and should remain so unless there is a compelling reason to make it public.
  • Be careful changing the state of the Graphics object passed in to paintComponent because it is used elsewhere. Usually we create a local copy which is disposed when we are done.

这篇关于绘制线在java(图形2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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