java题求帮忙,要点击画直线按钮自己就可以用鼠标画直线,点击画矩形就可以用鼠标画矩形,求帮忙
1个回答
推荐于2016-08-05
展开全部
类似的,自己改改就好了:
package ziliao;
//鼠标画图1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//鼠标事件处理
//事件接口: MouseListener, MouseMotionListener
//使用鼠标画图
//使用JPanel面板绘图
class Co13 extends JFrame
{
JPanel jp;
JRadioButton jrb1, jrb2, jrb3, jrb4;
Co13()
{
jrb1 = new JRadioButton("直线",true);
jrb2 = new JRadioButton("矩形");
jrb3 = new JRadioButton("椭圆");
jrb4 = new JRadioButton("圆");
ButtonGroup bg = new ButtonGroup(); //按钮组
bg.add(jrb1); //加入按钮组
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jrb3);
jp.add(jrb4);
add(jp,BorderLayout.NORTH);
add(new MyPanel(this));
setBounds(120,125,300,200);
setTitle("使用鼠标绘图");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public int getJrb() //判断判断并返回选择的是哪个单选按钮
{
int jrb;
if(jrb1.isSelected())
jrb = 1;
else if(jrb2.isSelected())
jrb = 2;
else if(jrb3.isSelected())
jrb = 3;
else
jrb = 4;
return jrb;
}
}
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{ //将JPanel面板作为画布
int x1, y1, x2, y2;
Co13 jf; //窗体对象,用于传递窗体中选择的按钮
MyPanel(Co13 jf)
{
this.jf = jf;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
//覆盖 paintComponent 方法
public void paintComponent(Graphics g)
{
super.paintComponent(g); //调用超类JPanel构造方法
String s = null;
g.setColor(Color.red);
switch (jf.getJrb()) //判断选择什么图形
{
case 1: //画直线
g.drawLine(x1, y1, x2, y2); //起始点和终点,两点固定一条直线
s = "直线绘制";
break;
case 2: //画矩形
g.drawRect(x1, y1, x2 - x1, y2 - y1); //以起始点为原点,移动距离为长宽
s = "矩形绘制";
break;
case 3: //画椭圆
g.drawOval(x1, y1, x2 - x1, y2 - y1); //以起始点为圆心,移动距离为半径
s = "椭圆绘制";
break;
case 4: //画圆
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int r = (int)(Math.sqrt(dx * dx + dy * dy));
g.drawOval(x1 - r, y1 - r, r + r, r + r);
g.setColor(Color.gray);
g.drawLine(x1, y1, x2, y2);
s = "正圆绘制";
break;
}
g.setColor(Color.blue);
g.drawString(s, 10, 10);
}
public void mousePressed(MouseEvent e)
{
x1 = e.getX(); //获取鼠标按下时的坐标
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX(); //获取鼠标拖动时的坐标
y2 = e.getY();
repaint(); //重绘图形
}
public void mouseMoved(MouseEvent e)
{
}
}
public class E6_13
{ public static void main(String args[])
{ new Co13();
}
}
package ziliao;
//鼠标画图1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//鼠标事件处理
//事件接口: MouseListener, MouseMotionListener
//使用鼠标画图
//使用JPanel面板绘图
class Co13 extends JFrame
{
JPanel jp;
JRadioButton jrb1, jrb2, jrb3, jrb4;
Co13()
{
jrb1 = new JRadioButton("直线",true);
jrb2 = new JRadioButton("矩形");
jrb3 = new JRadioButton("椭圆");
jrb4 = new JRadioButton("圆");
ButtonGroup bg = new ButtonGroup(); //按钮组
bg.add(jrb1); //加入按钮组
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jrb3);
jp.add(jrb4);
add(jp,BorderLayout.NORTH);
add(new MyPanel(this));
setBounds(120,125,300,200);
setTitle("使用鼠标绘图");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public int getJrb() //判断判断并返回选择的是哪个单选按钮
{
int jrb;
if(jrb1.isSelected())
jrb = 1;
else if(jrb2.isSelected())
jrb = 2;
else if(jrb3.isSelected())
jrb = 3;
else
jrb = 4;
return jrb;
}
}
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{ //将JPanel面板作为画布
int x1, y1, x2, y2;
Co13 jf; //窗体对象,用于传递窗体中选择的按钮
MyPanel(Co13 jf)
{
this.jf = jf;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
//覆盖 paintComponent 方法
public void paintComponent(Graphics g)
{
super.paintComponent(g); //调用超类JPanel构造方法
String s = null;
g.setColor(Color.red);
switch (jf.getJrb()) //判断选择什么图形
{
case 1: //画直线
g.drawLine(x1, y1, x2, y2); //起始点和终点,两点固定一条直线
s = "直线绘制";
break;
case 2: //画矩形
g.drawRect(x1, y1, x2 - x1, y2 - y1); //以起始点为原点,移动距离为长宽
s = "矩形绘制";
break;
case 3: //画椭圆
g.drawOval(x1, y1, x2 - x1, y2 - y1); //以起始点为圆心,移动距离为半径
s = "椭圆绘制";
break;
case 4: //画圆
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int r = (int)(Math.sqrt(dx * dx + dy * dy));
g.drawOval(x1 - r, y1 - r, r + r, r + r);
g.setColor(Color.gray);
g.drawLine(x1, y1, x2, y2);
s = "正圆绘制";
break;
}
g.setColor(Color.blue);
g.drawString(s, 10, 10);
}
public void mousePressed(MouseEvent e)
{
x1 = e.getX(); //获取鼠标按下时的坐标
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX(); //获取鼠标拖动时的坐标
y2 = e.getY();
repaint(); //重绘图形
}
public void mouseMoved(MouseEvent e)
{
}
}
public class E6_13
{ public static void main(String args[])
{ new Co13();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询