---
title: "Applet的一个绘图板 "
author: "rainytimes"
date: "2010-08-29T21:38:03Z"
tags: ["C", "C#", "C++", "Java"]
categories: ["编程语言"]
slug: "applet"
draft: false
lastmod: "2010-08-29T21:38:03Z"
id: 750271
url: "https://rainytimes.iteye.com/blog/750271"
---

\[code="java"] /\*用Applet做的一个会图板但是不知道为什么在画圆和方的时候,原来画好的图形会改变位置 如果谁能帮我一下 十分感激! 希望高手能在这留言赐教! \*/ import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Checkbox; import java.awt.CheckboxGroup; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.util.ArrayList; public class TestOne extends Applet { String str="我爱你中国，我的家在北京！"; int x1,y1,x2,y2,currentshape=ShapeParam.line; Color c=Color.black; CheckboxGroup cbg = new CheckboxGroup(); CheckboxGroup shapecbg=new CheckboxGroup(); ArrayList shapeparams=new ArrayList(); @Override public void destroy() { System.out.println("destroy"); } @Override public void init() { Button btn1=new Button("清屏"); Label lab1=new Label("ControlGroup"); btn1.setBackground(Color.LIGHT\_GRAY); Panel pcolor=new Panel(); Panel pshape=new Panel(); pcolor.setLayout(new GridLayout(2, 3)); Checkbox cb1=new Checkbox("Black", cbg, true); Checkbox cb2=new Checkbox("Red", cbg, false); Checkbox cb3=new Checkbox("Blue", cbg, false); Checkbox shapecb1=new Checkbox("线",shapecbg,true); Checkbox shapecb2=new Checkbox("圆",shapecbg,false); Checkbox shapecb3=new Checkbox("方",shapecbg,false); pcolor.add(cb1); pcolor.add(cb2); pcolor.add(cb3); pcolor.add(lab1); pcolor.add(shapecb1); pcolor.add(shapecb2); pcolor.add(shapecb3); pcolor.add(btn1); pcolor.setBackground(Color.LIGHT\_GRAY); pshape.setBackground(Color.LIGHT\_GRAY); this.add(pcolor,BorderLayout.CENTER); btn1.addActionListener(new Btn1Action()); this.addMouseListener(new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { x1=e.getX(); y1=e.getY(); } @Override public void mouseReleased(MouseEvent e) { shapeparams.add(new ShapeParam(x1,y1,x2,y2,c,currentshape)); } }); this.setSize(400,400); this.addMouseMotionListener(new MousePain(){ }); } @Override public void start() { } @Override public void stop() { } @Override public void paint(Graphics g) { int xx=x1; int yy=y1; if(x1&gt;x2)xx=x2; if(y1&gt;y2)yy=y2; drawshape(); g.setColor(c); changecolor(g); changeshape(); if(x1==0 &&x2==0 && y1==0 && y2==0 )return; if(currentshape==ShapeParam.line) { g.drawLine(x1, y1, x2, y2); } if(currentshape==ShapeParam.rec) { g.draw3DRect(xx, yy, Math.abs(x1-x2), Math.abs(y1-y2), true); } if(currentshape==ShapeParam.oval) { g.drawOval(xx, yy, Math.abs(x1-x2), Math.abs(y1-y2)); } } private void changeshape() { if(shapecbg.getSelectedCheckbox().getLabel()=="线")currentshape=ShapeParam.line; if(shapecbg.getSelectedCheckbox().getLabel()=="方")currentshape=ShapeParam.rec; if(shapecbg.getSelectedCheckbox().getLabel()=="圆")currentshape=ShapeParam.oval; } private void changecolor(Graphics g) { if(cbg.getSelectedCheckbox().getLabel()=="Blue") {g.setColor(c=Color.blue);return;} if(cbg.getSelectedCheckbox().getLabel()=="Red") {g.setColor(c=Color.red);return;} if(cbg.getSelectedCheckbox().getLabel()=="Black") {g.setColor(c=Color.black);return;} } private void drawshape() { int dx; int dy; Graphics g=this.getGraphics(); for(int i=0;isp.x2)dx=sp.x2; if(sp.y1&gt;sp.y2)dy=sp.y2; if(sp.shape==ShapeParam.line) {g.setColor(sp.c); g.drawLine(sp.x1, sp.y1, sp.x2, sp.y2); System.out.println("l"); } if(sp.shape==ShapeParam.rec) {g.setColor(sp.c); g.draw3DRect(dx, dy, Math.abs(sp.x1-sp.x2), Math.abs(sp.y1-sp.y2), true); System.out.println("r"); } if(sp.shape==ShapeParam.oval) {g.setColor(sp.c); g.drawOval(dx, dy, Math.abs(sp.x1-sp.x2), Math.abs(sp.y1-sp.y2)); System.out.println("o"); } } } class Btn1Action implements ActionListener{ public void actionPerformed(ActionEvent arg0) { x1=x2=y1=y2=0; shapeparams.removeAll(shapeparams); TestOne.this.repaint(); } } class MousePain implements MouseMotionListener { public void mouseDragged(MouseEvent e) { TestOne.this.x2=e.getX(); TestOne.this.y2=e.getY(); TestOne.this.repaint(); } public void mouseMoved(MouseEvent e) { } } class ShapeParam{ static final int line=0; static final int rec=1; static final int oval=2; int x1=0; int y1=0; int x2=0; int y2=0; int shape; Color c; public ShapeParam(int x1,int y1,int x2,int y2,Color c,int shape){ this.x1=x1; this.y1=y1; this.x2=x2; this.y2=y2; this.c=c; this.shape=shape; } public String toString(){ return String.valueOf(x1)+" "+x2+" "+y1+" "+y2+" "+shape; } } } // // \[/code]