绘制线在java(图形2D) [英] Drawing lines in java (Graphics 2D)
问题描述
但现在我有一个问题。当我点击面板时,绘制一条线,但是如果我单击另一个时间,第一行消失,第二行取而代之...
这是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
insidepaintComponent
! 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 topaintComponent
because it is used elsewhere. Usually we create a local copy which is disposed when we are done.
这篇关于绘制线在java(图形2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!