为什么不在我点击的地方绘制圆圈

时间:2016-03-23 02:57:59

标签: java events mouseevent

所以我试图绘制一个用户点击的圆圈,然后可以通过下方的栏重新调整大小。一切都有效,除了圈子不会画到我想要的地方。有什么建议吗?

这是我的小组

f(x)=1/(x^3), g(x)=1/x^2,h(x)=1/x

这是另一个创建窗口并调用TestClass的重要类;

    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;

    import javax.swing.*;
    import javax.swing.event.*;

public class TestClass extends JFrame {
    private JSlider slide;
    private MainClass myPanel;
    public int x1=0;
    public int y1=0;
    public TestClass(){
        super("The Title");
        myPanel = new MainClass();
        myPanel.setBackground(Color.YELLOW);
        slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
        slide.setMajorTickSpacing(10);
        slide.setPaintTicks(true);

        slide.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        myPanel.checkDiameter(slide.getValue());
                    }
                }
            );
        HandlerClass handler = new HandlerClass();
        slide.addMouseListener(handler);
        add(slide, BorderLayout.SOUTH);
        add(myPanel, BorderLayout.CENTER);


    }

    public int setX1(){
        return x1;
    }

    public int setY1(){
        return y1;
    }

    private class HandlerClass implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent event) {
            // TODO Auto-generated method stub
                    x1=event.getX();

                    y1=event.getY();
                    repaint();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }


    }

}

1 个答案:

答案 0 :(得分:1)

嘿伙计们我找到了让它运转的方法。

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import javax.swing.event.*;

public class TestClass extends JFrame {
    private JSlider slide;
    private MainClass myPanel;
    public static int x1=0,y1=0;
    public TestClass(){
        super("The Title");
        myPanel = new Panel();
        myPanel.setBackground(Color.YELLOW);
        //Allows you to re-size the drawn circle
        slide = new JSlider(SwingConstants.HORIZONTAL, 0, 200, 10);
        slide.setMajorTickSpacing(10);
        slide.setPaintTicks(true);

        slide.addChangeListener(
                new ChangeListener(){
                    public void stateChanged(ChangeEvent e){
                        myPanel.checkDiameter(slide.getValue());
                    }
                }
            );
        //Create a way to handle user mouse events
        HandlerClass handler = new HandlerClass();
        myPanel.addMouseListener(handler);
        add(slide, BorderLayout.SOUTH);
        add(myPanel, BorderLayout.CENTER);


    }



    private class HandlerClass implements MouseListener{

        @Override
        public void mouseClicked(MouseEvent event) {
            // TODO Auto-generated method stub

这里是代码的新内容,此方法获取单击的x和y坐标并将其发送到具有setPosition方法的MainClass对象myPanel;上课时。

myPanel.setPosition(event.getX(),event.getY());
                        repaint(); }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }


    }

}

这是MainClass类,它名为badley,因为它实际上不是主类......     import java.awt。;     import javax.swing。;     import java.awt.event.MouseEvent;     import java.awt.event.MouseListener;

  public class Panel extends JPanel{
        private int x1,y1;
            private int d = 10;

        public void paintComponent(Graphics g){
            super.paintComponent(g);

            g.setColor(Color.CYAN);
            g.fillOval(x1, y1, d, d);
        }

        public void checkDiameter(int newD)
            {
            //New format for if statements
            d = (newD >= 0 ? newD //if 
                    : 10//else
                    );

            repaint();
        }
        public void setPosition(int newX, int newY) {
            this.x1 = newX;
            this.y1 = newY;
            repaint();
        }

        public Dimension getPreferredSize(){
            return new Dimension(200,200);
        }
        public Dimension getMinimumSize(){
            return getPreferredSize();
        }
}