我试图每次按下四个按钮之一时改变图形。 这个数字每次运行文件都会改变,但是当我按下按钮时我不知道如何改变它。
是否有我可以调用重画StrokePanel的方法,还是我还需要做其他的事情?如何在java中重绘()StrokePanel
这是到目前为止的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.border.LineBorder;
public class GeomtryQuiz extends JApplet{
boolean playing = true;
static int chosen = 0;
static boolean answer = false;
static boolean change = false;
public static void main(String[] args){
JFrame frame = new JFrame();
frame.setTitle("Geometry Quiz");
frame.setSize(1024,900);
frame.setBackground(Color.white);
JApplet applet = new GeomtryQuiz();
applet.init();
JButton[] buttons = new JButton[4];
buttons[0] = new JButton("Triangle");
buttons[1] = new JButton("Square");
buttons[2] = new JButton("Pentagon");
buttons[3] = new JButton("Hexagon");
frame.getContentPane().add(applet);
Container c = frame.getContentPane();
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,2));
c.setLayout(new BorderLayout());
for(int i = 0 ; i < 4; i++){
buttons[i].setPreferredSize(new Dimension(70,70));
buttons[i].setBackground(Color.pink);
final JButton b = buttons[i];
final int angle = i;
final int n = 3;
b.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if(b.isEnabled()){
control(angle);
}
if(!b.isEnabled()){
}
}
}
);
p.add(buttons[i]);
}
c.add(p,BorderLayout.SOUTH);
c.add(applet,BorderLayout.CENTER);
frame.setVisible(true);
}
public static void control(int i){
if((i + 3) == chosen){
JOptionPane.showMessageDialog(null,"Correct");
}
else{
JOptionPane.showMessageDialog(null,"Wrong");
}
change = true;
}
public void init() {
JPanel panel = new StrokePanel();
getContentPane().add(panel);
}
class StrokePanel extends JPanel {
public StrokePanel() {
setPreferredSize(new Dimension(1024,800));
setBackground(Color.white);
}
public void paintComponent(Graphics f){
Graphics2D g = (Graphics2D)f;
Point2D center = new Point2D.Float(512,250);
float radius = 1000;
float[] dist = {0.1f, 0.2f, 1.0f};
Color[] colors1 = {Color.white, Color.white, Color.red};
RadialGradientPaint p = new RadialGradientPaint(center, radius, dist, colors1);
g.setPaint(p);
g.fillRect(0,0,1024,800);
int random = (int)(Math.random()*4 + 3);
chosen = random;
drawShape(g,random);
}
public void drawShape(Graphics g,int numri_brinjeve)
{
Graphics2D g2 = (Graphics2D)g;
g2.setStroke(new BasicStroke(2.0f));
g.setColor(Color.pink);
g2.translate(512,250);
double kendi = 360.0/numri_brinjeve;
GeneralPath path = new GeneralPath();
double a = 0;
double c = 0;
double x = 0;
double y = 0;
double r = 150;
for(int i = 1 ; i <= numri_brinjeve + 1 ; i++) {
x = r* Math.cos(Math.toRadians(i*kendi));
y = r* Math.sin(Math.toRadians(i*kendi));
a = x;
c = -y;
if(i == 1)
path.moveTo(a, c);
// System.out.println(i + ", " + a + ", " + c + "," + i*kendi);
path.lineTo(a, c);
}
g2.fill(path);
g2.setColor(Color.lightGray);
Stroke stroke = new BasicStroke(7);
g2.setStroke(stroke);
g2.draw(path);
}
}
}
你已经把'的System.out.println()'语句看到的代码是一个分支服用? – CraigR8806
我把这段代码放到IDE中,跳到我的第一件事是这些行,这会导致错误:'applet = new GeomtryQuiz();' 'Japplet applet.init();'因为applet是没有在上面声明。 – Sebastian
对不起,我在两个单独的文件中工作,并没有检查。但我不是在寻找,但无论如何感谢。 –