javademoframe.java

来自「有关java的源程序,为讲授java程序设计课程使用」· Java 代码 · 共 1,061 行 · 第 1/2 页

JAVA
1,061
字号

	private void displayImage(Graphics g)
	{
		if (!m_imageAllLoaded)
			return;
// Draw Image in the Right Corner of Frame
		g.drawImage(m_Images[m_nCurrImage],
				    size().width - m_nImgWidth*2,m_nImgHeight, this);	//or null
	}
}

class ColorDialogFrame extends Frame
{
	IDD_COLORDIALOG colorDialog;
	int redColor;
	int greenColor;
	int blueColor;
	Color backGround;	
	public ColorDialogFrame(String titleString)
	{
		super(titleString);
		//setTitle("This Is Find out Text About Frame Window");
		
		setCursor(Frame.MOVE_CURSOR);
// set The Dialog and Frame Font
		setFont(new Font("Helvetica",Font.PLAIN,14));		
		setBackground(Color.lightGray);
		setForeground(Color.blue);
		pack();

		redColor=JavaDemo.frameRed;
		greenColor=JavaDemo.frameGreen;
		blueColor=JavaDemo.frameBlue;

		colorDialog=new IDD_COLORDIALOG(this);
		colorDialog.CreateControls();
// set Dialog SubControl initial State value
		colorDialog.IDC_SCROLLBARRED.setValues(0,16,0,255);
		colorDialog.IDC_SCROLLBARRED.setValue(redColor);

		colorDialog.IDC_SCROLLBARGREEN.setValues(0,16,0,255);
		colorDialog.IDC_SCROLLBARGREEN.setValue(greenColor);

		colorDialog.IDC_SCROLLBARBLUE.setValues(0,16,0,255);
		colorDialog.IDC_SCROLLBARBLUE.setValue(blueColor);
		
// Show The Dialog & Its Frame Window
		setResizable(false);
		show();

	}

	public boolean handleEvent(Event event)
	{
		switch (event.id)
		{
			// Application shutdown (e.g. user chooses Close from the system menu).
			//------------------------------------------------------------------
			case Event.WINDOW_DESTROY:
				// TODO: Place additional clean up code here
				hide();
				dispose();
				break;
			case Event.SCROLL_LINE_UP:
			case Event.SCROLL_LINE_DOWN:
	        case Event.SCROLL_PAGE_UP:
			case Event.SCROLL_PAGE_DOWN:
			case Event.SCROLL_ABSOLUTE:
				
				if(event.target==colorDialog.IDC_SCROLLBARRED)
				{
					redColor=((Integer)event.arg).intValue();			
					if(redColor>255)
							redColor=255;
				}
				else if(event.target==colorDialog.IDC_SCROLLBARGREEN)
				{
					greenColor=((Integer)event.arg).intValue();			
					if(greenColor>255)
							greenColor=255;
				}
				else if(event.target==colorDialog.IDC_SCROLLBARBLUE)
				{
					blueColor=((Integer)event.arg).intValue();						
					if(blueColor>255)
							blueColor=255;
				}
				repaint();
				break;
		}	
			return super.handleEvent(event);
	}

	public boolean action(Event event,Object obj)
	{
		if(event.target==colorDialog.IDCANCEL)
		{
			hide();
			dispose();
		}							
		else if(event.target==colorDialog.IDOK)
		{
	 		JavaDemo.appletBackGround=new Color(redColor,greenColor,blueColor);		
			hide();
			dispose();
		}

		return false;
	}
	
