.drawLine()问题和缓冲图像 [英] .drawLine() issues and buffered image
问题描述
我有一个绘画程序,我已完成所有按钮和滑块但是我对实际绘画本身有问题。当我将光标拖过屏幕而不是一条连续线时,我几乎得到了一条我不想要的虚线。这是 JPanel
和 BufferedImage
中 MouseListener
的代码:
I have a paint programme and i have all the buttons and sliders done however i am having a problem with the actual painting itself. When I drag the cursor across the screen instead of an unbroken line I am getting almost a dotted line which i dont want. Here's the code for the MouseListener
in the JPanel
and BufferedImage
:
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();
}
推荐答案
- 为了证明我的评论是正确的,我正在添加这个答案,尽管评论中有一点点
的变化,这是使用
mousePressed(...)
而不是mouseClicked(...)
。 -
一更多的补充,因为你想要
Graphics2D
对象
BufferedImage
所以而不是使用getGraphics()
总是使用
createGraphics()
返回Graphics2D
对象,因此你
实际上不必担心Cast中的东西。- Just to justify my comment, I am adding this answer, though a slight
change from the comment is here, which being the use of
mousePressed(...)
instead ofmouseClicked(...)
. One more addition being, since you wanted the
Graphics2D
object of theBufferedImage
so instead of usinggetGraphics()
always usecreateGraphics()
which returns theGraphics2D
object, hence you don't really have to worry about the Cast thingy in this.请看下面的例子:
======================
======================
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(); } }); } }
这篇关于.drawLine()问题和缓冲图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Just to justify my comment, I am adding this answer, though a slight
change from the comment is here, which being the use of