我正在为我朋友制作的游戏创建一个地图编辑器。游戏通过查看png文件来读取地图。不同的十六进制颜色代码对应于不同的图块。现在,我让我的地图编辑器用它在游戏中显示的方式绘制地图(“预览”)。有没有办法可以将两个JPanel放在一起,所以只需一次点击就可以在“预览”屏幕上绘制图块,在第二个JPanel上绘制相应的颜色。我目前能够保存当前图像,我只想保存底部JPanel(非预览版)。
package com.socialdonut.spriteworld_editor;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main{
public static void main(String[] args) throws IOException {
Icon iconCT = new ImageIcon("cliffTop.png");
Icon iconCM = new ImageIcon("cliffMid.png");
Icon iconCB = new ImageIcon("cliffBot.png");
Icon iconW = new ImageIcon("water.png");
Icon iconG = new ImageIcon("grass.png");
Icon iconP = new ImageIcon("water.png");
JFrame frame = new JFrame("SpriteWorld Map Editor");
Container content = frame.getContentPane();
content.setLayout(new BorderLayout());
final PadDraw drawPad = new PadDraw();
content.add(drawPad, BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(32, 68));
panel.setMinimumSize(new Dimension(32, 68));
panel.setMaximumSize(new Dimension(32, 68));
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.clear();
}
});
JButton saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.save();
}
});
JButton cliffBottomButton = new JButton(iconCB);
cliffBottomButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.cliffBottom();
}
});
JButton cliffMidButton = new JButton(iconCM);
cliffMidButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.cliffMid();
}
});
JButton cliffTopButton = new JButton(iconCT);
cliffTopButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.cliffTop();
}
});
JButton blueButton = new JButton(iconW);
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.blue();
}
});
JButton greenButton = new JButton(iconG);
greenButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.green();
}
});
JButton pathButton = new JButton(iconP);
pathButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.path();
}
});
cliffMidButton.setPreferredSize(new Dimension(16, 16));
cliffTopButton.setPreferredSize(new Dimension(16, 16));
cliffBottomButton.setPreferredSize(new Dimension(16, 16));
blueButton.setPreferredSize(new Dimension(16, 16));
greenButton.setPreferredSize(new Dimension(16,16));
pathButton.setPreferredSize(new Dimension(16,16));
panel.add(greenButton);
panel.add(blueButton);
panel.add(pathButton);
panel.add(cliffTopButton);
panel.add(cliffMidButton);
panel.add(cliffBottomButton);
panel.add(clearButton);
panel.add(saveButton);
content.add(panel, BorderLayout.WEST);
frame.setSize(1366, 768);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class PadDraw extends JComponent{
Image image;
Image img;
Graphics2D graphics2D;
int currentX, currentY, oldX, oldY;
public PadDraw(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
}
});
addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
if(graphics2D != null) {
int displayX = (int) currentX/10, displayY = (int)currentY/10;
if (displayX%2 == 1)
displayX--;
if (displayY%2 == 1)
displayY--;
//graphics2D.fill(new Rectangle2D.Double(displayX, displayY, 2, 2));
graphics2D.drawImage(img, displayX, displayY, 2, 2, null);
}
repaint();
oldX = currentX;
oldY = currentY;
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
});
}
public void save() {
try {
ImageIO.write((RenderedImage) image, "PNG", new File("map1_back.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D)image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
AffineTransform tx1 = new AffineTransform();
tx1.scale(10, 10);
graphics2D.setTransform(tx1);
clear();
}
g.drawImage(image, 0, 0, null);
}
public void clear(){
graphics2D.setPaint(new Color(Integer.parseInt("000000", 16)));
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
repaint();
}
public void cliffBottom(){
//graphics2D.setPaint(new Color(Integer.parseInt("ca8e0c", 16)));
try {
img = ImageIO.read(new File("cliffBot.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
public void cliffMid(){
//graphics2D.setPaint(new Color(Integer.parseInt("ac790b", 16)));
try {
img = ImageIO.read(new File("cliffMid.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
public void cliffTop(){
//graphics2D.setPaint(new Color(Integer.parseInt("725006", 16)));
try {
img = ImageIO.read(new File("cliffTop.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
public void blue(){
//graphics2D.setPaint(new Color(Integer.parseInt("0000ff", 16)));
try {
img = ImageIO.read(new File("water.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
public void green(){
//graphics2D.setPaint(new Color(Integer.parseInt("00ff00", 16)));
try {
img = ImageIO.read(new File("grass.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
public void path(){
try {
img = ImageIO.read(new File("water.png"));
} catch (IOException e1) {
e1.printStackTrace();
}
repaint();
}
}
答案 0 :(得分:1)
要做你所描述的有很多方法,另一个答案就是这个。
有一种更好的方法,即使用单个面板显示然后在该面板中渲染多个图像。你会发现它更灵活,性能更好。
答案 1 :(得分:0)
您需要使用分层窗格和GlassPane http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html