import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import com.zxh.swing.*;public class MyPainter1 extends JFrame{
private JButton line=new JButton("line");
private JButton oval=new JButton("oval");
private JButton rectangle=new JButton("rectangle");
private JPanel buttonPanel=new JPanel();
private Container c;
private int flag;
private int x,y,x1,y1;
LineFigure lineFigure = null;
public MyPainter1() {
super("MyTextArea");
flag=0;
c=getContentPane();
c.setLayout(new BorderLayout());
c.add(buttonPanel,BorderLayout.WEST);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(line);
buttonPanel.add(oval);
buttonPanel.add(rectangle);
line.addActionListener(new lineListener());
//oval.addActionListener(new ovalListener());
// rectangle.addActionListener(new rectangleListener());
addMouseListener(new DrawListener());
lineFigure=new LineFigure();
setSize(640,480);
setVisible(true);
} public void paint(Graphics g){
super.paint(g);
if(lineFigure != null){
lineFigure.paint(g);
}
}class lineListener implements ActionListener {
public void actionPerformed(ActionEvent e){
flag=1;
}
}/*class ovalListener implements ActionListener{
public void actionPerformed(ActionEvent e){
flag=1;
}
}class rectangleListener implements ActionListener{
public void actionPerformed(ActionEvent e){
flag=1;
}
}*/
class DrawListener extends MouseAdapter {
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e){
x1=e.getX();
y1=e.getY();
repaint();
}// public void mouseClicked(MouseEvent e) {
// if (flag==1) {
// x=e.getX();
// y=e.getY();
// flag=2;
// }
// else if (flag==2) {
// x1=e.getX();
// y1=e.getY();
// repaint();
// }
// }
} public abstract class Figure{ public abstract void paint(Graphics g);
} class LineFigure extends Figure {
public void paint(Graphics g){
// if(flag==2)
g.drawLine(x,y,x1,y1);//»æÖÆÖ±Ïß
// flag=0;
}
} /*class OvalFigure extends Figure {
public void paint(Graphics g){
int X,Y;
if(x>x1)
X=x1;
else
X=x;
if(y>y1)
Y=y1;
else
Y=y1;
if(flag==2)g.drawOval(X,Y,Math.abs(x1-x),Math.abs(y1-y));//»æÖÆÍÖÔ²
flag=0;
}
}
class RectFigure extends Figure {
public void paint(Graphics g){
int X,Y;
if(x>x1)
X=x1;
else
X=x;
if(y>y1)
Y=y1;
else
Y=y;
if(flag==2)g.drawRect(X,Y,Math.abs(x1-x),Math.abs(y1-y)); //»æÖƾØÐÎ
flag=0;
}
}
*/public static void main(String args[]){
MyPainter1 app=new MyPainter1();
// app.addWindowListener(new MyWindowListener());
}
}
import java.awt.event.*;
import javax.swing.*;
//import com.zxh.swing.*;public class MyPainter1 extends JFrame{
private JButton line=new JButton("line");
private JButton oval=new JButton("oval");
private JButton rectangle=new JButton("rectangle");
private JPanel buttonPanel=new JPanel();
private Container c;
private int flag;
private int x,y,x1,y1;
LineFigure lineFigure = null;
public MyPainter1() {
super("MyTextArea");
flag=0;
c=getContentPane();
c.setLayout(new BorderLayout());
c.add(buttonPanel,BorderLayout.WEST);
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(line);
buttonPanel.add(oval);
buttonPanel.add(rectangle);
line.addActionListener(new lineListener());
//oval.addActionListener(new ovalListener());
// rectangle.addActionListener(new rectangleListener());
addMouseListener(new DrawListener());
lineFigure=new LineFigure();
setSize(640,480);
setVisible(true);
} public void paint(Graphics g){
super.paint(g);
if(lineFigure != null){
lineFigure.paint(g);
}
}class lineListener implements ActionListener {
public void actionPerformed(ActionEvent e){
flag=1;
}
}/*class ovalListener implements ActionListener{
public void actionPerformed(ActionEvent e){
flag=1;
}
}class rectangleListener implements ActionListener{
public void actionPerformed(ActionEvent e){
flag=1;
}
}*/
class DrawListener extends MouseAdapter {
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e){
x1=e.getX();
y1=e.getY();
repaint();
}// public void mouseClicked(MouseEvent e) {
// if (flag==1) {
// x=e.getX();
// y=e.getY();
// flag=2;
// }
// else if (flag==2) {
// x1=e.getX();
// y1=e.getY();
// repaint();
// }
// }
} public abstract class Figure{ public abstract void paint(Graphics g);
} class LineFigure extends Figure {
public void paint(Graphics g){
// if(flag==2)
g.drawLine(x,y,x1,y1);//»æÖÆÖ±Ïß
// flag=0;
}
} /*class OvalFigure extends Figure {
public void paint(Graphics g){
int X,Y;
if(x>x1)
X=x1;
else
X=x;
if(y>y1)
Y=y1;
else
Y=y1;
if(flag==2)g.drawOval(X,Y,Math.abs(x1-x),Math.abs(y1-y));//»æÖÆÍÖÔ²
flag=0;
}
}
class RectFigure extends Figure {
public void paint(Graphics g){
int X,Y;
if(x>x1)
X=x1;
else
X=x;
if(y>y1)
Y=y1;
else
Y=y;
if(flag==2)g.drawRect(X,Y,Math.abs(x1-x),Math.abs(y1-y)); //»æÖƾØÐÎ
flag=0;
}
}
*/public static void main(String args[]){
MyPainter1 app=new MyPainter1();
// app.addWindowListener(new MyWindowListener());
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货