gpt4 book ai didi

java - Repaint() 方法不会重新绘制

转载 作者:行者123 更新时间:2023-12-01 22:18:49 26 4
gpt4 key购买 nike

我无法让我的重绘方法在我的 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();
}

}

最佳答案

您的“主要”问题“似乎”是这样一个事实:您没有将对颜色对象的更改分配回任何内容;

green.brighter();

来自JavaDocs

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

你应该做一些类似的事情

green = green.brighter();

我还会考虑使用一种“基”颜色,您可以从中导出明亮/较暗的颜色,但这就是我

关于java - Repaint() 方法不会重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30386029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com