	public void paint(Graphics g)
	{
		Dimension rectSize=size();			
		Color foreGround=getForeground();
		g.setColor(Color.red);
		g.draw3DRect(0,0,rectSize.width-1,rectSize.height-1,true);
		g.draw3DRect(3,3,rectSize.width-7,rectSize.height-7,false);
		g.setColor(foreGround);

		foreGround=new Color(redColor,greenColor,blueColor);		
		g.setColor(foreGround);
		g.clipRect(colorDialog.IDC_STATIC10.location().x,
			       colorDialog.IDC_STATIC10.location().y+10,
				   colorDialog.IDC_STATIC10.preferredSize().width,
				   colorDialog.IDC_STATIC10.preferredSize().height*4);
		g.fillRect(g.getClipRect().x,g.getClipRect().y,
				   g.getClipRect().width,g.getClipRect().height);	
	}
}

class RedBlueSwapFilter extends RGBImageFilter
{
	public RedBlueSwapFilter()
	{
		canFilterIndexColorModel=true;
	}
	public int filterRGB(int x,int y,int rgb)
	{
		return ((rgb&0xff00ff00)|((rgb&0xff0000)>>16)
				|((rgb&0xff)<<16));
	}
}

class ExitDialogFrame extends Frame implements Runnable
{
	
	IDD_EXITDIALOG exitDialog;
	Image imageMap=null;
	Image filterImage=null;
	Point imageTopPoint;
	boolean isDown=true;
	Thread exitThread=null;
	int stringX;
	int stringY;	
	Dimension rectSize;
	int imageWidth;
	int imageHeight;

	public ExitDialogFrame(String titleString)
	{
		super(titleString);
		//setTitle("This Is Find out Text About Frame Window");
		setCursor(Frame.TEXT_CURSOR);
// set The Dialog and Frame Font
		setFont(new Font("Helvetica",Font.PLAIN,14));		
		setBackground(Color.lightGray);
		setForeground(Color.blue);
		pack();

		exitDialog=new IDD_EXITDIALOG(this);
		exitDialog.CreateControls();
// set Dialog SubControl initial State value
		
		
// Show The Dialog & Its Frame Window
		setResizable(false);
		show();
		String imageFileName="images1\\img01.gif";
		try
		{
		imageMap=loadImage(this,imageFileName);
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Don't Find Image File:"+
				System.getProperty("user.dir")+"\\"+imageFileName);
		}
//fliter the Image data
		if(imageMap!=null)
		{
			imageWidth=imageMap.getWidth(this);
			imageHeight=imageMap.getHeight(this);
		    ImageFilter colorFilter=new RedBlueSwapFilter();
		    filterImage=createImage(new FilteredImageSource(
			   		                imageMap.getSource(),colorFilter));
		}
		if(exitThread==null)
			exitThread=new Thread(this);
		exitThread.start();

