0

3x3 棋盘

我在 JPanel 上呈现了一个 3x3 类似检查板的图像,该图像被添加到 JFrame 上。然后我还有 9 个 JPanel(每个正方形顶部有 1 个),点击时需要在相应的正方形上绘制一些东西。我的问题是它只适用于左上角的正方形。其余的图纸似乎是在棋盘图像下方绘制的。因此,如果我注释掉加载棋盘图像的部分,然后像它们在那里一样单击,那么图纸就会正确显示。我使用分层窗格得到相同的结果。使用绝对定位并且坐标似乎是正确的,因为如果我删除棋盘图像,那么图纸会出现在它们应该出现的位置,并且图纸不会超过一个正方形。我的代码结构如下:

'main' 类创建框架并添加另一个类的实例,该类扩展 JPanel 并使用paintComponent(Graphics g) 绘制棋盘图像。'main' 类还添加了 9 个实例,该类扩展 JPanel 并使用paintComponent(Graphics g)在鼠标单击时绘制一些东西。每个实例都放置在一个正方形的顶部

请注意,因为我打算只使用 Rectangles 我将第二类命名为 Rectangles 但它是矩形 JPanels 而不是 java Rectangle 实例

代码:

public class Main3
{
    private JFrame frame=new JFrame("");
    private Rectangles rect00=new Rectangles(0,0,129,129);
    private Rectangles rect01=new Rectangles(136,0,129,129);
    private Rectangles rect02=new Rectangles(268,0,129,129);
    private Rectangles rect10=new Rectangles(0,136,129,129);
    private Rectangles rect11=new Rectangles(134,136,129,129);
    private Rectangles rect12=new Rectangles(269,137,129,129);
    private Rectangles rect20=new Rectangles(0,270,129,129);
    private Rectangles rect21=new Rectangles(136,269,129,129);
    private Rectangles rect22=new Rectangles(269,270,129,129);
    
    public void Display()
    {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(600,400); 
        sub inter=new sub();
        inter.setLayout(null);
        inter.setBounds(0,0,600,400);
        inter.setSize(600,400);
    
        rect00.setBounds(rect00.getX(),rect00.getY(),rect00.getWidth(),rect00.getHeight());
        rect01.setBounds(rect01.getX(),rect01.getY(),rect01.getWidth(),rect01.getHeight());
        rect02.setBounds(rect02.getX(),rect02.getY(),rect02.getWidth(),rect02.getHeight());
        rect10.setBounds(rect10.getX(),rect10.getY(),rect10.getWidth(),rect10.getHeight());
        rect11.setBounds(rect11.getX(),rect11.getY(),rect11.getWidth(),rect11.getHeight());
        rect12.setBounds(rect12.getX(),rect12.getY(),rect12.getWidth(),rect12.getHeight());
        rect20.setBounds(rect20.getX(),rect20.getY(),rect20.getWidth(),rect20.getHeight());
        rect21.setBounds(rect21.getX(),rect21.getY(),rect21.getWidth(),rect21.getHeight());
        rect22.setBounds(rect22.getX(),rect22.getY(),rect22.getWidth(),rect22.getHeight());
        rect00.setOpaque(false);
        rect01.setOpaque(false);
        rect02.setOpaque(false);
        rect10.setOpaque(false);
        rect11.setOpaque(false);
        rect12.setOpaque(false);
        rect20.setOpaque(false);
        rect21.setOpaque(false);
        rect22.setOpaque(false);
        
        inter.add(rect00);
        inter.add(rect01);
        inter.add(rect02);
        inter.add(rect10);
        inter.add(rect11);
        inter.add(rect12);
        inter.add(rect20);
        inter.add(rect21);
        inter.add(rect22);
        frame.add(inter);
        frame.setResizable(false);
        frame.setVisible(true);     
    }
    
    public static void main(String args[])
    {
        new main().Display();
    }
    
    
    private class sub extends JPanel
    {
        
        private BufferedImage image;
        
