.drawLine()的问题和缓冲的图像(.drawLine() issues and buffer

2019-06-26 22:14发布

我有一个画图程序,我有所有的按钮和滑块做不过我有实际的绘画本身的问题。 当我拖动光标在屏幕上,而不是一个完整的线我得到几乎是虚线,我不想。 下面是该代码MouseListenerJPanelBufferedImage

      public void mouseDragged(MouseEvent e) {
          Graphics g=buffered.getGraphics();
          g.setColor(mycol);
              Graphics2D graph=(Graphics2D)g;
          BasicStroke stroke=new BasicStroke(30);
          graph.setStroke(stroke);
              //  g.fillRect(xcor, ycor, 20, 20);
          /  /varx=e.getX();
            ycor=e.getY();
             xcor=e.getX();
            int bad=xcor;
            int good=ycor;
            graph.drawLine(xcor, ycor, bad, good);
           // buffered.setRGB(xcor, ycor, mycol.getRGB());
            repaint();
            // g.drawLine(xcor, ycor, x, x)
             repaint();


        }

Answer 1:

  • 只是为了证明我的意见,我加入了这个答案,但在征求意见稍有变化就在这里,这是使用mousePressed(...)代替mouseClicked(...)
  • 还有一个另外的存在,因为你希望Graphics2D中的对象BufferedImage因此而不是使用getGraphics()总是使用createGraphics()它返回Graphics2D对象,所以你真的不担心这个的演员啄。

    请你看看下面的例子:

======================

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class PaintingExample {

    private BufferedImage bImage;
    private ImageIcon image;
    private JLabel imageLabel;
    private int xClicked = 0;
    private int yClicked = 0;
    private int xDragged = 0;
    private int yDragged = 0;

    private MouseAdapter mouseListener = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent me) {
            xClicked = me.getX();
            yClicked = me.getY();
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            xDragged = me.getX();
            yDragged = me.getY();

            Graphics2D g2 = bImage.createGraphics();
            g2.setColor(Color.WHITE);
            BasicStroke stroke=new BasicStroke(30);
            g2.setStroke(stroke);
            g2.drawLine(xClicked, yClicked, xDragged, yDragged);
            g2.dispose();
            imageLabel.setIcon(new ImageIcon(bImage));
        }
    };

    public PaintingExample() {
        try {
            bImage = ImageIO.read(new URL(
                    "http://i.imgur.com/fHiBMwI.jpg"));
            image = new ImageIcon(bImage);          
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void displayGUI() {
        JFrame frame = new JFrame("Painting on Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentPane = new JPanel();
        imageLabel = new JLabel(image);
        imageLabel.addMouseListener(mouseListener);
        imageLabel.addMouseMotionListener(mouseListener);

        contentPane.add(imageLabel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PaintingExample().displayGUI();
            }
        });
    }
}


Answer 2:

三个是像素非常宽的线,我可以想像,当无抗锯齿绘制的,它会看起来非常参差不齐的; 这可能是你所看到的。 你可能想尝试一些像

graph.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);

在另一方面,也许你已经得到抗锯齿,并希望将其关闭; 然后

graph.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_OFF);

其中之一是保证改变你的外观形象; 希望这将是根据自己的喜好。



Answer 3:

如果我理解正确你的问题,你将不得不主要问题是拖动鼠标时,您将收到更新的数量。

即使你慢慢拖,你不会总是被通知每一个像素的运动,而不是系统等待“空闲”状态(或阈值)来通知你,“似乎”这是一个平滑的运动。

我可以通过稍微修改你的代码把这个一起

private MouseAdapter mouseListener =
    new MouseAdapter() {
        private boolean paint = false;
        @Override
        public void mousePressed(MouseEvent me) {

            xClicked = me.getX();
            yClicked = me.getY();
            xDragged = xClicked;
            yDragged = yClicked;

            paint = true;

        }

        @Override
        public void mouseReleased(MouseEvent e) {

            xClicked = -1;
            xClicked = -1;
            xDragged = -1;
            yDragged = -1;

            paint = false;

        }

        @Override
        public void mouseMoved(MouseEvent me) {
        }

        @Override
        public void mouseDragged(MouseEvent me) {

            if (paint) {

                xClicked = xDragged;
                yClicked = yDragged;

                xDragged = me.getX();
                yDragged = me.getY();

                xDragged = me.getX();
                yDragged = me.getY();

                Graphics2D g2 = bImage.createGraphics();
                g2.setColor(Color.WHITE);
                g2.drawLine(xClicked, yClicked, xDragged, yDragged);
                g2.dispose();
                imageLabel.setIcon(new ImageIcon(bImage));

                me.getComponent().invalidate();
                me.getComponent().repaint();

            }

        }
    };

基本上,这个想法是借鉴过去的“已知位置”到当前位置的路线。

希望这是在棒球场



文章来源: .drawLine() issues and buffered image