		rectSize=size();			
  	    stringX=rectSize.width;
	}

	public boolean handleEvent(Event event)
	{
		switch (event.id)
		{
			// Application shutdown (e.g. user chooses Close from the system menu).
			//------------------------------------------------------------------
			case Event.WINDOW_DESTROY:
				// TODO: Place additional clean up code here
				hide();
				dispose();
				break;
		}	
		return super.handleEvent(event);
	}

	public boolean action(Event event,Object obj)
	{
		if(event.target==exitDialog.IDCANCEL)
		{
			hide();
			dispose();
		}							
		else if(event.target==exitDialog.IDOK)
		{
				dispose();
				System.exit(0);
		}
		return false;
	}
	
	public boolean mouseDown(Event event,int x,int y)
	{
		imageTopPoint=
	new Point((this.preferredSize().width-imageWidth*2)/3,
			  (this.preferredSize().height-imageHeight)/2);

		if((x>imageTopPoint.x-2)&&(x<imageTopPoint.x+imageWidth+4)
			&&(y>imageTopPoint.y-2)&&(y<imageTopPoint.y+imageHeight+4))
		{
			isDown=!isDown;
		    repaint();							
		}
		return true;
	}
	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		long startTime=System.currentTimeMillis();

		while(Thread.currentThread()==exitThread)
		{
			if(stringX<4)
				stringX=rectSize.width;
			repaint();
			stringX=stringX-5;
			try
			{
				startTime=startTime+500;
				Thread.sleep(Math.max(0,
					startTime-System.currentTimeMillis()));
			}
			catch(InterruptedException e)
			{
			}
		}
	}

	public void paint(Graphics g)
	{
		g.setColor(Color.red);
		g.draw3DRect(0,0,rectSize.width-1,rectSize.height-1,true);
		g.draw3DRect(3,3,rectSize.width-7,rectSize.height-7,false);
		g.setColor(getForeground());
		if(imageMap!=null)
		{
			imageTopPoint=
	new Point((this.preferredSize().width-imageWidth*2)/3,
			  (this.preferredSize().height-imageHeight)/2);

			g.setColor(getBackground());
			g.draw3DRect(imageTopPoint.x-2,imageTopPoint.y-2,
				   imageWidth+4,imageHeight+4,isDown);
//draw the filterd Image Rectangle Line
 			g.draw3DRect((imageTopPoint.x*2+imageWidth)-2,imageTopPoint.y-2,
				   imageWidth+4,imageHeight+4,!isDown);
 			g.setColor(getForeground());
		}
	}

	public void update(Graphics g)
	{
//display image	
		if(imageMap!=null)
		{
		Image ramImage1=createImage(imageWidth,imageHeight);
		Graphics imageG1=ramImage1.getGraphics();
		imageG1.drawImage(imageMap,0,0,imageWidth,imageHeight,this); //this
		g.drawImage(ramImage1,imageTopPoint.x,imageTopPoint.y,this);//null
//draw the filtered Image		
		Image ramImage2=createImage(imageWidth,imageHeight);
		Graphics imageG2=ramImage2.getGraphics();
		imageG2.drawImage(filterImage,0,0,imageWidth,imageHeight,this); //this
		g.drawImage(ramImage2,imageTopPoint.x*2+imageWidth,
			imageTopPoint.y,this);			//null

//display moving String
		FontMetrics fm=getFontMetrics(getFont());
     	if(fm==null)
	         stringY=rectSize.height-50;	
	    stringY=rectSize.height-fm.getHeight()*2;
//clear string background color
		g.setColor(getBackground());
 		g.fillRect(1,stringY-fm.getHeight(),rectSize.width-4,
			                  fm.getHeight()*2);
		g.setColor(getForeground());
//again display string
		g.drawString("Hello, Friend !",stringX,stringY);
		}
	}

	public static Image loadImage(Component compon,String fileName)
					throws FileNotFoundException
	{
		boolean  m_fAllLoaded=false;

		File file=new File(fileName);
		if(!file.exists())
			throw new FileNotFoundException(fileName);
		MediaTracker mt=new MediaTracker(compon);
//Can Use the default toolkit---Toolkit.getDefaultToolkit().getImage(fileName);
		Image img= compon.getToolkit().getImage(fileName);
		mt.addImage(img,0);
		try
		{
			mt.waitForAll();
			m_fAllLoaded = !mt.isErrorAny();
		}
		catch(InterruptedException e)
		{
			System.out.println("Load Image File "+fileName+" Error !");
		}
		if(!m_fAllLoaded)
			img=null;

        return img;
	}
	public Image createDisabledImage(Image img,Component comp)
	{
		int w=img.getWidth(comp);
		int h=img.getHeight(comp);
		int pixels[]=new int[w*h];
		int emb[]=new int [w*h];
		PixelGrabber pg=new PixelGrabber(img,0,0,w,h,pixels,0,w);
		try
		{
			pg.grabPixels();
		}
		catch(InterruptedException e)
		{
			return null;
		}
		if((pg.status()&ImageObserver.ABORT)!=0)
			return null;
		Color bg=comp.getBackground();
		Color darker=bg.darker();
		Color brighter=bg.brighter();
		for(int i=0;i<w*h;i++)
			emb[i]=bg.getRGB();
		bg=new Color(pixels[0]);
		for(int j=0;j<h;j++)
			for(int i=0;i<w;i++)
			if(!bg.equals(new Color(pixels[j*w+i])))
			{
				if((i<w-1)&&(j<h-1))
					emb[(j+1)*w+i+1]=brighter.getRGB();
				emb[j*w+i]=darker.getRGB();
			}
		return comp.createImage(new MemoryImageSource(w,h,emb,0,w));
	}
}


