2012-08-09 73 views
2

我有一个绘画程序,我已经完成了所有按钮和滑块,但是我对实际绘画本身有问题。当我将光标拖过屏幕而不是一条完整的线时,我几乎得到了一条我不想要的虚线。下面是在JPanelBufferedImageMouseListener代码:.drawLine()问题和缓冲图像

 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(); 


     } 
+1

我看来,你提供了错误的参数给'的drawLine()'方法,因为值x1,y1是一样X2,Y2。因此,点就是你的情况,你应该做的是'mouseClick()'存储x1,y1,然后拖动get x2,y2。然后通过这两个'drawLine(xClicked,yClicked,xDragged,yDragged)',这将使该行出现在你的情况:-) – 2012-08-09 18:33:13

回答

2

三十像素是一个非常宽的线,我可以想像,当无抗锯齿绘制的,它要寻找非常锯齿状;这可能是你所看到的。你可能想尝试像

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

在另一方面,也许你已经越来越抗锯齿,并希望将其关闭;那么

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

其中一个保证会改变图像的外观;希望它会更符合你的喜好。

5
  • 只是为了证明我的意见,我加入了这个答案,但在征求意见略有 变化是在这里,这是使用 mousePressed(...)而不是mouseClicked(...)
  • 还有一个另外的存在,因为你想要的 的BufferedImageGraphics2D对象,而不是使用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(); 
      } 
     }); 
    } 
} 
+0

谢谢你解决了我的问题,并保存我再次写出数百行代码。 – 2012-08-11 22:25:33

+0

你最欢迎并保持微笑:-) – 2012-08-12 01:35:21

2

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

即使您拖慢速度,也不会总是收到每个像素移动的通知,而是系统会等待“空闲”状态(或阈值)通知您,以便它“看起来”是平滑移动。

我能够通过修改代码来把这个稍微一起

Hello ;)

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(); 

      } 

     } 
    }; 

基本上,这个想法是从最后一个“已知位置”到当前位置画一条线。

希望这是在棒球场