java 画图板 就简单的画个矩形 线条,圆就好 ,但是可以是连续不断的画的 ,不是画了一个后又重新画 .
展开全部
按照你的要求,编写的Java程序如下:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import 伍誉java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Point{
int x;
int y;
}
class MyPanel extends JPanel implements ActionListener,MouseListener,MouseMotionListener{
JButton line=new JButton("画线");
JButton oval=new JButton("画圆");
JButton rect=new JButton("腔世段画矩形");
ArrayList<Point> al=new ArrayList<Point>();
int flag=0,x,y;
MyPanel(){
add(line);
add(oval);
add(rect);
setBackground(Color.WHITE);
addMouseListener(this);
addMouseMotionListener(this);
line.addActionListener(this);
oval.addActionListener(this);
rect.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==line){
flag=1;
al.clear();
repaint();
}
if(e.getSource()==oval){
flag=2;
al.clear();
repaint();
}
if(e.getSource()==rect){
flag=3;
al.clear();
repaint();
}
}
public void paint(Graphics g){
super.paint(g);
switch(flag){
case 1:
for(int i=0;i<al.size()-1;i+=2){
g.drawLine(al.get(i).x,al.get(i).y,al.get(i+1).x,al.get(i+1).y);//画线
}
if(al.size()>=1)
g.drawLine(al.get(al.size()-1).x,al.get(al.size()-1).y,x,y);
break;
case 2:
for(int i=0;i<al.size()-1;i+=2){
int width=Math.abs(al.get(i+1).x-al.get(i).x);
int height=Math.abs(al.get(i+1).y-al.get(i).y);
int radius=(int)(Math.sqrt(width*width+height*height)/2);
g.drawOval(al.get(i).x,al.get(i).y,2*radius,2*radius);//画圆
}
if(al.size()>=1){
int width=Math.abs(x-al.get(al.size()-1).x);
int height=Math.abs(y-al.get(al.size()-1).y);
int radius=(int)(Math.sqrt(width*width+height*height)/2);
g.drawOval(al.get(al.size()-1).x,al.get(al.size()-1).y,2*radius,2*radius);
}
break;
case 3:
for(int i=0;i<al.size()-1;i+=2){
int width=Math.abs(al.get(i+1).x-al.get(i).x);
返旦 int height=Math.abs(al.get(i+1).y-al.get(i).y);
g.drawRect(al.get(i).x,al.get(i).y,width,height);//画矩形
}
if(al.size()>=1){
int width=Math.abs(x-al.get(al.size()-1).x);
int height=Math.abs(y-al.get(al.size()-1).y);
g.drawRect(al.get(al.size()-1).x,al.get(al.size()-1).y,width,height);
}
break;
}
}
@Override
public void mouseClicked(MouseEvent me) {}
@Override
public void mouseEntered(MouseEvent me) {}
@Override
public void mouseExited(MouseEvent me) {}
@Override
public void mousePressed(MouseEvent me) {
Point p=new Point();
p.x=me.getX();
p.y=me.getY();
al.add(p);
}
@Override
public void mouseReleased(MouseEvent me) {
Point p=new Point();
p.x=me.getX();
p.y=me.getY();
al.add(p);
repaint();
}
@Override
public void mouseDragged(MouseEvent me) {
x=me.getX();
y=me.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent me) {}
}
public class DrawGraphicsDemo extends JFrame {
MyPanel p=new MyPanel();
DrawGraphicsDemo(){
super("DrawGraphicsDemo");
add(p);
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main(String[] args){
new DrawGraphicsDemo();
}
}
运行结果:
画线
画圆
画矩形
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询