礼花(菜鸟程序)之一

来源:互联网 发布:最新网络水军价格 编辑:程序博客网 时间:2024/04/28 07:02

包,类层次:

测试类DropTest

        包LiHua(含类:Position)

                 |

                 包Pen(含类:Drop)

 

*******************************************DropTest:********************************************************

import java.awt.*;
import java.awt.event.*;
import LiHua.Pen.*;
import LiHua.*;

class DropTest{
 Frame frame;
 Panel panel;
 LiHua.Pen.Drop drop;
 
 DropTest(){
  System.out.println("Test in DropTest");
  frame=new Frame();
  panel=new Panel();
  drop=new Drop();
  panel.setBackground(Color.blue);
  frame.add(panel);
  frame.setSize(300,300);
  frame.setVisible(true);
  frame.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    frame.dispose();
    System.exit(0);
   }
  });
 }
 
 public static void main(String args[]){
  DropTest test=new DropTest();
  test.drop.setColor(Color.red);
  test.drop.setSize(10);
  test.drop.draw(new Position(200,150),test.panel.getGraphics());
 }
}

 

***********************************************Position:***************************************************

package LiHua;

public class Position{
 int x;         //horizontal position
 int y;         //vertical position
 
 //default position is on origin point
 public Position(){
  //ok       System.out.println("test in Position"); // testing eeeee
  x=0;
  y=0;
 }
 
 //create it with given horizontal and vertical positons
 public Position(int x,int y){
  this.x=x;
  this.y=y;
 }
 
 //create it with given center,radius and angle
 public Position(Position center,int radius,int angle){
  x=radius*(int)Math.cos(angle)+center.getX();
  y=radius*(int)Math.sin(angle)+center.getY();
 }
 
 public int getX(){
  return x;
 }
 
 public int getY(){
  return y;
 }
 
 public void setX(int x){
  this.x=x;
 }
 
 public void setY(int y){
  this.y=y;
 }

 

*********************************************Drop:*******************************************************

/********************************************************
 * A full filled circle
 * which can be set with diferent colors and sizes
 ********************************************************/

package LiHua.Pen;
import java.awt.*;
import LiHua.*;

public class Drop{
  int size;       //the diameter of he Drop
  Color color;

  //if arguments are not given, the crytic diameter and color will be 5 and red
  public Drop(){
    size=5;
    color=new Color(255,0,0);
  }

  public Drop(int size){
   this.size=size;
   color=new Color(255,0,0);
  }

  public Drop(Color color){
    size=5;
    this.color=color;
  }

  public Drop(int size,Color color){
    this.size=size;
    this.color=color;
  }

  //draw the filled circle with the setted diameter and color
  public void draw(LiHua.Position position,Graphics g){
   
   System.out.println("Test in Drop.draw()");
   g.setColor(color);
    g.fillOval(position.getX(),position.getY(),size,size);
  }
 
  public void draw(Graphics g,LiHua.Position position){
   g.setColor(color);
    g.fillOval(position.getX(),position.getY(),size,size);
  }

  public String toString(){
    return "Drop:/n/tsize="+size+"/n/tcolor="+color;
  }
 
  public void setColor(Color color){
   this.color=color;
  }
 
  public void setSize(int size){
   this.size=size;
  }
}

***************************************Problemes:*****************************************************

现存问题:

        把窗口最小化后,再复原,红色实心圆消失。

---------------------------------------------------------------------------------------

曾出现问题:

        开始的DropTest用JFrame,JPanel,然后用JPanel panel.getGraphics()获得画笔作为Drop的画笔参数。测试发现圆点时而出现,时而不出现,有时在窗口打开瞬间闪烁以下就消失了。

        后来我想到可能getGraphics()是java.awt.Component的l类方法,或许用java.awt.Panel可以解决,于是换用Panel,果然圆点随即出现的问题解决了,但是很奇怪:javax.swing.JPanel也是java.awt.Component的子类 呀!用它为什么却会出现问题呢?


原创粉丝点击