在JFrame上绘图

时间:2016-05-01 23:10:13

标签: java swing

我正在制作一个简单的绘图程序。我想要做的是,我想在jframe上绘制一条线,因为鼠标在jframe上拖动,非常像画笔应用程序。当按下鼠标时,线条开始绘制,当鼠标被释放时,线条结束。同样,如果在JFrame中的任何其他点上按下鼠标,则另一行开始绘制。在单个类中创建程序时,这并不难,但我在不同的包和类中创建程序。我想要做的是,我想将jframe的Mouselistener放在不同的类中,并且在同一个类中,我想编写绘制该jframe的绘制函数。我尝试过不同的东西但似乎没什么用。这就是我到目前为止所做的...我是Java的新手,所以用例子解释会有所帮助。

GUI包 class gui

package Gui;

import Logic.Draw;
import Logic.MenuHandler;
import java.awt.*;
import javax.swing.*;

public class gui{

    public JFrame jf = new JFrame("Paint Brush");
    JPanel jp = new JPanel();
    JMenuBar jmb = new JMenuBar();
    JMenu jm = new JMenu("Options");
    JMenuItem jmi = new JMenuItem("Change Colour");
    JMenuItem jmi2 = new JMenuItem("Exit");


    public void make_gui()
    {
        jf.setSize(500, 500);
        jf.setLocation(370, 150);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jf.add(jmb);
        jf.setJMenuBar(jmb);

        jm.add(jmi);
        jm.add(jmi2);
        jmb.add(jm);

        jmi.setActionCommand("1");
        jmi2.setActionCommand("2");

        MenuHandler obj_mh = new MenuHandler(this);
        jmi.addActionListener(obj_mh);
        jmi2.addActionListener(obj_mh);


        jp.setBackground(Color.WHITE);
        jf.add(jp);

        Draw obj_d = new Draw(this);
        jf.addMouseListener(obj_d);

        jf.setVisible(true);
    }

    public void make_colourChooser()
    {
        JFrame jf2 = new JFrame("Choose Colour");
        JPanel jp2 = new JPanel();
        JButton jb1 = new JButton("Red");
        JButton jb2 = new JButton("Blue");
        JButton jb3 = new JButton("Green");
        JButton jb4 = new JButton("Yellow");
        JButton jb5 = new JButton("Grey");
        JButton jb6 = new JButton("Black");
        JButton jb7 = new JButton("Orange");
        JButton jb8 = new JButton("Cyan");
        JButton jb9 = new JButton("Pink");


        jb1.setSize(200,200);
        jb1.setBackground(Color.red);
        jp2.add(jb1);

        jb2.setSize(200,200);
        jb2.setBackground(Color.BLUE);
        jp2.add(jb2);

        jb3.setSize(200,200);
        jb3.setBackground(Color.GREEN);
        jp2.add(jb3);

        jb4.setSize(200,200);
        jb4.setBackground(Color.YELLOW);
        jp2.add(jb4);

        jb5.setSize(200,200);
        jb5.setBackground(Color.GRAY);
        jp2.add(jb5);

        jb6.setSize(200,200);
        jb6.setBackground(Color.BLACK);
        jp2.add(jb6);

        jb7.setSize(200,200);
        jb7.setBackground(Color.ORANGE);
        jp2.add(jb7);

        jb8.setSize(200,200);
        jb8.setBackground(Color.CYAN);
        jp2.add(jb8);

        jb9.setSize(200,200);
        jb9.setBackground(Color.PINK);
        jp2.add(jb9);

        jf2.setSize(300,300);
        jf2.setLocation(400,150);

        jp2.setLayout(new GridLayout(3,3));

        jf2.add(jp2);
        jf2.setVisible(true);

    }

}

逻辑包 类Draw

package Logic;

import Gui.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Draw implements MouseListener{

    gui obj_g2 = new gui();

     int startpointX, endpointX;
     int startpointY, endpointY;

    public Draw(gui a) {

       obj_g2 = a;        
    }

    public Draw() {

    }

    @Override
    public void mouseClicked(MouseEvent e) {

        startpointX = e.getX();
        startpointY = e.getY();
    }

    @Override
    public void mousePressed(MouseEvent e) {


    }

    @Override
    public void mouseReleased(MouseEvent e) {

        endpointX = e.getX();
        endpointY = e.getY();
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    public void draw(Graphics g)
    {
        g.setColor(Color.BLUE);
        g.drawLine(startpointX, startpointY, endpointX, endpointY);
    }
}

主要课程

package paintbrush;

import Gui.gui;

public class PaintBrush {

    public static void main(String[] args) {

        gui obj = new gui();
        obj.make_gui();
    }

}

0 个答案:

没有答案