2017-05-03 23 views
0

我试图每次按下四个按钮之一时改变图形。 这个数字每次运行文件都会改变,但是当我按下按钮时我不知道如何改变它。
是否有我可以调用重画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); 
    } 
} 
} 
+0

你已经把'的System.out.println()'语句看到的代码是一个分支服用? – CraigR8806

+0

我把这段代码放到IDE中,跳到我的第一件事是这些行,这会导致错误:'applet = new GeomtryQuiz();' 'Japplet applet.init();'因为applet是没有在上面声明。 – Sebastian

+0

对不起,我在两个单独的文件中工作,并没有检查。但我不是在寻找,但无论如何感谢。 –

回答

1

I'm trying to make the figure change everytime i press one of the four buttons

需要调用repaint引发新的油漆周期

意见...

extends JApplet是令人困惑的问题。作为一般规则,你不应该自己创建和操作一个Applet,它们应该由浏览器插件管理。

根据你的代码,我看不出有什么真正的理由来完成它,所以我会开始摆脱它。现在

,这将开始创造更多的问题,这凸显了问题,你很可能有

//JApplet applet = new GeomtryQuiz(); 
//applet.init(); 

frame.getContentPane().add(applet); 
//... 
c.add(p, BorderLayout.SOUTH); 
c.add(applet, BorderLayout.CENTER); 

好了,你先添加applet到内容窗格中,但随后又添加它以后.. 。?在这种情况下,这实际上是一个无操作,但可能不会导致问题的结束,请确保您只添加一次组件

但是,这引发了下一个问题:使用什么来代替小应用程序?那么,因为小程序唯一要做的就是将StrokePanel添加到它,那么很简单,只需使用StrokePanel而不是

这就是事情变得更加复杂的地方。

基本上,StrokePanel需要保持它的当前状态,所以chosen实际上应该是StrokePanel的属性,也chosen不应paintComponent内进行修改,这将导致没有问题的结束,因为paintComponent可以随时以任何数量的原因被调用,大多数情况下无需您的参与。所以,相反,你需要一些方法来设置和获取价值

StrokePanel

class StrokePanel extends JPanel { 

    private int chosen = 0; 

    public StrokePanel() { 
     setPreferredSize(new Dimension(1024, 800)); 
     setBackground(Color.white); 
    } 

    public int getChosen() { 
     return chosen; 
    } 

    public void setChosen(int chosen) { 
     this.chosen = chosen; 
     repaint(); 
    } 

    @Override 
    protected void paintComponent(Graphics f) { 
     super.paintComponent(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); 
     drawShape(g, getChosen()); 

    } 

    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); 
    } 
} 

GeomtryQuiz

我已经在这个重新工作,主要的不是一点点,我已删除JApplet并将核心应用程序移出static上下文(宠物讨厌)。

主要更新是update方法,它生成随机形状的包容和更新StrokePanel

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class GeomtryQuiz { 

    boolean playing = true; 
    boolean answer = false; 
    boolean change = false; 

    private StrokePanel strokePanel; 

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

    public GeomtryQuiz() { 

     JFrame frame = new JFrame(); 
     frame.setTitle("Geometry Quiz"); 
     frame.setSize(1024, 900); 
     frame.setBackground(Color.white); 

     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"); 

     Container c = frame.getContentPane(); 
     JPanel p = new JPanel(); 
     p.setLayout(new GridLayout(2, 2)); 

     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() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (b.isEnabled()) { 
         control(angle); 
        } 
        if (!b.isEnabled()) { 
        } 
       } 
      }); 

      p.add(buttons[i]); 
     } 
     c.add(p, BorderLayout.SOUTH); 

     strokePanel = new StrokePanel(); 
     c.add(strokePanel, BorderLayout.CENTER); 
     update(); 
     frame.setVisible(true); 
    } 

    protected void update() { 
     int last = strokePanel.getShape(); 
     int random = last; 
     do { 
      random = (int) (Math.random() * 4 + 3); 
     } while (random != last); 
     strokePanel.setShape(random); 
    } 

    public void control(int i) { 
     if ((i + 3) == strokePanel.getShape()) { 
      JOptionPane.showMessageDialog(null, "Correct"); 
      update(); 
     } else { 
      JOptionPane.showMessageDialog(null, "Wrong"); 

     } 
     change = true; 
    } 
} 
+0

谢谢你的一切,但我试过了代码但它不会运行。当我运行代码时,没有框架出现。 –

+0

根据我发布(和测试)的例子,它对我来说工作正常 – MadProgrammer