gpt4 book ai didi

java - 需要帮助在 2 个彼此重叠的 Jpanels 上绘画

转载 作者:行者123 更新时间:2023-12-01 13:37:04 26 4
gpt4 key购买 nike

我正在为我 friend 正在制作的游戏创建 map 编辑器。游戏通过查看 png 文件来读取 map 。不同的十六进制颜色代码对应于不同的图 block 。现在,我让 map 编辑器按照游戏中显示的方式绘制 map (“预览”)。有没有一种方法可以将两个 JPanel 放在一起,这样只需单击一下即可在“预览”屏幕上绘制图 block ,并在第二个 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();
}
}

最佳答案

要执行您所描述的操作,有多种方法,其他答案涵盖了这一点。

有一种更好的方法,即使用单个面板来显示然后在该面板中渲染多个图像。您会发现这更加灵活并且性能更好。

关于java - 需要帮助在 2 个彼此重叠的 Jpanels 上绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21191277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com