有一个作业 :
随手画例程,可以画矩形、圆形、三角形、直线,同时具有网络能力,就是说服务端画完可以发给客户端,客户端画完也可以发给服务端,这是基本的要求现在画图功能都已经实现了 但是不知道网络功能如何实现 请各位高手帮帮忙已经写好的代码:
import java.awt.*;
import java.awt.event.*;//包含MouseListener
import javax.swing.*;
import javax.swing.event.*;
class PainterPanel extends JPanel implements MouseListener{
int shape=-1; //图案类型
Point[] point=new Point[3]; //记录鼠标拖动的起始点和终点
//构造函数**********************************************
public PainterPanel(){
super(); //调用父类构造函数
this.setBackground(Color.white); //设置背景颜色(JPanel成员) 其他成员对绘图没有实质性的帮助
point[0]=new Point(-1,-1); //初始化变量
point[1]=new Point(-1,-1);
addMouseListener(this); //增加鼠标事件
}
//构造函数************************************************
public void mouseReleased(MouseEvent e){ //鼠标释放事件
point[1]=new Point(e.getX(),e.getY()); //设置终点位置
repaint(); //重绘屏幕
} public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e)
{ point[2]=new Point (e.getX(),e.getY());
}
public void mousePressed(MouseEvent e)
{ //鼠标按下时事件
point[0]=new Point(e.getX(),e.getY()); //设置起始点位置
} public void paint(Graphics g){
super.paint(g);
switch (shape){ //根据shape值绘制图形
case 0:
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
break; case 1:
int width=point[1].x-point[0].x;
int height=point[1].y-point[0].y;
g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
break; case 2:
width=point[1].x-point[0].x;
height=point[1].y-point[0].y;
g.drawRect(point[0].x,point[0].y,width,height); //绘矩形
break; case 3: g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y);
g.drawLine(point[0].x,point[0].y,point[2].x,point[2].y);
g.drawLine(point[2].x,point[2].y,point[1].x,point[1].y);
break;
}
}
public void drawShape(int shape)
{
this.shape=shape;
}
}
public class PainterDemo extends JFrame{
JToggleButton[] button=new JToggleButton[4]; //按钮组
PainterPanel painter=new PainterPanel(); //绘图面板 上面构造了
public PainterDemo(){
super("Java随手画"); //调用父类构造函数
String[] buttonName={"直线","椭圆","矩形"," 三角形"}; //按钮文字
DrawShapeListener buttonListener=new DrawShapeListener(); //按钮事件
JToolBar toolBar=new JToolBar(); //实例化工具栏
ButtonGroup buttonGroup=new ButtonGroup(); //实例化按钮组 for (int i=0;i<button.length;i++){
button[i]=new JToggleButton(buttonName[i]); //实例化按钮
button[i].addActionListener(buttonListener); //增加按钮事件处理
buttonGroup.add(button[i]); //增加按钮到按钮组
toolBar.add(button[i]); //增加按钮到工具栏
} Container container=getContentPane(); //得到窗口容器
container.add(toolBar,BorderLayout.NORTH); //增加组件到容器上
container.add(painter,BorderLayout.CENTER);
setSize(300,200); //设置窗口尺寸
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
//内部类
class DrawShapeListener implements ActionListener{ //按钮事件处理
public void actionPerformed(ActionEvent e){
for (int i=0;i<button.length;i++){
if (e.getSource()==button[i]){ //判断来自于哪个按钮
painter.drawShape(i); //绘制图形
}
}
}
} public static void main(String[] args){
new PainterDemo();
}
}
随手画例程,可以画矩形、圆形、三角形、直线,同时具有网络能力,就是说服务端画完可以发给客户端,客户端画完也可以发给服务端,这是基本的要求现在画图功能都已经实现了 但是不知道网络功能如何实现 请各位高手帮帮忙已经写好的代码:
import java.awt.*;
import java.awt.event.*;//包含MouseListener
import javax.swing.*;
import javax.swing.event.*;
class PainterPanel extends JPanel implements MouseListener{
int shape=-1; //图案类型
Point[] point=new Point[3]; //记录鼠标拖动的起始点和终点
//构造函数**********************************************
public PainterPanel(){
super(); //调用父类构造函数
this.setBackground(Color.white); //设置背景颜色(JPanel成员) 其他成员对绘图没有实质性的帮助
point[0]=new Point(-1,-1); //初始化变量
point[1]=new Point(-1,-1);
addMouseListener(this); //增加鼠标事件
}
//构造函数************************************************
public void mouseReleased(MouseEvent e){ //鼠标释放事件
point[1]=new Point(e.getX(),e.getY()); //设置终点位置
repaint(); //重绘屏幕
} public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e)
{ point[2]=new Point (e.getX(),e.getY());
}
public void mousePressed(MouseEvent e)
{ //鼠标按下时事件
point[0]=new Point(e.getX(),e.getY()); //设置起始点位置
} public void paint(Graphics g){
super.paint(g);
switch (shape){ //根据shape值绘制图形
case 0:
g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
break; case 1:
int width=point[1].x-point[0].x;
int height=point[1].y-point[0].y;
g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
break; case 2:
width=point[1].x-point[0].x;
height=point[1].y-point[0].y;
g.drawRect(point[0].x,point[0].y,width,height); //绘矩形
break; case 3: g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y);
g.drawLine(point[0].x,point[0].y,point[2].x,point[2].y);
g.drawLine(point[2].x,point[2].y,point[1].x,point[1].y);
break;
}
}
public void drawShape(int shape)
{
this.shape=shape;
}
}
public class PainterDemo extends JFrame{
JToggleButton[] button=new JToggleButton[4]; //按钮组
PainterPanel painter=new PainterPanel(); //绘图面板 上面构造了
public PainterDemo(){
super("Java随手画"); //调用父类构造函数
String[] buttonName={"直线","椭圆","矩形"," 三角形"}; //按钮文字
DrawShapeListener buttonListener=new DrawShapeListener(); //按钮事件
JToolBar toolBar=new JToolBar(); //实例化工具栏
ButtonGroup buttonGroup=new ButtonGroup(); //实例化按钮组 for (int i=0;i<button.length;i++){
button[i]=new JToggleButton(buttonName[i]); //实例化按钮
button[i].addActionListener(buttonListener); //增加按钮事件处理
buttonGroup.add(button[i]); //增加按钮到按钮组
toolBar.add(button[i]); //增加按钮到工具栏
} Container container=getContentPane(); //得到窗口容器
container.add(toolBar,BorderLayout.NORTH); //增加组件到容器上
container.add(painter,BorderLayout.CENTER);
setSize(300,200); //设置窗口尺寸
setVisible(true); //设置窗口为可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
//内部类
class DrawShapeListener implements ActionListener{ //按钮事件处理
public void actionPerformed(ActionEvent e){
for (int i=0;i<button.length;i++){
if (e.getSource()==button[i]){ //判断来自于哪个按钮
painter.drawShape(i); //绘制图形
}
}
}
} public static void main(String[] args){
new PainterDemo();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货