        public sub ()
        {
             
            try
            {                 
                 image=ImageIO.read(new File("image.jpg"));
                        
            }
            catch (IOException e)
            {
             
                e.printStackTrace();
            }
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(600,400));
        }
        
        
        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);   
        }
        
       
    }
}

这是另一个类

public class Rectangles extends JPanel implements MouseListener
{

    private int Posx;
    private int Posy;
    private int width;
    private int height;
    private boolean selected=false;
    public Rectangles(int Posx,int Posy,int width,int height)
    {
          this.Posx=Posx;
          this.Posy=Posy;
          this.width=width;
          this.height=height;
          this.addMouseListener(this);     
    }
        
    @Override
    protected void paintComponent(Graphics g)
    {
        if(selected==true)
        {
            Graphics2D g2 = (Graphics2D) g;
               super.paintComponent(g2);
               g2.setColor(new Color(250, 235, 215));
               g2.drawRect(Posx,Posy,width,height);
               Graphics2D g3=(Graphics2D)g;
               g2.setColor(new Color(0,0,0));
               g3.setStroke(new BasicStroke(20));
               g3.drawLine(Posx,Posy,Posx+width,Posy+height);
               g3.drawLine(Posx+width,Posy,Posx,Posy+height);
           
        }   
        
    }
    
    public int getX()
    {
        return Posx;
    }
    
    public int getY()
    {
        return Posy;
    }
    
    
    public int getWidth()
    {
        return width;
    }
    
    
    public int getHeight()
    {
        return height;
    }
    
    
    public void setSelected()
    {
        selected=true;
    }
    
    @Override
    public void mouseClicked(MouseEvent arg0)
    {
        
    }
    
    @Override
    public void mouseEntered(MouseEvent arg0) 
    {
        
    }
         
    public void mouseExited(MouseEvent arg0) 
    {
        
    }
    @Override
    public void mousePressed(MouseEvent arg0) 
    {
        
    }
         
    @Override
    public void mouseReleased(MouseEvent arg0)
    {
        selected=true;
        repaint();
        
    }
}
4

1 回答 1

2

1)您不尊重组件油漆链

根据java文档paintComponent(Graphics g)

 此外,如果你不调用 super 的实现,你必须遵守 opaque 属性,也就是说,如果这个组件是不透明的,你必须用不透明的颜色完全填充背景。如果您不尊重 opaque 属性,您可能会看到视觉伪影。

2)super.paintComponent在大多数情况下是方法中的第一个调用。

3)但还有更多,您两次转换为 Graphics2D,不应该这样做:

Graphics2D g2 = (Graphics2D) g;
...
Graphics2D g3=(Graphics2D)g;

省略g3它不需要的你已经投射到一个Graphics2D对象

4) 另一个问题出在sub课堂上。您在主代码中执行此操作:

inter.add(rect00);
inter.add(rect01);
...

但在inter哪个是你的类实例的变量名,sub你只有:

@Override
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null);   
}

因此,无论您添加多少个矩形,它都只会绘制一个图像!

也不要这样做 ,因为 JPanel 已添加到坐标及其容器上,当您在 JPanel 上绘制时,我们g2.drawLine(Posx, Posy, Posx + width, Posy + height);希望 从左上角开始,即 0,0,更改值会将图像在其容器上进一步向下移动g2.drawLine(0, 0, Posx + width, Posy + height);xy

