我无法让我的重绘方法在我的SimonPanel课程中工作。起初,我认为这是因为我使用paint()而不是paintComponent(),但这似乎并没有解决问题。我的
SimonShape.java(保持框架并更改形状的颜色)
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(保持形状)
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));
}
}
测试类
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();
}
}
答案 0 :(得分:3)
你的“主要”问题似乎“似乎”你没有将颜色对象的变化分配回任何东西;
green.brighter();
来自JavaDocs
创建新颜色,这是此颜色的更亮版本。
你应该做更像
的事情green = green.brighter();
我还会考虑使用“基色”颜色,从中可以得到明亮/深色,但那就是我