鼠标随手画的java代码
还有相关功能:编写一个用鼠标来画图,并且有菜单保存功能,有4个按钮(方形,圆形,三角形,直线),按一下弹出一个对话框,输入参数就显示相应图形...
还有相关功能:编写一个用鼠标来画图,并且有菜单保存功能,有4个按钮(方形,圆形,三角形,直线),按一下弹出一个对话框,输入参数就显示相应图形
展开
1个回答
展开全部
//画布类
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
public void drawRect(Graphics g, int x, int y, int width, int height){
g.drawRect(x, y, width, height);
}
public void drawOval(Graphics g, int x, int y, int width ,int height){
g.drawOval(x, y, width, height);
}
public void drawLine(Graphics g, int x1 ,int y1 ,int x2, int y2){
g.drawLine(x1, y1, x2, y2);
}
public void drawTriangle(Graphics g, int x, int y, int borderSize){
int[] xx = {borderSize,x,borderSize*2};
int[] yy = {y,borderSize*2,borderSize*2};
g.drawPolygon(new Polygon(xx,yy,3));
}
}
//主界面类
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyPaint extends JFrame implements ActionListener{
private JPanel toolArea;
private MyPanel imageArea;
private JButton rect;
private JButton oval;
private JButton line;
private JButton triangle;
public MyPaint() {
rect = new JButton("矩形");
rect.addActionListener(this);
oval = new JButton("圆形");
oval.addActionListener(this);
line = new JButton("直线");
line.addActionListener(this);
triangle = new JButton("三角");
triangle.addActionListener(this);
toolArea = new JPanel(new GridLayout(4,1));
toolArea.add(rect,0);
toolArea.add(oval,1);
toolArea.add(line,2);
toolArea.add(triangle,3);
imageArea = new MyPanel();
this.add("West",toolArea);
this.add("Center",imageArea);
this.setVisible(true);
this.setBounds(112, 84, 800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Graphics g = imageArea.getGraphics();
if(e.getSource() == rect){
imageArea.drawRect(g, 10, 10, 500, 300);
}else if(e.getSource() == oval){
imageArea.drawOval(g, 50, 50, 100, 100);
}else if(e.getSource() == line){
imageArea.drawLine(g, 20, 20, 80, 80);
}else if(e.getSource() == triangle){
imageArea.drawTriangle(g, 0, 40, 100);
}
}
public static void main(String[] args) {
new MyPaint();
}
}
有点麻烦,没写完,先给你开个头吧。你现在自己写写,有什么不懂的知识点再问我好啦。我吃早餐去了。
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
public void drawRect(Graphics g, int x, int y, int width, int height){
g.drawRect(x, y, width, height);
}
public void drawOval(Graphics g, int x, int y, int width ,int height){
g.drawOval(x, y, width, height);
}
public void drawLine(Graphics g, int x1 ,int y1 ,int x2, int y2){
g.drawLine(x1, y1, x2, y2);
}
public void drawTriangle(Graphics g, int x, int y, int borderSize){
int[] xx = {borderSize,x,borderSize*2};
int[] yy = {y,borderSize*2,borderSize*2};
g.drawPolygon(new Polygon(xx,yy,3));
}
}
//主界面类
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyPaint extends JFrame implements ActionListener{
private JPanel toolArea;
private MyPanel imageArea;
private JButton rect;
private JButton oval;
private JButton line;
private JButton triangle;
public MyPaint() {
rect = new JButton("矩形");
rect.addActionListener(this);
oval = new JButton("圆形");
oval.addActionListener(this);
line = new JButton("直线");
line.addActionListener(this);
triangle = new JButton("三角");
triangle.addActionListener(this);
toolArea = new JPanel(new GridLayout(4,1));
toolArea.add(rect,0);
toolArea.add(oval,1);
toolArea.add(line,2);
toolArea.add(triangle,3);
imageArea = new MyPanel();
this.add("West",toolArea);
this.add("Center",imageArea);
this.setVisible(true);
this.setBounds(112, 84, 800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Graphics g = imageArea.getGraphics();
if(e.getSource() == rect){
imageArea.drawRect(g, 10, 10, 500, 300);
}else if(e.getSource() == oval){
imageArea.drawOval(g, 50, 50, 100, 100);
}else if(e.getSource() == line){
imageArea.drawLine(g, 20, 20, 80, 80);
}else if(e.getSource() == triangle){
imageArea.drawTriangle(g, 0, 40, 100);
}
}
public static void main(String[] args) {
new MyPaint();
}
}
有点麻烦,没写完,先给你开个头吧。你现在自己写写,有什么不懂的知识点再问我好啦。我吃早餐去了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询