JPanel 上的 Graphics2d

问题描述 投票:0回答:1

我想用Java编写一个程序,在鼠标所在的位置放置一个方块。为此,我想绘制一个网格作为背景,然后添加正方形,但是一旦我向

JFrame
添加新组件,背景就会消失。这也会以相反的方式发生,所以如果我添加一个正方形,然后添加背景,正方形就会消失。以下是代码。我现在的问题是如何添加第二个图形而不使第一个图形消失。如果我的英语不是很好,我也很抱歉,这不是我的母语。

import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

//Mainmethod
public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello World");
        MainFrame myFrame = new MainFrame();
        myFrame.init(); 
    }
}
//Window
public class MainFrame extends JFrame{
    public void init(){
        Image Logo; // Image for the window icon
        JPanel contentPane;
        Background background = new Background();
        Sand sand = new Sand();

        try {//sets the WidnowIcon
            Logo = ImageIO.read(new File("Logo.jpg"));
            setIconImage(Logo);
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        //JFrameconfiguratio     
        setTitle("Sandsim");
        setSize(600, 600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        //contentPaneconfiguration
        contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        contentPane.setVisible(true);
        setContentPane(contentPane);

        //adds Background and sand but you can only see the one that was added last
        contentPane.add(background);
        contentPane.add(sand);
    }
}
//draws the Bakground as grid of 10 by 10 squares
class Background extends JComponent {
    public void paintComponent(Graphics g) {

        int positionX = 0;
        int positionY = 0;

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.BLACK);


        for (int i = 0; i < getBounds().width / 10; i++) {
            g2d.drawLine(positionX, 0, positionX, getBounds().height);
            positionX += 10;
        }
        for (int i = 0; i < getBounds().height / 10; i++) {
            g2d.drawLine(0, positionY, getBounds().width, positionY);
            positionY += 10;
        }
    }
}
//draws a Sandcorn as 10 by 10 Rectangle
public class Sand extends JComponent{
    public void paintComponent(Graphics g){

        Graphics2D g2d =(Graphics2D)g;
        g2d.setColor(Color.YELLOW);
        g2d.fillRect(10, 10, 10, 10);

    }
}

我试图让它工作几个小时,但我找不到解决方案。我查看了 Oracle 并在互联网上搜索了一些教程,但它们总是可以工作,无需进行任何特殊调整或类似的操作。我真的很感激任何形式的帮助。

java swing jframe jpanel
1个回答
0
投票

这是您所描述的一个非常基本的示例。它执行以下操作:

  • 获取当前鼠标位置并将其转换为正确的网格位置。 如果鼠标位于 258,584,它会将其转换为 250, 580,这将是目标网格方块的左上角。
  • 在鼠标单击的网格上绘制一个黄色方块。它会绘制缩进 1 像素且大小为 9x9 的内容以适合网格。
  • 它还结合了上面的其他建议并消除了一些类。 我也忽略了徽标代码,尽管它有效。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class MainFrame extends JPanel {
    JFrame f = new JFrame();

    record Square(int row, int col) {
    }

    List<Square> squares = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        System.out.println("Hello World");
        MainFrame myFrame = new MainFrame();
        myFrame.init();
    }

    public void init() {

        // JFrameconfiguratio
        f.setTitle("Sandsim");
        f.add(this);
        addMouseListener(new MyMouseListener());
        setPreferredSize(new Dimension(600, 600));
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);

    }

    // draws the Bakground as grid of 10 by 10 squares

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        int positionX = 0;
        int positionY = 0;

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.BLACK);

        for (int i = 0; i < getBounds().width / 10; i++) {
            g2d.drawLine(positionX, 0, positionX, getBounds().height);
            positionX += 10;
        }
        for (int i = 0; i < getBounds().height / 10; i++) {
            g2d.drawLine(0, positionY, getBounds().width, positionY);
            positionY += 10;
        }

        g2d.setColor(Color.YELLOW);
        for (Square sq : squares) {
            g2d.fillRect(sq.row, sq.col, 9, 9);
        }
    }

    class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent me) {
            squares.add(new Square(me.getX()/10*10 + 1, me.getY()/10*10+1));
            f.repaint();
        }

    }

}
© www.soinside.com 2019 - 2024. All rights reserved.