在Java中单击鼠标后如何重新粉刷?

时间:2019-05-30 17:10:49

标签: java mouseevent draw

单击鼠标后,图形应以新颜色重新绘制,但是没有。

我已经在组件和框架中实现了MouseListener,但是在两个版本中都没有用。

框架和MouseListener:

public class frame extends Frame
 {
     public static void main(String[] args)
        {

           JFrame frame = new JFrame();

           String string = new String("1  2  3  4  5  6  7");

           final int FRAME_WIDTH = 527;
           final int FRAME_HEIGHT = 77;

           frame.setAlwaysOnTop(true);
           frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
           frame.setTitle("Praxis");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setUndecorated(true);
           // JFrame zentriert positionieren; selbst berechnet:
           Toolkit tk = Toolkit.getDefaultToolkit();
           Dimension d = tk.getScreenSize();
           frame.setLocation((int) ((d.width)-700), (int) (0));

           Font font = new Font("Jokerman", Font.BOLD, 35);
           JLabel textLabel = new JLabel(string);
           textLabel.setFont(font);

           test component = new test();
           frame.add(component);

           MouseListener listen = new MouseListener()
           {
            public void mouseClicked(MouseEvent e) {
                GlobalVar obj = new GlobalVar();
                obj.fillColor1 = obj.farben[1];
                component.repaint();
            }
            @Override
            public void mousePressed(MouseEvent e) {
                // TODO Auto-generated method stub

            }
            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub

            }
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }
            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }
           };
           component.addMouseListener(listen);

           frame.setVisible(true);

        }

 }

他们从中获取颜色的文件:

public class GlobalVar{
    public static final Color[] farben = new Color[4];{ 
        farben[0] = Color.LIGHT_GRAY;
        farben[1] = Color.RED;
        farben[2] = Color.GREEN;
        farben[3] = Color.PINK;
    }
    Color fillColor1 = farben[0];
    Color fillColor2 = farben[0];
    Color fillColor3 = farben[0];
    Color fillColor4 = farben[0];
    Color fillColor5 = farben[0];
    Color fillColor6 = farben[0];
    Color fillColor7 = farben[0];
}

绘制图形的组件:

public class test extends JComponent
 { 
        GlobalVar obj = new GlobalVar();

     @Override
        public void paintComponent(Graphics g)
        {       

          if(g instanceof Graphics2D) {
           // Recover Graphics2D
           Graphics2D g2 = (Graphics2D) g;

           // Construct a rectangle and draw it
           Rectangle box1 = new Rectangle(2, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box1);
           g2.setColor(obj.fillColor1);
           g2.fill(box1);

           Rectangle box2 = new Rectangle(77, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box2);
           g2.setColor(obj.fillColor2);
           g2.fill(box2);

           Rectangle box3 = new Rectangle(152, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box3);
           g2.setColor(obj.fillColor3);
           g2.fill(box3);

           Rectangle box4 = new Rectangle(227, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box4);
           g2.setColor(obj.fillColor4);
           g2.fill(box4);

           Rectangle box5 = new Rectangle(302, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box5);
           g2.setColor(obj.fillColor5);
           g2.fill(box5);

           Rectangle box6 = new Rectangle(377, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box6);
           g2.setColor(obj.fillColor6);
           g2.fill(box6);

           Rectangle box7 = new Rectangle(452, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box7);
           g2.setColor(obj.fillColor7);
           g2.fill(box7);

           g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

           Font f = new Font("Dialog", Font.PLAIN, 30);
           g2.setFont(f);
           g2.setColor(Color.BLACK);
           g2.drawString("1", 30, 48);
           g2.drawString("2", 105, 48);
           g2.drawString("3", 180, 48);
           g2.drawString("4", 255, 48);
           g2.drawString("5", 330, 48);
           g2.drawString("6", 405, 48);
           g2.drawString("7", 480, 48);
          }

        }

        private Color fillColor1;
        private Color fillColor2;
        private Color fillColor3;
        private Color fillColor4;
        private Color fillColor5;
        private Color fillColor6;
        private Color fillColor7;
 }

正如我所说,我希望图形重新绘制时,第一个正方形将用红色填充,但是实际结果是,单击框架后什么也没有发生。

1 个答案:

答案 0 :(得分:0)

我没有让测试类扩展JComponent,而是将其放入JPanel,然后将JPanel放在JFrame之上。

然后,我将从JFrame中删除mouselistener并将其添加到JPanel上,通过这样做,唯一需要重绘的命令是repaint();。 我相信,这种方式比您目前正在做的更加简单,更有条理。

这应该可以解决问题(注意:我是在netbeans中做到的,这就是为什么有一些自动生成的代码的原因。):

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;

public class Test extends javax.swing.JPanel {

    /**
     * Creates new form Test
     */
    public Test() {
        initComponents();
    }
        GlobalVar obj = new GlobalVar();

     @Override
        public void paintComponent(Graphics g)
        {       

          if(g instanceof Graphics2D) {
           // Recover Graphics2D
           Graphics2D g2 = (Graphics2D) g;

           // Construct a rectangle and draw it
           Rectangle box1 = new Rectangle(2, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box1);
           g2.setColor(obj.fillColor1);
           g2.fill(box1);

           Rectangle box2 = new Rectangle(77, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box2);
           g2.setColor(obj.fillColor2);
           g2.fill(box2);

           Rectangle box3 = new Rectangle(152, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box3);
           g2.setColor(obj.fillColor3);
           g2.fill(box3);

           Rectangle box4 = new Rectangle(227, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box4);
           g2.setColor(obj.fillColor4);
           g2.fill(box4);

           Rectangle box5 = new Rectangle(302, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box5);
           g2.setColor(obj.fillColor5);
           g2.fill(box5);

           Rectangle box6 = new Rectangle(377, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box6);
           g2.setColor(obj.fillColor6);
           g2.fill(box6);

           Rectangle box7 = new Rectangle(452, 2, 71, 71);
           g2.setColor(Color.BLACK);
           g2.draw(box7);
           g2.setColor(obj.fillColor7);
           g2.fill(box7);

           g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

           Font f = new Font("Dialog", Font.PLAIN, 30);
           g2.setFont(f);
           g2.setColor(Color.BLACK);
           g2.drawString("1", 30, 48);
           g2.drawString("2", 105, 48);
           g2.drawString("3", 180, 48);
           g2.drawString("4", 255, 48);
           g2.drawString("5", 330, 48);
           g2.drawString("6", 405, 48);
           g2.drawString("7", 480, 48);
          }

        }

        private Color fillColor1;
        private Color fillColor2;
        private Color fillColor3;
        private Color fillColor4;
        private Color fillColor5;
        private Color fillColor6;
        private Color fillColor7;


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>                        

    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
                GlobalVar obj = new GlobalVar();
                obj.fillColor1 = obj.farben[1];
                repaint();
        // TODO add your handling code here:
    }                                 


    // Variables declaration - do not modify                     
    // End of variables declaration                   
}