Applet of a drawing board

2010-08-29  来源:本站原创  分类:Java  人气:244 

/* Use the Applet to do a drawing board, but do not know why draw a circle and party time  , Original drawing of the graph will change location  

 If anyone can help me thank  !

 Want to master the minor in this message  !

*/

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=" I love you, my home is in Beijing  !";
 int x1,y1,x2,y2,currentshape=ShapeParam.line;
 Color c=Color.black;
 CheckboxGroup cbg = new CheckboxGroup();
 CheckboxGroup shapecbg=new CheckboxGroup();
    ArrayList<ShapeParam> shapeparams=new ArrayList<ShapeParam>();

 @Override
 public void destroy() {
  System.out.println("destroy");
 }

 @Override
 public void init() {
  Button btn1=new Button(" Screen clearing  ");
  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(" Line  ",shapecbg,true);
   Checkbox shapecb2=new Checkbox(" Circle  ",shapecbg,false);
   Checkbox shapecb3=new Checkbox(" Party  ",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>x2)xx=x2;
     if(y1>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()==" Line  ")currentshape=ShapeParam.line;
  if(shapecbg.getSelectedCheckbox().getLabel()==" Party  ")currentshape=ShapeParam.rec;
  if(shapecbg.getSelectedCheckbox().getLabel()==" Circle  ")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;i<shapeparams.size();i++){

   ShapeParam sp=shapeparams.get(i);

   //System.out.println(sp.toString());
   dx=sp.x1;
   dy=sp.x2;
   if(sp.x1>sp.x2)dx=sp.x2;
      if(sp.y1>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;
  }
 }

}

//<applet code="TestOne.class" width=400 height=400>

//</applet>
相关文章
  • Applet of a drawing board 2010-08-29

    /* Use the Applet to do a drawing board, but do not know why draw a circle and party time , Original drawing of the graph will change location If anyone can help me thank ! Want to master the minor in this message ! */ import java.applet.Applet; impo

  • How to make a simple drawing board 2010-08-01

    Implementation of a simple drawing board Introduction: As just learning Java soon, so very little to do Sketchpad features. Can only draw some of the interface circle, rectangle, straight line, but also function with a pencil. But you can change the

  • Flex drawing board to achieve - 2010-10-15

    As the project requirements, the need to develop a Flex based whiteboard and document sharing capabilities. The Internet to find the one, basically a complete whiteboard and document sharing on the example code, so just to write one, and here the bas

  • The second java program - drawing board 2010-11-27

    I. Features: 1. To achieve draw lines, circles, rectangles, four rubber shapes. Drawing mode: hold down the mouse button drag, and then released, and Windows, similar to the way drawing; 2. Can select pen color; 3. According to the user to specify th

  • Summary drawing board 2010-12-03

    Originally wanted to write something with synthesis, but found that the problem is too many, or whether each item separately enumerate the things and issues which a summary. Drawing board Summary 1. Sketchpad in the production process, the first comp

  • Magpie Plum drawing board three (Scene Assembly) 2010-04-07

    [1b] 3, scene assembly [/ 1b] Increase of five layers. Name from the top down: music, borders, text, floating flower, plum, background. 1, the background layer: Rectangle tool, brush color ban, fill color: F7F7F7 painting 550 * 400 rectangle, the who

  • Flex drawing board (turn) 2010-04-14

    <? Xml version = "1.0" encoding = "utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"> <mx:Script> <! [CDATA [ import mx.bin

  • flex accustomed to drawing board (Reprinted) 2010-06-07

    <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"> <mx:Script> <![CDATA[ import mx.binding.ut

  • JAVA2D learning can drag the selection box drawing 2010-03-19

    Get a day, basically the general drawing software drawing a rectangle selection box functionality. Drag a rectangle with the dotted line to adjust the size of selected area Release the mouse to complete the selection, change the rectangular selected

  • Prototype map using Visio drawing 2010-08-20

    http://blog.luwenjin.com/index.php/2009/03/26/visio-wireframe-prototype-tutorial/ Some settings ... ... visio professional, as a general-purpose drawing tool, the default setting is not very suitable for designing web / application prototypes, after

  • JS drawing class (pure DIV drawing) 2010-08-20

    Written long ago to a drawing class, when the gallery VML and SVG is not very popular, the initial inspiration and the graph algorithm called w_jsGraphics.js from a class library. With a point to draw out the world. Very passionate to commemorate tha

  • Introduction to the fourteenth chapter of the drawing Android 2010-12-18

    Common control that a lot, and now also used to talk about mobile phone development to the drawing. Android to master the drawing, we must first look at the basic use of the graphical interface: 1.Bitmap, can come from the resources / documents can a

  • flex online game development - Siguojunqi game: (b) checker board 2010-12-24

    Play too many games in the four countries, and now I intend to be a flex version of the game four countries, the following article and code is written by doing so, the time when I posted, maybe I have to reconstruct the code , but, if you are a devel

  • Windows CMD command Daquan 2010-03-26

    accwiz.exe> Accessibility Wizard for walking you through setting up your machine for your mobility needs. aids Wizard acsetups.exe> ACS setup DCOM server executable actmovie.exe> Direct Show setup tool directly to the installation tool append.exe

  • Java programming experience (cattle were written) 2010-04-01

    To learn Java also has two years, and always dare not say how proficient, but also want to talk about their feelings, write software, school colleagues, to help people in the technology less detours on the road. Put great thing is our best hope for t

  • JBPM-line analysis of process design and implementation of part of the device concept 2010-03-10

    In an article before writing about blog statement, because my posts JE limited functionality, so we return the contents of the way, I do not reply to you online, hope forgive me. Need for technical communication, please add me Q: 17629634. JBPM more

  • Start → Run (cmd) command Daquan 2010-05-28

    gpedit.msc----- Group Policy sndrec32 ------- recorder Nslookup ------- IP address detector explorer ------- Open the Explorer logoff ----- ---- cancellation of orders tsshutdn ------- 60 seconds step shutdown command lusrmgr.msc---- This machine use

  • windows xp system CMD command Encyclopaedia (1) 2010-06-04

    About entries cmd-cmd.exe-Process Information Process File: cmd or cmd.exe Process Name: WindowsCommandPrompt Description: cmd.exe is a Microsoft Windows system, command-line program. similar to Microsoft's DOS operating system. cmd.exe is a 32-bit c

  • Daquan CMD command windows xp system 2010-06-18

    About entries cmd-cmd.exe-Process Information Process File: cmd or cmd.exe Process Name: WindowsCommandPrompt Description: cmd.exe is a Microsoft Windows system, command-line program. similar to Microsoft's DOS operating system. cmd.exe is a 32-bit c

  • Start → Run (cmd) command Daquan (absolute classic) (change) 2010-06-21

    CMD run command Start → Run → CMD → type the following command: gpedit.msc----- Group Policy sndrec32 ------- recorder Nslookup ------- IP address detector explorer ------- Open the Explorer logoff ----- ---- cancellation of orders tsshutdn ------- 6