简体   繁体   中英

Repaint() Method wiill not re-paint

I cannot get my repaint method to work in my SimonPanel class. At first, I thought it was because I used paint() instead of paintComponent(), but that didn't seem to solve the problem. My

SimonShape.java(Holds the frame and changes the colors of the shape)

public class SimonShape extends JFrame implements KeyListener {



private int level = 1;

// speed of the light up sequence
private int lightUpSpd = 500;

// chooses random color based on numbers 0-3
private int random;

// keeps track of user inputs 
private int compCounter = 0;

ArrayList<Integer> comp = new ArrayList<Integer>();

SimonPanel simon = new SimonPanel();
//SimonLabel keyLabel = new SimonLabel();

private Color blue = Color.BLUE.darker();
private Color red = Color.RED.darker();
private Color yellow = Color.YELLOW.darker();
private Color green = Color.GREEN.darker();


public SimonShape ()
{

    JLabel label = new JLabel();

    setSize(800,800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);


    simon.setFocusable(true);
    simon.setOpaque(true);
    simon.addKeyListener(this);
    this.add(simon);
    setVisible(true);
    simon.requestFocusInWindow();


    label.setFocusable(true);
    label.setOpaque(true);
    label.addKeyListener(this);
    this.add(label);
    setVisible(true);
    label.requestFocusInWindow();


    randomColorChange();
}


private void randomColorChange()
{

        JOptionPane.showMessageDialog(this, "Level " + level);
        random = (int) (Math.random() * 4);
        comp.add(random);
        //light up sequence
        for (int i = 0; i < level; i++)
        {

            if (comp.get(i) == 0)   simon.colorChange(0);
            else if (comp.get(i) == 1)  simon.colorChange(1);
            else if (comp.get(i) == 2)  simon.colorChange(2);
            else if (comp.get(i) == 3)  simon.colorChange(3);
        }
}

SimonPanel.java (Holds the shape)

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SimonPanel extends JPanel{

private int width = 500;
private int height = 500;
private int x = 150;
private int y = 150;
private int TURN = 45;


private SimonListener listener;
private Timer timer;

private Color blue = Color.BLUE.darker();
private Color red = Color.RED.darker();
private Color yellow = Color.YELLOW.darker();
private Color green = Color.GREEN.darker();

// speed of the light up sequence
  private int lightUpSpd = 500;

// chooses random color based on numbers 0-3
  private int random;

// keeps track of user inputs 
  private int compCounter = 0;


public SimonPanel()
{   

}

public void colorChange(int color)
{

    if (color == 0)
    {
        //lightUp();
        green.brighter();
        repaint();
        listener = new SimonListener(this,green);
        timer = new Timer(lightUpSpd,listener);
        System.out.println("green");
        timer.start();

    }
    else if (color == 1)
    {
        red.brighter();
        repaint();
        listener = new SimonListener(this,red);
        timer = new Timer(lightUpSpd,listener);
        System.out.println("red");
        timer.start();
    }
    else if (color == 2)
    {
        blue.brighter();
        repaint();
        listener = new SimonListener(this,blue);
        timer = new Timer(lightUpSpd,listener);
        System.out.println("blue");
        timer.start();
    }
    else if (color == 3)
    {
        yellow.brighter();
        this.repaint();
        listener = new SimonListener(this,yellow);
        timer = new Timer(lightUpSpd,listener);
        System.out.println("yellow");
        timer.start();
    }
}


public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    // Blue Section
    g2.setStroke(new BasicStroke(1.0f));
    g2.setPaint(blue);
    g2.fill(new Arc2D.Double(x,y,width,height,180+TURN,90,Arc2D.PIE));

    // Red Section
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(red);
    g2.fill(new Arc2D.Double(x,y,width,height,90+TURN,90,Arc2D.PIE));

    // Yellow Section
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(yellow);
    g2.fill(new Arc2D.Double(x,y,width,height,-90+TURN,90,Arc2D.PIE));

    // Green Section
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(green);
    g2.fill(new Arc2D.Double(x,y,width,height,360+TURN,90,Arc2D.PIE));

}
}

Test Class

public class SimonTest  {

public static void main(String[] args)
{
    new SimonShape();
}

  }

SimonListener.java

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;


public class SimonListener implements ActionListener {


private JPanel panel;
private Color color;



public SimonListener(JPanel panel, Color color)
{
    this.panel = panel;
    this.color = color;
}


public void setColor(Color newColor)
{
    color = newColor;
}

public void actionPerformed(ActionEvent e) 

{
    color.brighter();
    System.out.println("Called");
    panel.repaint();

    ((Timer) e.getSource()).stop();
}

}

Your "main" problem "seems" to the fact that you are not assiging the changes to the color objects back to anything;

green.brighter();

From the JavaDocs

Creates a new Color that is a brighter version of this Color.

You should be doing something more like

green = green.brighter();

I would also consider having a "base" color from which you can derive bright/darker colors from, but that's me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM