我想做一个游戏的地图编辑器,我的思想是把一个大图,用bufferedimage切成小图,然后,存到bufferedimage的数组里,然后,这个地图有放大和缩小功能,我的地图元素块是16x16,地图尺寸是64x64,然后我的地图有3层,我的地图块有500块,所以我的内存bufferimage[500],结果我运行了,内存虽然不占很多,CPU非常的耗资源,因为我放大显示后,就出现CPU资源达到90%多,我画图的放大是
g.drawImage(buf[i],0,0,buf[i].getWidht()*2,buf[i].getHeight()*2,null);一放大就出问题
我是把小的buf[i]图片画到一个大的宽800*600的大图片buf上了,结果,一放大就慢!
这个问题怎么解决啊!
g.drawImage(buf[i],0,0,buf[i].getWidht()*2,buf[i].getHeight()*2,null);一放大就出问题
我是把小的buf[i]图片画到一个大的宽800*600的大图片buf上了,结果,一放大就慢!
这个问题怎么解决啊!
从google map,你可以得到许多的思路。
我的方法是BufferedImage buf ;//图象缓冲区域
Graphics2D g2 = buf.getGraphics();
BufferedImage [50] bi;//那些小的地图块,地图元素,需要贴到buf上的.
void paint(Graphics g)
{
for(int i=0;i<map.高;i++)
{
for(int j=0;j<宽;j++)
g2.drawImage(bi[map[i][j],j*w,i*h,null);
} g.drawImage(buf,0,0,buf.宽度*2,buf.高度*2,null);//这个是放大的
}
交个朋友,我是开发手机游戏的,我的QQ286180684
将每个瓦片用单独的容器绘制,例如用各自独立的JLabel、JPanel等等。
而完整的图是一个大的容器,每个小容器在大容器中各自的位置。 对于每个小容器,只要更新它的图片的时候,赋予它值,然后它自己使用一个线程来更新数据。
这样的话,放大、缩小所需要等待的时间就少了很多。
比如:java RectJigsaw "C:\WINNT\Web\Wallpaper\Windows 2000.jpg"图片文件只能是gif或jpg// RectJigsaw.java
// author: unagainimport sun.awt.image.ToolkitImage;import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;import java.util.*;import javax.swing.*;
import javax.swing.border.*;public class RectJigsaw
extends JPanel{
final static int rowCount = 7;
final static int colCount = 8;
final static int HCTRL = 40;
final static int HMARGIN = 50;
final static int VMARGIN = 50;
private BufferedImage bi; static int imgWidth = 0, imgHeight = 0;
static int cellWidth = 0, cellHeight = 0;
static Vector<BufferedImage> images = new Vector<BufferedImage>();
static Vector<Integer> imgOrder = new Vector<Integer>(); public RectJigsaw(String fn) {
super();
setBackground(Color.BLACK);
setBorder(new BevelBorder(BevelBorder.LOWERED)); initCells(fn);
} private void initCells(String fn) {
final Image img = Toolkit.getDefaultToolkit().getImage(fn);
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
} catch (Exception e) {} bi = ((ToolkitImage) img).getBufferedImage(); bi = roundImgBounds(bi); Graphics2D g2 = (Graphics2D) bi.getGraphics();
int x = 0;
int y = 0; cellWidth = bi.getWidth() / colCount;
cellHeight = bi.getHeight() / rowCount; g2.setColor(Color.WHITE);
int k=0; for (int row=0; row<rowCount; row++) {
for (int col=0; col<colCount; col++) {
g2.draw3DRect(
x + cellWidth * col,
y + cellHeight * row,
cellWidth - 2, cellHeight - 2, true); imgOrder.add(k++); images.add(
bi.getSubimage(
x + cellWidth * col,
y + cellHeight * row,
cellWidth, cellHeight));
}
}
g2.setColor(Color.BLACK);
for (int col=0; col < colCount; col++) {
g2.drawLine(
x + cellWidth*(col+1) - 1,
y,
x + cellWidth *(col+1) - 1,
y + bi.getHeight() - 1);
} for (int row=0; row < rowCount; row++) {
g2.drawLine(
x,
y + cellHeight*(row+1) - 1,
x + bi.getWidth() - 1,
y + cellHeight*(row+1) - 1);
} }
public void paintComponent(Graphics g){
super.paintComponent(g); Graphics2D g2 = (Graphics2D) g;
int orgX = (getWidth() - imgWidth) / 2;
int orgY = (getHeight() - imgHeight) / 2; int i = -1; for (Integer index : imgOrder) {
i ++; int row = i / colCount;
int col = i % colCount; BufferedImage img = images.get(index.intValue()); g2.drawImage(
img,
orgX + col * cellWidth,
orgY + row * cellHeight,
this);
}
} private BufferedImage roundImgBounds(BufferedImage bi) { int height = bi.getHeight();
int width = bi.getWidth(); float cellHeight = Math.round(height / 1.0 / rowCount);
float cellWidth = Math.round(width / 1.0 / colCount); AffineTransform at = new AffineTransform(); imgWidth = (int)cellWidth * colCount;
imgHeight = (int)cellHeight * rowCount; at.scale(imgWidth / width,
imgHeight / height); AffineTransformOp biop =
new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage newbi = biop.filter(bi, null);
return newbi;
} public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
} JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel pane = new RectJigsaw(args[0]);
JPanel ctrlPane = new JPanel(); ctrlPane.setBorder(new LineBorder(Color.DARK_GRAY));
ctrlPane.add(
new JButton(
new AbstractAction("Restore"){
public void actionPerformed(ActionEvent e) {
Collections.sort(imgOrder);
pane.repaint();
}
}));
ctrlPane.add(
new JButton(
new AbstractAction("Shuffle") {
public void actionPerformed(ActionEvent e) { Collections.shuffle(imgOrder);
SwingUtilities.invokeLater(
new Runnable() {
public void run(){
pane.repaint();
}
}); }
})); frame.getContentPane().add(pane, BorderLayout.CENTER);
frame.getContentPane().add(ctrlPane, BorderLayout.PAGE_END); ctrlPane.setPreferredSize(
new Dimension(imgWidth + HMARGIN * 2, HCTRL));
frame.setPreferredSize(
new Dimension(imgWidth + HMARGIN * 2, imgHeight + VMARGIN * 2 + HCTRL)); frame.pack();
frame.setVisible(true);
}
}