| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 650 人关注过本帖
标题:用鼠标在面板上拖拽,画不同颜色的线条
只看楼主 加入收藏
爱好者
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2005-8-15
收藏
 问题点数:0 回复次数:1 
用鼠标在面板上拖拽,画不同颜色的线条

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

public class Myframe1 extends JFrame { Mypanel1 p1; Mypanel2 p2; public Color c=Color.white; public Myframe1(String str) { super(str); p1=new Mypanel1(c,p2); p2=new Mypanel2(c); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(p1,BorderLayout.NORTH); this.getContentPane().add(p2,BorderLayout.CENTER); setSize(600,500); setVisible(true); } public static void main(String args[]) { new Myframe1("画图"); } } class Mypanel1 extends JPanel implements ActionListener { JButton b1,b2,b3,b4,b5; public Color c; Mypanel2 p2; public Mypanel1(Color c,Mypanel2 p2) { this.c=c; this.p2=p2; b1=new JButton("红色"); b2=new JButton("蓝色"); b3=new JButton("绿色"); b4=new JButton("橡皮"); b5=new JButton("清除"); this.add(b1); this.add(b2); this.add(b3); this.add(b4); this.add(b5); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); } public void actionPerformed(ActionEvent e) { if(e.getSource()==b1) { c=Color.red; } else if(e.getSource()==b2) { c=Color.blue; } else if(e.getSource()==b3) { c=Color.green; } else if(e.getSource()==b4) { c=Color.white; } else { p2.repaint(); } } } class Mypanel2 extends JPanel { public Color c; public Mypanel2(Color c) { this.c=c; this.addMouseMotionListener(new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { Graphics g=getGraphics(); g.setColor(c);/*这里老报错*/ g.fillOval(e.getX(),e.getY(),3,3); } }); } public void paintComponent(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,getSize().width,getSize().height); } }

搜索更多相关主题的帖子: 鼠标 线条 面板 颜色 
2005-08-25 08:01
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
// try it
import java.util.Vector;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Myframe1 extends JFrame
{
  Mypanel1 p1;
  Mypanel2 p2;
  public Color c=Color.white;
  public Myframe1(String str)
  {
    super(str);
    p2=new Mypanel2();
    p1=new Mypanel1(p2);
   
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(p1,BorderLayout.NORTH);
    this.getContentPane().add(p2,BorderLayout.CENTER);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(600,500);
    setVisible(true);
  }
  public static void main(String args[])
  {
    new Myframe1("画图");
  }
}

class Mypanel1 extends JPanel implements ActionListener
{
  JButton b1,b2,b3,b4,b5;
  public Color c;
  final Mypanel2 p2;
  public Mypanel1(Mypanel2 p2)
  {
    this.p2=p2;
    b1=new JButton("红色");
    b2=new JButton("蓝色");
    b3=new JButton("绿色");
    b4=new JButton("橡皮");
    b5=new JButton("清除");
    this.add(b1);
    this.add(b2);
    this.add(b3);
    this.add(b4);
    this.add(b5);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
  }
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==b1)
    {
      p2.setCurrentColor(Color.red);
      p2.setMode(Mypanel2.RED);
    }
    else if(e.getSource()==b2)
    {
      p2.setCurrentColor(Color.blue);
      p2.setMode(Mypanel2.BLUE);
    }
    else if(e.getSource()==b3)
    {   
      p2.setCurrentColor(Color.green);
      p2.setMode(Mypanel2.GREEN);
    }
    else if(e.getSource()==b4)
    {
      p2.setCurrentColor(Color.white);
      p2.setMode(Mypanel2.RUBBER);
    }
    else
    {
      p2.setCurrentColor(Color.white);
      p2.setMode(Mypanel2.ERASE);
    }
    p2.repaint();
  }
}

class Mypanel2 extends JPanel implements MouseMotionListener
{
  public final static int RED     = 0;
  public final static int BLUE    = 1;
  public final static int GREEN   = 2;
  public final static int RUBBER  = 3;
  public final static int ERASE   = 4;
  public final static int NOTHING = 5;
  int mode = NOTHING;
  public Color c;
  Vector rects = new Vector();
  int x = 0;
  int y = 0;
  final int w = 9;
  final int h = 9;
  public Mypanel2()
  {
    c = Color.white;
    setBackground(c);
    this.addMouseMotionListener(this);
  }
  public void paintComponent(Graphics g)
  {
    g.setColor(c);
    super.paintComponent(g);
    switch(mode)
    {
      case RED:
      case BLUE:
      case GREEN:
        setBackground(c);
        break;
      case RUBBER:
        for(int i = 0; i<rects.size(); i++)
        {
          Rectangle rect = (Rectangle) rects.elementAt(i);
          int x = rect.x;
          int y = rect.y;
          int w = rect.width;
          int h = rect.height;
          g.fillOval(x,y,w,h);
        }
        break;
      case ERASE:
        rects.clear();
        setBackground(c);
        break;
      case NOTHING:
        setBackground(c);
        break;
    }
  }
  public void setCurrentColor(Color currentC)
  {
    this.c = currentC;
  }
  public void setMode(int mode)
  {
    this.mode = mode;
  }
  public void mouseDragged(MouseEvent e)
  {
    if(mode == RUBBER)
    {
      x = e.getX();
      y = e.getY();
      Rectangle rect = new Rectangle(x,y,w,h);
      rects.add(rect);
      repaint();
    }
  }
  public void mouseMoved(MouseEvent e){}
}

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-08-25 13:22
快速回复:用鼠标在面板上拖拽,画不同颜色的线条
数据加载中...
 
   



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

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