| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2604 人关注过本帖
标题:java 用鼠标画直线,圆,矩形的共存问题
只看楼主 加入收藏
zlfqwe
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-1-18
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
java 用鼠标画直线,圆,矩形的共存问题
再窗口上切换图形的时候,已经画好的图形也随之转换过来。
没有能够达到3种图形共存。怎么修改比较好呢?
就是想画直线的时候出直线,想画圆的时候出圆,但它们彼此共存再窗口上。
-------------------------------------
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Test{
    public static void main(String[] args){
    MyFrame frame = new MyFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
}

class MyFrame extends JFrame implements ActionListener{

     MyPanel panel = new MyPanel();
   
    JToolBar t;
    JButton obj1;
    JButton obj2;
    JButton obj3;

    public MyFrame(){

    obj1 = new JButton("Line");
    obj2 = new JButton("yuan");
    obj3 = new JButton("juxing");
    t = new JToolBar();
    obj1.addActionListener(this);
    obj2.addActionListener(this);
    obj3.addActionListener(this);

    t = new JToolBar();
    t.add(obj1);
    t.add(obj2);
    t.add(obj3);


    setTitle("DrawTest");
    setSize(W,H);

    Container contentPane = getContentPane();

    contentPane.add(t,BorderLayout.NORTH);
    contentPane.add(panel,BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e){
    if(e.getSource()==obj1)
        panel.shape=0;//直線
    if(e.getSource()==obj2)
        panel.shape=1;//円
    if(e.getSource()==obj3)
        panel.shape=2;//長方形
    }
   
    public static final int W = 400;
    public static final int H = 400;
}


class MyPanel extends JPanel implements MouseListener{

  
    private ArrayList<Point> pointList = new ArrayList<Point>();


    int shape = -1;
    int x1 = 0,y1 = 0;
    int x2 = 0,y2 = 0;

    MyPanel(){
    addMouseListener(this);
    }
   
    public void mousePressed(MouseEvent e){

    x1 = e.getX();
    y1 = e.getY();
    pointList.add(new Point(x1,y1));

    }
    public void mouseReleased(MouseEvent e){

    x2 = e.getX();
    y2 = e.getY();
    pointList.add(new Point(x2,y2));
    repaint();

    }
   
    public void mouseClicked(MouseEvent e){
    }
   
    public void mouseMoved(MouseEvent e){
    }
   
    public void mouseEntered(MouseEvent e){
    }
   
    public void mouseExited(MouseEvent e){
    }
   
   

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

    for(int i =0 ;i<pointList.size()-1;i=i+2){
        Point p1 = pointList.get(i);
        Point p2 = pointList.get(i+1);
        
        switch(shape){
        
        case 0:        
        g.drawLine(p1.x,p1.y,p2.x,p2.y);
        
        break;
        case 1:
        
        int width1  = p2.x - p1.x;
        int height1 = p2.y - p1.y;
        g.drawOval(p1.x,p1.y,width1,height1);
        break;
        case 2:
        int width2  = p2.x - p1.x;
        int height2 = p2.y - p1.y;
        g.drawRect(p1.x,p1.y,width2,height2);
        break;
        
        default:
        System.out.println("please once again!");
        break;
        }   
    }
    }
}
搜索更多相关主题的帖子: 图形 public frame import 
2013-01-18 12:28
w527705090
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:441
专家分:1882
注 册:2011-6-28
收藏
得分:0 
这个问题还是留做课后练习吧

有心者,千方百计;无心者,千难万难。
2013-01-18 18:53
shellingford
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:19
帖 子:228
专家分:1348
注 册:2010-8-9
收藏
得分:20 
程序代码:
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class Test {
    public static void main(String[] args) {
        MyFrame frame = new MyFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class MyFrame extends JFrame implements ActionListener {

    MyPanel panel = new MyPanel();

    JToolBar t;
    JButton obj1;
    JButton obj2;
    JButton obj3;

    public MyFrame() {

        obj1 = new JButton("Line");
        obj2 = new JButton("yuan");
        obj3 = new JButton("juxing");
        t = new JToolBar();
        obj1.addActionListener(this);
        obj2.addActionListener(this);
        obj3.addActionListener(this);

        t = new JToolBar();
        t.add(obj1);
        t.add(obj2);
        t.add(obj3);

        setTitle("DrawTest");
        setSize(W, H);

        Container contentPane = getContentPane();

        contentPane.add(t, BorderLayout.NORTH);
        contentPane.add(panel, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == obj1)
            panel.shape = 0;// 直線
        if (e.getSource() == obj2)
            panel.shape = 1;//
        if (e.getSource() == obj3)
            panel.shape = 2;// 長方形
    }

    public static final int W = 400;
    public static final int H = 400;
}

//建立一个自己的对应动作
class MyAction {
    private Point begin;
    private Point end;
    private int shape;
    public Point getBegin() {
        return begin;
    }
    public void setBegin(Point begin) {
        this.begin = begin;
    }
    public Point getEnd() {
        return end;
    }
    public void setEnd(Point end) {
        this.end = end;
    }
    public int getShape() {
        return shape;
    }
    public void setShape(int shape) {
        this.shape = shape;
    }
}

class MyPanel extends JPanel implements MouseListener {

    private ArrayList<MyAction> pointList = new ArrayList<MyAction>();

    int shape = -1;  //panel只有一个,这个属性只有一个,是不可能区分所有的鼠标点击事件后需要画不同的内容
    int x1 = 0, y1 = 0;
    int x2 = 0, y2 = 0;
    private MyAction myaction;

    MyPanel() {
        addMouseListener(this);
    }

    public void mousePressed(MouseEvent e) {

        x1 = e.getX();
        y1 = e.getY();
        myaction=new MyAction();
        myaction.setBegin(new Point(x1, y1));
        myaction.setShape(shape);
    }

    public void mouseReleased(MouseEvent e) {

        x2 = e.getX();
        y2 = e.getY();
        myaction.setEnd(new Point(x2, y2));
        pointList.add(myaction);
        myaction=null;
        repaint();

    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

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

        for (MyAction action : pointList) {
           

            Point p1 = action.getBegin();
            Point p2 = action.getEnd();

            switch (action.getShape()) {

            case 0:
                g.drawLine(p1.x, p1.y, p2.x, p2.y);

                break;
            case 1:

                int width1 = p2.x - p1.x;
                int height1 = p2.y - p1.y;
                g.drawOval(p1.x, p1.y, width1, height1);
                break;
            case 2:
                int width2 = p2.x - p1.x;
                int height2 = p2.y - p1.y;
                g.drawRect(p1.x, p1.y, width2, height2);
                break;

            default:
                System.out.println("please once again!");
                break;
            }
        }
    }
}
2013-01-19 12:58
zlfqwe
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-1-18
收藏
得分:0 
回复 3楼 shellingford
谢谢,我已经写出来了。你的也可以拿出参考。
2013-01-21 06:48
快速回复:java 用鼠标画直线,圆,矩形的共存问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035107 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved