无法用Java绘制形状

时间:2016-04-05 10:22:40

标签: java swing

另一个java swing问题。我真的真的搜索了很多小时但我不能让我的代码工作..所以我希望你们能帮助我。我想我有一些基本的理解问题。

我必须扩展现有代码。 我的第一个任务是从mouseclick中画一条线来释放,我成功地实现了它。

现在我应该在RandomDrawer类中实现缺少的代码,通过单击JMenuItem来绘制圆圈。它们应该是随机的,但是现在这一点并不重要,我可以这样做,当我实际绘制时,看看我的形状:D。

我的问题是我根本无法画出任何东西。 我怎么能告诉他在画布上画画? 它在Painter类中工作,当我以与RandomDrawer类相同的方式实现它时,它没有。 有人能帮助我吗?

到目前为止,这是我的代码:

    package plotterpackage;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Line2D;

    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.WindowConstants;
    import javax.swing.border.BevelBorder;

    /

public class Main {
    private JFrame frame;
    private JPanel canvas;
    private JPanel statusBar;
    private JTextField status;

    public Main() {
        initialize();
    }

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    protected void initialize() {
        frame = new JFrame("Main");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setBounds(50,50, 50+640, 50+480);
        frame.setBackground(Color.GREEN);
        frame.setJMenuBar(createMenuBar());

        frame.getContentPane().add(createContent(), BorderLayout.CENTER);
        frame.getContentPane().add(createStatusBar(),BorderLayout.SOUTH);
    }

    public void start() {
        // show the GUI
        frame.setVisible(true);
        status.setText("Application started");
    }

    protected JComponent createContent() {
        canvas = new JPanel();
        canvas.addMouseListener(new Painter());
        canvas.setBackground(new Color(128,218,255));
        canvas.setForeground(Color.RED);
        canvas.setBorder(new BevelBorder(BevelBorder.LOWERED));
        return canvas;
    }

    protected JComponent createStatusBar() {
        FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
        layout.setHgap(5);

        statusBar = new JPanel(layout);
        statusBar.add(new JLabel("Status: "));

        status = new JTextField();
        status.setPreferredSize(new Dimension(400,25));
        status.setEditable(false);
        status.setBorder(new BevelBorder(BevelBorder.RAISED,Color.MAGENTA,Color.LIGHT_GRAY));
        status.getInsets().set(2, 10, 2, 10);
        statusBar.add(status);

        return statusBar;
    }

    protected JMenuBar createMenuBar() {
        JMenuBar mb = new JMenuBar();
        JMenuItem item;
        JMenu menu;
        // Action menu
        menu = new JMenu("Actions");
        mb.add(menu);
        item = new JMenuItem("Draw RandomCircle");
        item.addActionListener(new RandomDrawer());
        menu.add(item);

        item = new JMenuItem("Draw RandomTriangle");
        item.addActionListener(new RandomDrawer());
        menu.add(item);

        menu.addSeparator();
        item = new JMenuItem("Exit");
        item.addActionListener(new AppCloser());
        menu.add(item);

        // Color menu not used so far
        menu = new JMenu("Colors");        
        mb.add(menu);
        // Help menu not used so far
        menu = new JMenu("Help");        
        mb.add(menu);

        return mb;
    }

    class AppCloser implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.printf("application finished, bye-bye... \n");
            frame.setVisible(false);
            frame.dispose();
            System.exit(0);
        }        
    }

    class RandomDrawer extends JPanel implements ActionListener {

        @Override 
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawOval(50, 50, 50, 50);
            //repaint();
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            double x = Math.random();
            double y = Math.random();
            status.setText(String.format("rnd draw x:%.3f y:%.3f",x,y));
            //Graphics2D gg = (Graphics2D) canvas.getGraphics();
            //paintComponent(gg);
            //gg.draw(new Line2D.Double(0, 0, x, y));
            //gg.drawLine(5, 5, 20, 20);
            //gg.paintComponent();
        }


    }


    class Painter extends MouseAdapter {
        Point start, end;
        int startX, startY;

        @Override
        public void mousePressed(MouseEvent e) {
            start = e.getPoint();
            startX = e.getX();
            startY = e.getY();
            status.setText(String.format("Mouse start: " + start));

        }


        @Override
        public void mouseReleased(MouseEvent e) {
            end = e.getPoint();
            status.setText(String.format("Mouse end: " + end));
            Graphics2D gc = (Graphics2D) canvas.getGraphics();
            //gc.drawLine(0, 0, 80, 80);
            gc.drawLine(startX, startY, e.getX(), e.getY());
            //gc.drawOval(startX, startY, e.getX(), e.getY());
            //gc.fillOval(startX, startY, e.getX(), e.getY());
        }        
    }
}

非常感谢。

1 个答案:

答案 0 :(得分:0)

您还需要在画布中包含MouseMotionListener。

 Painter painter = new Painter();
canvas.addMouseListener(painter);
canvas.addMouseMotionListener(painter);

另外,要为您的矩形添加绘制效果,您需要更改Painter类,如下所示: 用此替换您的Painter类并运行应用程序,您可以根据绘图应用程序绘制矩形。了解我在课堂上的变化,如果有任何问题,请告诉我:

class Painter extends MouseAdapter implements MouseMotionListener
  {
    Point start, end;

    Point oldStart, oldEnd;

    int startX, startY, oldX, oldY, oldWidth, oldHeight;

    public void mousePressed(MouseEvent e)
    {
      start = e.getPoint();
      oldX = startX;
      oldY = startY;
      startX = e.getX();
      startY = e.getY();
      status.setText(String.format("Mouse start: " + start));

    }

    public void mouseDragged(MouseEvent e)
    {
      Graphics2D gc = (Graphics2D)canvas.getGraphics();
      end = e.getPoint();
      gc.setColor(new Color(128, 218, 255));
      gc.drawOval(oldX, oldY, oldWidth, oldHeight);
      oldX = startX;
      oldY = startY;
      oldWidth = e.getX();
      oldHeight = e.getY();
      gc.setColor(Color.BLACK);
      gc.drawOval(startX, startY, e.getX(), e.getY());
    }

    public void mouseReleased(MouseEvent e)
    {
      end = e.getPoint();
      status.setText(String.format("Mouse end: " + end));
      Graphics2D gc = (Graphics2D)canvas.getGraphics();
      oldX = 0;
      oldY = 0;
      oldWidth = 0;
      oldHeight = 0;

    }
  }