想要一个用java的eclipse软件开发的画图软件的代码、急需的。财富值少了点。但也希望有人能帮帮忙、
发布网友
发布时间:2022-07-08 10:26
我来回答
共1个回答
热心网友
时间:2023-10-10 03:20
当年的作业,比较简单的小程序
import java.awt.*;
import java.awt.event.*;
import javax. swing.*;
class Graphic implements ActionListener
{ JFrame f;
JMenuBar myMenuBar;
JMenu menuDraw,menuHelp;
JMenuItem itemCircle,itemRect,itemLine;
int x,y,lastX=0,lastY=0,drawType;
public void display()
{ f = new JFrame("带有菜单的窗口");
f.setSize(300,300); f.setLocation(300,300);
f.setBackground(Color.lightGray);
myMenuBar=new JMenuBar();
menuDraw=new JMenu("画图");
menuHelp=new JMenu("帮助");
myMenuBar.add(menuDraw); myMenuBar.add(menuHelp);
itemCircle=new JMenuItem("画圆");
itemRect=new JMenuItem("画矩形");
itemLine=new JMenuItem("画线");
menuDraw.add(itemLine);
menuDraw.add(itemCircle);
menuDraw.add(itemRect);
f.setJMenuBar(myMenuBar);
f.setVisible(true);
itemCircle.addActionListener(this);
itemRect.addActionListener(this);
itemLine.addActionListener(this);
f.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
lastX=e.getX();
lastY=e.getY();
}
});
f.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX();
y=e.getY();
Graphics g=f.getGraphics();
if(drawType==1)
{
g.clearRect(0,50,300,300);
g.drawOval(lastX,lastY,x-lastX,y-lastY);
}
else if(drawType==2)
{
g.clearRect(0,50,300,300);
g.drawRect(lastX,lastY,x-lastX,y-lastY);
}
else if(drawType==3)
{
g.clearRect(0,50,300,300);
g.drawLine(lastX,lastY,x,y);
}
}
});
}
public void actionPerformed(ActionEvent e)
{ if(e.getActionCommand()=="画圆")
drawType=1;
else if(e.getActionCommand()=="画矩形")
drawType=2;
else if(e.getActionCommand()=="画线")
drawType=3;
}
public static void main(String arg[])
{ (new Graphic()).display(); }
}