首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >自由线绘制java2d应用程序连接每个新绘图的线条

自由线绘制java2d应用程序连接每个新绘图的线条
EN

Stack Overflow用户
提问于 2013-02-24 16:26:47
回答 1查看 635关注 0票数 1

我正在开发一个应用程序,它可以识别用鼠标绘制的线条并创建音乐。我对java2d并不熟悉,所以我遇到的问题是:

  • 当您绘图,然后再绘制另一个绘图时,前一个绘图的最后一点将连接到新绘图的第一个。我还想不出怎么解决这个问题。下面是密码。
  • 另一个问题是:我想将绘图的每一笔(从mousePressed到mouseReleased)存储到一个类型为ArrayList的形状中,我如何才能进入其中呢?

我想被指向正确的方向,因为我还没有找到有用的信息在网上。谢谢!

代码语言:javascript
运行
复制
public class DrawBoard extends JPanel implements MouseListener,
        MouseMotionListener {

    public JLabel status;
    public Point pstart, pfinish;
    private Shape currentShape = null;
    private ArrayList<Point> points = new ArrayList<Point>();
    private ArrayList<Shape> lines = new ArrayList<Shape>();

    public DrawBoard() {

        Dimension size = getPreferredSize();
        size.setSize(1024, 800); // w, h
        setPreferredSize(size);
        setOpaque(false);
        status = new JLabel("default");
        add(status, BorderLayout.SOUTH);

        addMouseListener(this);
        addMouseMotionListener(this);

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        status.setText(String.format("Clicked at %d,%d", e.getX(), e.getY()));
    }

    // Where the drawing happens
    @Override
    public void mousePressed(MouseEvent e) {
        status.setText("you pressed down the mouse");
        this.pstart = e.getPoint();

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        status.setText("you draged the mouse");
        points.add(e.getPoint());
        repaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        status.setText("you release the mouse click");
        pfinish = e.getPoint();
    }

    // End of where the drawing happens

    @Override
    public void mouseEntered(MouseEvent e) {
        status.setText("you entered the area");
    }

    @Override
    public void mouseExited(MouseEvent e) {
        status.setText("mouse exited the area");
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        // throw new UnsupportedOperationException("Not supported yet.");
    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(5));

        for (int i = 0; i < points.size() - 2; i++) {

            Point p1 = points.get(i);
            Point p2 = points.get(i + 1);

            g2.drawLine(p1.x, p1.y, p2.x, p2.y);
        }

    }
}

我用BufferedImage对它进行了修改,它通常用于绘图,现在唯一可行的方法就是清晰的方法,我尝试过不同的方法,但没有一种方法起作用。我的新代码如下:

代码语言:javascript
运行
复制
  public class DrawBoard extends JPanel implements MouseListener, MouseMotionListener{


public JLabel status;
private JLabel imgLabel;
public Point pstart, pfinish;
private Shape currentShape = null;
private List<Point> points = new ArrayList<Point>();
private List<BufferedImage> lines = new ArrayList<BufferedImage>();

private static final int BI_WIDTH = 1024;
private static final int BI_HEIGHT = 800;

private BufferedImage bImage = new BufferedImage(BI_WIDTH, BI_HEIGHT,
        BufferedImage.TYPE_INT_ARGB);

public DrawBoard(){  

   Graphics2D g2d = bImage.createGraphics();
   g2d.dispose();

    Dimension size = getPreferredSize();
    size.setSize(1024,800); //w, h
    setPreferredSize(size);
    status =  new JLabel("default");
    add(status, BorderLayout.SOUTH);
    addMouseListener(this);
    addMouseMotionListener(this);  


     imgLabel = new JLabel(new ImageIcon(bImage)) {
     @Override
     protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        paintInLabel(g);
     }
  };
     imgLabel.setOpaque(false);
     setOpaque(false);
      add(imgLabel, BorderLayout.CENTER);

}

private void paintInLabel(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  g2d.setColor(Color.BLUE); // this colour is when mouse is pressed
  g2d.setStroke(new BasicStroke(5));
  if (points.size() < 2) {
     return;
  }
  for (int i = 1; i < points.size(); i++) {
     int x1 = points.get(i - 1).x;
     int y1 = points.get(i - 1).y;
     int x2 = points.get(i).x;
     int y2 = points.get(i).y;
     g2d.drawLine(x1, y1, x2, y2);
  }
 }



@Override
public void mouseClicked(MouseEvent e) {
    status.setText(String.format("Clicked at %d,%d", e.getX(), e.getY()));
}


// Where the drawing happens
@Override
public void mousePressed(MouseEvent e) {
    status.setText("you pressed down the mouse");
    this.pstart = e.getPoint();
    points.add(e.getPoint());

}

@Override
public void mouseDragged(MouseEvent e) {
  status.setText("you draged the mouse");
  points.add(e.getPoint());
  imgLabel.repaint();
}

@Override
public void mouseReleased(MouseEvent e) {
    status.setText("you release the mouse click");
    Graphics2D g2d = bImage.createGraphics();
    g2d.setColor(Color.blue); // this is the final colour
    g2d.setStroke(new BasicStroke(5));

     if (points.size() >= 2) {
        for (int i = 1; i < points.size(); i++) {
           int x1 = points.get(i - 1).x;
           int y1 = points.get(i - 1).y;
           int x2 = points.get(i).x;
           int y2 = points.get(i).y;
           g2d.drawLine(x1, y1, x2, y2);
        }
     }
     g2d.dispose();

     points.clear();
     imgLabel.repaint();

}
// End of where the drawing happens

@Override
public void mouseEntered(MouseEvent e) {
   status.setText("you entered the area");
}


@Override
public void mouseExited(MouseEvent e) {
   status.setText("mouse exited the area");
}

@Override
public void mouseMoved(MouseEvent e) {
    //throw new UnsupportedOperationException("Not supported yet.");
}

public void clearDrawBoard() {

    imgLabel.setIcon(null);
}

 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-24 16:30:03

以下是一些解决办法和建议:

  • 不要从单个List<Point>中抽签,因为无法知道一行的结尾和另一行的起始位置。
  • 考虑在BufferedImage完成后将每一行绘制到它,并在JComponent中的paintComponent方法中显示该paintComponent。您可以将其放置在BufferedImage中的mouseReleased(...)方法中。
  • 或者考虑创建一个List<List<Point>>,这样您就可以根据需要遍历每一行。您可以将新的List<Point>添加到mouseReleased(...)方法中的原始列表中。
  • 考虑使用您的List<Shape>并使用可以绘制的Line2D对象填充它。Line2D将添加到mouseReleased(...)方法中的List<Shape>中。
  • 另外:您不应该重写paint(...),而应该重写“`paintComponent(.)”。
  • 另外:为了更安全的编码,不要忘记@重写注释。

例如,请查看我的代码并回答here

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15053802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档