在Java中的线绘图 [英] line Drawing in java

查看:97
本文介绍了在Java中的线绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码有许多问题

  public class LineEx extends JFrame implements MouseMotionListener,MouseListener {
int X1,Y1,X2,Y2;
public LineEx(){
JLabel image = new JLabel();
JFileChooser chooser = new JFileChooser();

chooser.setCurrentDirectory(new File(。));
int r = chooser.showOpenDialog(new JFrame());
文件文件= chooser.getSelectedFile();
if(r == JFileChooser.APPROVE_OPTION){
try {
BufferedImage bf;
bf = ImageIO.read(file);
ImageIcon icon = new ImageIcon(bf);
image.setIcon(icon);
image.setHorizo​​ntalAlignment(JLabel.CENTER);
$ b $ catch(IOException ex){
Logger.getLogger(LineEx.class.getName()).log(Level.SEVERE,null,ex);
}
}
JScrollPane jsp = new JScrollPane(image);
getContentPane()。add(jsp);
image.addMouseListener(this);
image.addMouseMotionListener(this);
this.pack();
}
public static void main(String ar []){
LineEx line = new LineEx();
line.setVisible(true);
line.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void mouseClicked(MouseEvent e){

}

public void mousePressed(MouseEvent e){
X1 = e.getX();
y1 = e.getY();


public void mouseReleased(MouseEvent e){
JOptionPane.showMessageDialog(rootPane,X1 =+ x1 +Y1 =+ y1);
}

public void mouseEntered(MouseEvent e){
}

public void mouseExited(MouseEvent e){
}
@Override
public void paint(Graphics g){
super.paintComponents(g);
Graphics2D gd =(Graphics2D)g;
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
Line2D line = new Line2D.Double(x1,y1,x2,y2);
gd.draw(line);
}

public void mouseDragged(MouseEvent e){
x2 = e.getX();
y2 = e.getY();
repaint();
}

public void mouseMoved(MouseEvent e){
}
}




  1. MouseEvents没有得到确切的坐标,这意味着每当我画一条线时它都不在其位置上。这背后的原因是什么?

  2. 当滚动条上下移动时,我想沿图像移动一行,我该怎么做?

>

解决方案

  • MouseEvents没有得到确切的坐标,这意味着每当我画一个它不在它的位置上。这背后的原因是什么?



当滚动条上下移动时,我想沿着图像移动线条,我该怎么做? >

您从 JLabel 中获得了正确的坐标,但在的JFrame 。框架坐标从左上角开始,包含窗口标题/边框。



覆盖 paintComponent 方法放在 JLabel 上,它会得到正确的插入和坐标。





$ b

 类ImageComponent继承JComponent 
实现MouseListener,MouseMotionListener {
private final BufferedImage img;
私人点p1,p2;

public ImageComponent(URL url)抛出IOException {
img = ImageIO.read(url);
setPreferredSize(new Dimension(img.getWidth(),img.getHeight()));
addMouseListener(this);
addMouseMotionListener(this);
}
@Override protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img,0,0,img.getWidth(),img.getHeight(),this);
if(p1!= null&& p2!= null)
g.drawLine(p1.x,p1.y,p2.x,p2.y);
}
@Override public void mousePressed(MouseEvent e){
p1 = e.getPoint();
}
@Override public void mouseDragged(MouseEvent e){
mouseReleased(e);
}
@Override public void mouseReleased(MouseEvent e){
p2 = e.getPoint();
repaint();
}
@Override public void mouseMoved(MouseEvent e){}
@Override public void mouseClicked(MouseEvent e){}
@Override public void mouseEntered(MouseEvent e){}
@Override public void mouseExited(MouseEvent e){}
}

测试代码(生成此截图):



  public static void main(String [] args)throws Exception {

最终网址lenna =
新网址(http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png);

final ImageComponent image = new ImageComponent(lenna);

JFrame frame = new JFrame(Test);
frame.add(new JScrollPane(image));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setVisible(true);
}


There are number of problems with this code

public class LineEx extends JFrame implements MouseMotionListener,MouseListener{
    int x1,y1,x2,y2;
    public LineEx(){
        JLabel image=new JLabel("");
        JFileChooser chooser=new JFileChooser();

        chooser.setCurrentDirectory(new File("."));
        int r=chooser.showOpenDialog(new JFrame());
        File file=chooser.getSelectedFile();
        if(r==JFileChooser.APPROVE_OPTION){
            try {
                BufferedImage bf;
                bf = ImageIO.read(file);
                ImageIcon icon=new ImageIcon(bf);
                image.setIcon(icon);
                image.setHorizontalAlignment(JLabel.CENTER);

            } catch (IOException ex) {
                Logger.getLogger(LineEx.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        JScrollPane jsp=new JScrollPane(image);
        getContentPane().add(jsp);
        image.addMouseListener(this);
        image.addMouseMotionListener(this);
        this.pack();
    }
    public static void main(String ar[]){
        LineEx line=new LineEx();
        line.setVisible(true);
        line.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void mouseClicked(MouseEvent e) {

    }

    public void mousePressed(MouseEvent e) {
        x1=e.getX();
        y1=e.getY();
    }

    public void mouseReleased(MouseEvent e) {
        JOptionPane.showMessageDialog(rootPane, "X1="+x1+"  Y1="+y1);
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void paint(Graphics g){
        super.paintComponents(g);
        Graphics2D gd=(Graphics2D)g;
        gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        Line2D line=new Line2D.Double(x1,y1,x2,y2);
        gd.draw(line);
    }

    public void mouseDragged(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();
        repaint();
    }

    public void mouseMoved(MouseEvent e) {
    }
}

  1. MouseEvents are not getting exact co-ordinates that means whenever i draw a line it is not on its position. What is the reason behind this?
  2. I want to move line along the image when scrollbar goes up and down, how can i do that?

解决方案

  • MouseEvents are not getting exact co-ordinates that means whenever i draw a line it is not on its position. What is the reason behind this?
  • I want to move line along the image when scrollbar goes up and down, how can i do that?

You are getting the correct coordinates from the JLabel but paints on the JFrame. And the frame coordinates begins at the top left point and "includes" the window title/border.

Override the paintComponent method on the JLabel and it you will get the correct insets and coordinates.


Example:

class ImageComponent extends JComponent 
        implements MouseListener, MouseMotionListener {
    private final BufferedImage img;
    private Point p1, p2;

    public ImageComponent(URL url) throws IOException {
        img = ImageIO.read(url);
        setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
        addMouseListener(this);
        addMouseMotionListener(this);
    }
    @Override protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), this);
        if (p1 != null && p2 != null)
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
    }
    @Override public void mousePressed(MouseEvent e) {
        p1 = e.getPoint();
    }
    @Override public void mouseDragged(MouseEvent e) {
        mouseReleased(e);
    }
    @Override public void mouseReleased(MouseEvent e) {
        p2 = e.getPoint();
        repaint();
    }
    @Override public void mouseMoved(MouseEvent e) {}
    @Override public void mouseClicked(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
}

Test code (generates this screenshot):

public static void main(String[] args) throws Exception {

    final URL lenna =
        new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png");

    final ImageComponent image = new ImageComponent(lenna);

    JFrame frame = new JFrame("Test");
    frame.add(new JScrollPane(image));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 300);
    frame.setVisible(true);
}

这篇关于在Java中的线绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