当前位置: 技术问答>java相关
各位老大,怎样写一个类来装图片,没有滚去条,
来源: 互联网 发布时间:2015-09-12
本文导语: 各位老大,怎样写一个类来装图片,没有滚去条,但是能装任何大小的图片。 谢谢! | package oasui; import javax.swing.JPanel; import java.awt.Image; import java.awt.Graphics; import java.awt.Toolkit; import java.a...
各位老大,怎样写一个类来装图片,没有滚去条,但是能装任何大小的图片。
谢谢!
谢谢!
|
package oasui;
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.MediaTracker;
import java.awt.Dimension;
import java.awt.Point;
public class ImageControl extends JPanel
{
private Image m_Image = null; //图象对象
private MediaTracker m_Tracker = null;
private boolean m_bFitSize = true; //是否自动调整到与该控件适合的尺寸
private Point m_Pos = new Point(0,0); //显示起始位置
public ImageControl()
{
super();
}
public ImageControl(byte[] imageData) throws Exception
{
super();
LoadImage(imageData);
}
public ImageControl(String sFile) throws Exception
{
super();
LoadImage(sFile);
}
public boolean LoadImage(byte[] imageData) throws Exception
{
Image image = Toolkit.getDefaultToolkit().createImage(imageData);
if(image == null)
throw new Exception("读取图形数据错误!");
m_Image = image;
if(m_Tracker != null)
m_Tracker = null;
m_Tracker = new MediaTracker(this);
m_Tracker.addImage(m_Image,0);
return true;
}
public boolean LoadImage(String sFile) throws Exception
{
Image image = Toolkit.getDefaultToolkit().createImage(sFile);
if(image == null)
throw new Exception("读取图形数据错误!");
m_Image = image;
if(m_Tracker != null)
m_Tracker = null;
m_Tracker = new MediaTracker(this);
m_Tracker.addImage(m_Image,0);
return true;
}
protected void paintComponent(Graphics g)
{
try
{
super.paintComponent(g);
if(m_Image != null)
{
if(!m_Tracker.checkID(0))
m_Tracker.waitForID(0);
if(m_bFitSize)
g.drawImage(m_Image,1,1,
getWidth() - 2,getHeight() - 2,
null);
else
g.drawImage(m_Image,m_Pos.x,m_Pos.y,null);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Dimension getImageSize()
{
if(m_Image == null)
return new Dimension(0,0);
try
{
if(!m_Tracker.checkID(0))
m_Tracker.waitForID(0);
return new Dimension(m_Image.getWidth(null),
m_Image.getHeight(null));
}
catch(Exception e)
{
e.printStackTrace();
}
return new Dimension(0,0);
}
public void setFitSize(boolean bFitSize)
{
m_bFitSize = bFitSize;
}
public boolean getFitSize()
{
return m_bFitSize;
}
public void setPos(Point pos)
{
m_Pos.x = pos.x;
m_Pos.y = pos.y;
}
public Point getPos()
{
return (Point)m_Pos.clone();
}
public void clear()
{
m_Tracker = null;
m_Image = null;
}
}
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.MediaTracker;
import java.awt.Dimension;
import java.awt.Point;
public class ImageControl extends JPanel
{
private Image m_Image = null; //图象对象
private MediaTracker m_Tracker = null;
private boolean m_bFitSize = true; //是否自动调整到与该控件适合的尺寸
private Point m_Pos = new Point(0,0); //显示起始位置
public ImageControl()
{
super();
}
public ImageControl(byte[] imageData) throws Exception
{
super();
LoadImage(imageData);
}
public ImageControl(String sFile) throws Exception
{
super();
LoadImage(sFile);
}
public boolean LoadImage(byte[] imageData) throws Exception
{
Image image = Toolkit.getDefaultToolkit().createImage(imageData);
if(image == null)
throw new Exception("读取图形数据错误!");
m_Image = image;
if(m_Tracker != null)
m_Tracker = null;
m_Tracker = new MediaTracker(this);
m_Tracker.addImage(m_Image,0);
return true;
}
public boolean LoadImage(String sFile) throws Exception
{
Image image = Toolkit.getDefaultToolkit().createImage(sFile);
if(image == null)
throw new Exception("读取图形数据错误!");
m_Image = image;
if(m_Tracker != null)
m_Tracker = null;
m_Tracker = new MediaTracker(this);
m_Tracker.addImage(m_Image,0);
return true;
}
protected void paintComponent(Graphics g)
{
try
{
super.paintComponent(g);
if(m_Image != null)
{
if(!m_Tracker.checkID(0))
m_Tracker.waitForID(0);
if(m_bFitSize)
g.drawImage(m_Image,1,1,
getWidth() - 2,getHeight() - 2,
null);
else
g.drawImage(m_Image,m_Pos.x,m_Pos.y,null);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Dimension getImageSize()
{
if(m_Image == null)
return new Dimension(0,0);
try
{
if(!m_Tracker.checkID(0))
m_Tracker.waitForID(0);
return new Dimension(m_Image.getWidth(null),
m_Image.getHeight(null));
}
catch(Exception e)
{
e.printStackTrace();
}
return new Dimension(0,0);
}
public void setFitSize(boolean bFitSize)
{
m_bFitSize = bFitSize;
}
public boolean getFitSize()
{
return m_bFitSize;
}
public void setPos(Point pos)
{
m_Pos.x = pos.x;
m_Pos.y = pos.y;
}
public Point getPos()
{
return (Point)m_Pos.clone();
}
public void clear()
{
m_Tracker = null;
m_Image = null;
}
}
|
不懂
滚去条?
装任何大小的图片??
什么环境啊?
滚去条?
装任何大小的图片??
什么环境啊?