0

嗨!

我想在 JFrame 中绘制自由线。我在一个扩展 JPanel 的类中实现了它。它的定义如下所示:

public class Draw extends JPanel implements MouseMotionListener, MouseListener

我向 Draw 类添加了一个 void 方法,在该方法中我还生成了 JFrame 并添加了 Draw 对象。我现在的问题是:我希望能够从菜单中选择线条的颜色:

JMenu colorMenu = new JMenu();
JMenuItem greenChoice = new JMenuItem("GREEN");
greenChoice.addActionListener(this);
colorMenu.add(greenChoice);
JMenuItem redChoice = new JMenuItem("RED");
colorMenu.add(redChoice);
JMenuBar bar = new JMenuBar();
bar.add(colorMenu);

我不知道在哪里实现这段代码!我应该做 2 个类,一个用于绘图,一个用于 JFrame 及其菜单?我应该如何告诉绘图类与我的 JMenu 交互,例如使用我从菜单中选择的颜色?在这里,我在 Drwa 类中生成我的行,默认颜色为蓝色 :(

public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
if(point1!=null && point2!=null){
g2d.setPaint(Color.BLUE);
g2d.setStroke(new BasicStroke(1.5f));
g2d.draw(line2d);
  }
  }

谢谢你!

4

1 回答 1

0

这是我用来说明 Swing 图形的免费手绘课程。根据需要修改它。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

public class DrawingTest implements Runnable {

    private JFrame frame;

    private MyDrawPanel drawPanel;

    private List<Point> points;

    public DrawingTest() {
        points = new ArrayList<Point>();
    }

    @Override
    public void run() {
        frame = new JFrame("Bouncing Vertices");

        drawPanel = new MyDrawPanel(this);
        MyListener alpha = new MyListener(this);
        drawPanel.addMouseMotionListener(alpha);
        drawPanel.addMouseListener(alpha);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(drawPanel);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public JPanel getDrawingPanel() {
        return drawPanel;
    }

    public List<Point> getPoints() {
        return points;
    }

    public void setPoint(int x, int y) {
        points.add(new Point(x, y));
    }

    public void resetPoints() {
        points.clear();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DrawingTest());
    }

    private class MyListener extends MouseInputAdapter {

        private DrawingTest drawingTest;

        public MyListener(DrawingTest drawingTest) {
            this.drawingTest = drawingTest;
        }

        @Override
        public void mouseDragged(MouseEvent event) {
            drawingTest.setPoint(event.getX(), event.getY());
            drawingTest.getDrawingPanel().repaint();
        }

        @Override
        public void mouseReleased(MouseEvent event) {
            drawingTest.resetPoints();
        }

    }

    private class MyDrawPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private DrawingTest drawingTest;

        public MyDrawPanel(DrawingTest drawingTest) {
            this.drawingTest = drawingTest;
        }

        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.BLUE);
            for (int i = 1; i < drawingTest.getPoints().size(); i++) {
                Point p1 = drawingTest.getPoints().get(i - 1);
                Point p2 = drawingTest.getPoints().get(i);
                g.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
        }
    }

}
于 2012-11-01T19:03:49.780 回答