想做一个类似画图板的.java,当点击circle,可以在panel画一个圆,现在遇到两个问题

用mvc设计模式,将elements为父类,圆,直线等作为其子类,各自有不同的draw()的方法;
control是响应鼠标及按钮监听事件的类;
Gui是界面,可以点击按钮,点击panel一点,拖动,另一点释放,实现作图

代码如下:以画直线和画矩形为例:
在panel做出的图形无法显示,不知道该怎么实现,新建一个mypic类,让它继承JPanel,但是没有出现@over
在line里设置了输出"你画了一条直线",为了检测是否实现mouse event. 点第一次“line", 出现"你画了一条直线";
再点一次rectangle,会先出现"你画了一条直线"并且出现"你画了个矩形"
如果再点一次rectangle,会出现"你画了一条直线""你画了个矩形""你画了个矩形"
每一次都会重复再做一次,之前所有按钮监听的事件~

package elements;
import java.awt.Graphics;
public class Line extends Elements {
    public int x1;
    public int y1;
    public int x2;
    public int y2;
    Graphics g;   
    public Line(int x1, int y1, int x2, int y2)
    {
        super(x1, y1, x2, y2);
        this.x1 = x1; 
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    } 
    @Override
    public void draw(Graphics g) {        
        System.out.print("你画了一条直线");        
        g.drawLine( x1, x2, y1, y2);
    }
}


package control; 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JPanel;
import elements.Elements;
import elements.Line;
import elements.Rectangle;

public class Control extends JPanel implements ActionListener, MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;
    public Graphics g;    
    public JButton curcolor;
    public Color color;

    public void getGraphics(Graphics g) 
    {
        this.g = g;
    }

    public void setG(Graphics g)
    {
        this.g= g;
    }//传递画笔


    public void setcurcolor(JButton curcolor)
    {                
        this.curcolor = curcolor;
    }

    public void setcolor(Color color)
    {
        this.color = color;
    }

    public String doit = "";
    public int x1, y1, x2, y2; //鼠标的按下和释放时的坐标

    ArrayList <Elements> drawX = new ArrayList <Elements>();

    public void add(Elements picture)
    {
        drawX.add(picture);
    }  

    public ArrayList<Elements> getDrawX() {
        return drawX;
    }


    public void setDrawX(ArrayList<Elements> drawX) {
        this.drawX = drawX;
    }

    @Override
    public void actionPerformed(ActionEvent e) {           
        if (e.getActionCommand().equals(""))
        {
                JButton jb = (JButton) e.getSource();
                color = jb.getBackground();    
            //    System.out.println("red");
        }
        else
        {
            doit = e.getActionCommand();               
        }           
    }        

    @Override
    public void mouseMoved(MouseEvent e) {

    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }
    @Override
    public void mousePressed(MouseEvent e) {
        x1 = e.getX();        
        y1 = e.getY(); 
        g.setColor(color);

    }
    @Override
    public void mouseReleased(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();


        if ( doit.equals("rectangle"))
        {
            drawX.add(new Rectangle(x1, y1, x2, y2));
       //     doit = "";           
        }      
        if ( doit.equals("line"))
        {
            drawX.add(new Line(x1, y1, x2, y2));
       //     doit = "";          
        }  
        for (Elements sth : drawX)
        {
            sth.draw(g);
        }        
   }
    @Override
    public void mouseEntered(MouseEvent e) {  }
    @Override
    public void mouseExited(MouseEvent e) { }        
    @Override
    public void mouseDragged(MouseEvent e) {        
    } 
}

package elements;

import java.awt.*;
import java.awt.event.*;

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

public abstract class Elements extends JPanel {

    private static final long serialVersionUID = 1L;

    public Elements(int x1, int y1, int x2, int y2) {
        // TODO Auto-generated constructor stub
    }


    public void draw(Graphics g) {


    }    
}

package view;

import java.awt.*;
import javax.swing.*;

import control.Control;
import elements.Elements;

import java.awt.event.*;
import java.util.ArrayList;

public class Gui extends JFrame {
public Control drawit;
Graphics g;

private static final long serialVersionUID = 1L;


public Gui()
{
    JFrame frame = new JFrame("miniCAD");
    JPanel panelBig = new JPanel();//画板    
    JPanel panelSmall  = new JPanel();//图形和颜色板

    JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelBig, panelSmall);          
    panelBig.setBackground(Color.white);
    panelSmall.setBackground(Color.gray);
    pane.setRightComponent(panelSmall);
    pane.setLeftComponent(panelBig);   
    pane.setContinuousLayout(true);
    pane.setOneTouchExpandable(true);
    frame.add(pane);

// Container C = frame.getContentPane();
// C.add(pane);

//添加图形

JButton buttoncircle = new JButton("circle");
JButton buttonline = new JButton("line");
JButton buttonrectangle = new JButton("rectangle");
JButton buttontext = new JButton("text");
JButton buttontriangle = new JButton("triangle");
frame.setVisible(true);

    panelSmall.setLayout(new GridLayout(6,1,0,0));     
    panelSmall.add(buttoncircle);
    panelSmall.add(buttonline);
    panelSmall.add(buttonrectangle);
    panelSmall.add(buttontext);
    panelSmall.add(buttontriangle);

//添加颜色板

JPanel colorpane = new JPanel();
panelSmall.add(colorpane);
colorpane.setLayout(new GridLayout(3,3));
JButton buttonred = new JButton();
buttonred.setBackground(Color.red);
colorpane.add(buttonred);
JButton buttonblack = new JButton();
buttonblack.setBackground(Color.black);
colorpane.add(buttonblack);
JButton buttonblue = new JButton();
buttonblue.setBackground(Color.blue);
buttonblue.setContentAreaFilled(false);
colorpane.add(buttonblue);
JButton buttongreen = new JButton();
buttongreen.setBackground(Color.green);
colorpane.add(buttongreen);
JButton buttongray = new JButton();
buttongray.setBackground(Color.gray);
colorpane.add(buttongray);
JButton buttonwhite = new JButton();
buttonwhite.setBackground(Color.white);
colorpane.add(buttonwhite);
JButton buttonyellow = new JButton();
buttonyellow.setBackground(Color.yellow);
colorpane.add(buttonyellow);
JButton buttonpink = new JButton();
buttonpink.setBackground(Color.pink);
colorpane.add(buttonpink);
JButton buttonorange = new JButton();
buttonorange.setBackground(Color.orange);
colorpane.add(buttonorange);

  //监听使用哪个图形  
    control.Control monitor = new control.Control();

//添加颜色监听器
// control.Control color = new control.Control();
buttonred.addActionListener(monitor);
buttonblack.addActionListener(monitor);
buttonblue.addActionListener(monitor);
buttongreen.addActionListener(monitor);
buttongray.addActionListener(monitor);
buttonwhite.addActionListener(monitor);
buttonyellow.addActionListener(monitor);
buttonpink.addActionListener(monitor);
buttonorange.addActionListener(monitor);

    buttoncircle.addActionListener(monitor);
    buttonline.addActionListener(monitor);
    buttonrectangle.addActionListener(monitor);
    buttontext.addActionListener(monitor);
    buttontriangle.addActionListener(monitor); 

    panelBig.addMouseListener(monitor);
    panelBig.addMouseMotionListener(monitor);
    monitor.setG(panelBig.getGraphics());
    monitor.getDrawX();


    frame.setVisible(true);               
}   

public static void main(String[] args)
{
    Gui pic = new Gui();
}

}











Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!