2

我的程序涉及在我单击它们的地方绘制三角形。

有两个类,Ecad 和 Line 类。Ecad 是主框架,Line 类用于绘制线条。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Ecad extends JFrame implements MouseListener{
    ArrayList<Line2> lines=new ArrayList();
    public Ecad(){
        this.setVisible(true);
        this.setSize(600,400);
        this.addMouseListener(this);
    }
    public void mouseReleased(MouseEvent me){
        Point p1,p2,p3;
        int X=me.getX();
        int Y=me.getY();
        p1=new Point(X,Y);                                                       
        p2=new Point((int)(p1.getX()-100),(int)(p1.getY()+(1.732/2*200)));
        p3=new Point((int)(p1.getX()+100),(int)(p1.getY()+(1.732/2*200)));                                          
        Line2 l1=new Line2(p2,p1);
        Line2 l2=new Line2(p1,p3);
        Line2 l3=new Line2(p2,p3);
        lines.add(l1);
        lines.add(l2);
        lines.add(l3);
        this.repaint();
    }
     public void mouseClicked(MouseEvent me){

    }
    public void mouseExited(MouseEvent me){

    }
    public void mouseEntered(MouseEvent me){

    }
    public void mousePressed(MouseEvent me){
    }
    public void mouseMoved(MouseEvent me){

    }
    public static void main(String args[]){
        new Ecad();
    }
    public void paint(Graphics g){
        Graphics2D g2=(Graphics2D)g;
        super.paintComponents(g2);
        //g2.scale(0.5, 0.5);
        for(final Line2 r:lines){
            r.paint((Graphics2D)g2);
        }
    }
}

这是线类

import java.awt.*;
public class Line2 {
    Point start,end;
    public Line2(Point a,Point b){    
        start=a;
        end=b;   
    }
    public void paint(Graphics2D g){
        g.drawLine((int)start.getX(),(int)start.getY(),(int)end.getX(),(int)end.getY());
    }

}

在 Ecad 类的 paint() 方法中,如果我使用 scale 选项来放大或缩小,鼠标坐标不会被转换。所以在它被放大之后,如果我点击一个点,三角形就会被放置在另一个点上。当我缩放图形组件时,有没有办法转换鼠标坐标?

4

1 回答 1

2

同样,您应该使用比例尺和一个固定点来转换您正在绘制的形状(这里再次显示每个形状的固定点将是三角形的顶点,但如果您如此决定,它可能是中心。平移基于简单的几何原理,是 fixedPoint.x * (1 - scale) / scale,y 平移也是如此。

例如(这个使用 JPanel 作为您上面的示例应该):

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;

class EcadB extends JPanel {
   private static final int PREF_W = 800;
   private static final int PREF_H = 600;
   private List<MyShape> myShapes = new ArrayList<MyShape>();
   private double scale = 1.0;
   private JSlider slider = new JSlider(0, 200, 100);

   public EcadB() {
      addMouseListener(new MyMouseAdapter());
      setLayout(new BorderLayout());

      slider.setOpaque(false);
      slider.setMajorTickSpacing(20);
      slider.setMinorTickSpacing(10);
      slider.setPaintLabels(true);
      slider.setPaintTicks(true);
      slider.addChangeListener(new SliderChangeListener());
      add(slider, BorderLayout.SOUTH);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
      Graphics2D g2b = (Graphics2D)g2.create();
      g2b.scale(scale, scale);
      for (MyShape myShape : myShapes) {
         myShape.draw(g2b, scale);
      }
      g2b.dispose();
   }

   public void setScale(double scale) {
      this.scale = scale;
      repaint();
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         Point2D p1 = e.getPoint();
         Point2D p2 = new Point((int) (p1.getX() - 100),
               (int) (p1.getY() + (1.732 / 2 * 200)));
         Point2D p3 = new Point((int) (p1.getX() + 100),
               (int) (p1.getY() + (1.732 / 2 * 200)));
         Path2D path = new Path2D.Double();
         path.moveTo(p1.getX(), p1.getY());
         path.lineTo(p2.getX(), p2.getY());
         path.lineTo(p3.getX(), p3.getY());
         path.lineTo(p1.getX(), p1.getY());

         myShapes.add(new MyShape(path, p1));
         repaint();
      }
   }

   private class SliderChangeListener implements ChangeListener {
      @Override
      public void stateChanged(ChangeEvent arg0) {
         double value = slider.getValue() / 100.0;
         setScale(value);
      }
   }

   public static void main(String args[]) {
      // new Ecad();
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            EcadB ecadB = new EcadB();
            JFrame frame = new JFrame("Scale");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(ecadB);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
         }
      });
   }
}

class MyShape {
   Shape shape;
   Point2D fixedPoint;

   public MyShape(Shape shape, Point2D fixedPoint) {
      this.shape = shape;
      this.fixedPoint = fixedPoint;
   }

   public void draw(Graphics2D g2, double scale) {
      Graphics2D g2b = (Graphics2D) g2.create();
      double tx = fixedPoint.getX() * (1.0 - scale) / scale;
      double ty = fixedPoint.getY() * (1.0 - scale) / scale;
      g2b.translate(tx, ty);
      g2b.draw(shape);
      g2b.dispose();
   }
}

请注意,我在转换我的 Graphics 对象之前制作了它们的副本,以免对可能由 Graphics 对象绘制的其他对象产生转换效果。例如,如果您摆脱了我在 JPanelpaintComponent(...)方法中使用的 Graphics2D 副本,您会发现 JSlider 与其他所有内容一起缩放。

于 2012-04-10T21:51:01.000 回答