请参阅此处的固定代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    private JFrame frame = new JFrame("");
    private Rectangles rect00 = new Rectangles(0, 0, 129, 129);
    private Rectangles rect01 = new Rectangles(136, 0, 129, 129);
    private Rectangles rect02 = new Rectangles(268, 0, 129, 129);
    private Rectangles rect10 = new Rectangles(0, 136, 129, 129);
    private Rectangles rect11 = new Rectangles(134, 136, 129, 129);
    private Rectangles rect12 = new Rectangles(269, 137, 129, 129);
    private Rectangles rect20 = new Rectangles(0, 270, 129, 129);
    private Rectangles rect21 = new Rectangles(136, 269, 129, 129);
    private Rectangles rect22 = new Rectangles(269, 270, 129, 129);

    public void Display() {
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(600, 400);
        sub inter = new sub();
        inter.setLayout(null);
        inter.setBounds(0, 0, 600, 400);
        inter.setSize(600, 400);

        rect00.setBounds(rect00.getX(), rect00.getY(), rect00.getWidth(), rect00.getHeight());
        rect01.setBounds(rect01.getX(), rect01.getY(), rect01.getWidth(), rect01.getHeight());
        rect02.setBounds(rect02.getX(), rect02.getY(), rect02.getWidth(), rect02.getHeight());
        rect10.setBounds(rect10.getX(), rect10.getY(), rect10.getWidth(), rect10.getHeight());
        rect11.setBounds(rect11.getX(), rect11.getY(), rect11.getWidth(), rect11.getHeight());
        rect12.setBounds(rect12.getX(), rect12.getY(), rect12.getWidth(), rect12.getHeight());
        rect20.setBounds(rect20.getX(), rect20.getY(), rect20.getWidth(), rect20.getHeight());
        rect21.setBounds(rect21.getX(), rect21.getY(), rect21.getWidth(), rect21.getHeight());
        rect22.setBounds(rect22.getX(), rect22.getY(), rect22.getWidth(), rect22.getHeight());
        rect00.setOpaque(false);
        rect01.setOpaque(false);
        rect02.setOpaque(false);
        rect10.setOpaque(false);
        rect11.setOpaque(false);
        rect12.setOpaque(false);
        rect20.setOpaque(false);
        rect21.setOpaque(false);
        rect22.setOpaque(false);

        inter.addPanel(rect00);
        inter.addPanel(rect01);
        inter.addPanel(rect02);
        inter.addPanel(rect10);
        inter.addPanel(rect11);
        inter.addPanel(rect12);
        inter.addPanel(rect20);
        inter.addPanel(rect21);
        inter.addPanel(rect22);
        frame.add(inter);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        new Test().Display();
    }

    private class sub extends JPanel {

        private BufferedImage image;
        private ArrayList<Rectangles> rects = new ArrayList<>();

        public sub() {

            try {
                image = ImageIO.read(new File("c:/image.png"));

            } catch (IOException e) {

                e.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return (new Dimension(600, 400));
        }

        void addPanel(Rectangles r) {
            rects.add(r);
            add(r);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for (Rectangles r : rects) {
                g.drawImage(image, r.getX(), r.getY(), null);
            }
        }
    }
}

class Rectangles extends JPanel implements MouseListener {

    private int Posx;
    private int Posy;
    private int width;
    private int height;
    private boolean selected = false;

    public Rectangles(int Posx, int Posy, int width, int height) {
        this.Posx = Posx;
        this.Posy = Posy;
        this.width = width;
        this.height = height;
        this.addMouseListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (selected == true) {
            Graphics2D g2 = (Graphics2D) g;
            g2.setColor(new Color(250, 235, 215));
            g2.drawRect(0,0, width, height);
            g2.setColor(new Color(0, 0, 0));
            g2.setStroke(new BasicStroke(20));
            g2.drawLine(0,0, width,height);
            g2.drawLine(getWidth(),0, 0, height);
        }

    }

    public int getX() {
        return Posx;
    }

    public int getY() {
        return Posy;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public void setSelected() {
        selected = true;
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        selected = true;
        repaint();


    }
}

其他一些指针:

  • 不要使用Absolute/Null布局。A GridLayoutorGridBagLayout会很好地满足您的需求。(请参阅此处了解更多信息。)
  • 在设置可见之前不要JFrame#setSize(...);使用正确LayoutManager并调用。pack()JFrame
  • 不要调用setSize您的Rectangles实例,只需getPreferredSize像使用sub面板一样覆盖?
  • 无需实现MouseListener,只需使用即可MouseAdapter让您自由选择要覆盖的方法,而不仅仅是覆盖所有方法。
  • 阅读 Swing 中的并发,尤其是Event-Dispatch-Thread
于 2013-06-28T12:24:25.257 回答