class BlinkTextFrame extends Frame implements Runnable
{
	Thread blinkTextThread=null;
	int stringX;
	int stringY;	
	int startX;
	Dimension rectSize;
	String dispText="Hello, My Friend !";
	FontMetrics fm;
	int textLength;

	public BlinkTextFrame(String titleString,int frameWidth,int frameHeight)
	{
		super(titleString);
		Font font=new Font("Helvetica",Font.PLAIN,50);
		setFont(font);
		fm=getFontMetrics(font);
		textLength=fm.stringWidth(dispText);
		setBackground(Color.lightGray);
		pack();
			
// Show The Dialog & Its Frame Window
		show();
		resize(frameWidth,frameHeight);
		setResizable(false);
		rectSize=size();
		startX=(rectSize.width-textLength)/2;

		if(blinkTextThread==null)
			blinkTextThread=new Thread(this);
		blinkTextThread.start();
	}

	public boolean handleEvent(Event event)
	{
		switch (event.id)
		{
			case Event.WINDOW_DESTROY:
				hide();
				dispose();
				break;
		}	
		return super.handleEvent(event);
	}

	public boolean action(Event event,Object obj)
	{
		return false;
	}
	
	public void run()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		long startTime=System.currentTimeMillis();
		stringX=startX;
		
		while(Thread.currentThread()==blinkTextThread)
		{
			if(stringX>=(startX+textLength))
				stringX=startX;
			repaint();
			stringX+=10;
			try
			{
				startTime=startTime+100;
				Thread.sleep(Math.max(0,
					startTime-System.currentTimeMillis()));
			}
			catch(InterruptedException e)
			{
			}
		}
	}

	public void paint(Graphics g)
	{
		update(g);	
	}

	public void update(Graphics g)
	{
		g.setColor(Color.red);
		g.drawString(dispText,startX,rectSize.height/2+fm.getHeight());
		g.clipRect(stringX,rectSize.height/2,textLength,rectSize.height/4);
		g.setColor(Color.green);
		g.drawString(dispText,startX,rectSize.height/2+fm.getHeight());
	}
}

class CardTestFrame extends Frame
{
	Panel cardPanel;
	final static String BUTTONPANEL="panel with buttons";
	final static String TEXTPANEL="panel with Textfield";
	public CardTestFrame()
	{
		setLayout(new BorderLayout());
		setFont(new Font("Helvetica",Font.PLAIN,14));
		Panel cp=new Panel();
		Choice c=new Choice();
		c.addItem(BUTTONPANEL);
		c.addItem(TEXTPANEL);
		cp.add(c);
		add("North",cp);
		cardPanel=new Panel();
		cardPanel.setLayout(new CardLayout());
		Panel p1=new Panel();
		p1.add(new Button("button 1"));
		p1.add(new Button("button 2"));
		Panel p2=new Panel();
		p2.add(new Label("Input:"));
		p2.add(new TextField("TextField",20));
		cardPanel.add(BUTTONPANEL,p1);
		cardPanel.add(TEXTPANEL,p2);
		add("Center",cardPanel);

		setTitle("CardLayout Test");
		pack();
		show();
		resize(400,300);
		setResizable(false);
	}
	public boolean action(Event event,Object obj)
	{
		if(event.target instanceof Choice)
		{
			((CardLayout)cardPanel.getLayout()).show(cardPanel,(String)obj);
			return true;
		}
		return false;
	}

	public boolean handleEvent(Event event)
	{
		switch (event.id)
		{
			case Event.WINDOW_DESTROY:
				hide();
				dispose();
				break;
		}	
		return super.handleEvent(event);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?