Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中重新绘制()StrokePanel_Java_Awt - Fatal编程技术网

如何在java中重新绘制()StrokePanel

如何在java中重新绘制()StrokePanel,java,awt,Java,Awt,每次我按下四个按钮中的一个,我都试图改变这个数字。 每次运行文件时,该数字都会更改,但我不知道在按下按钮时如何更改该数字。 是否有一种方法可以调用以重新绘制StrokePanel,或者我必须执行其他操作 这是到目前为止的代码 import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.*; import javax.swing.border.LineBorder;

每次我按下四个按钮中的一个,我都试图改变这个数字。 每次运行文件时,该数字都会更改,但我不知道在按下按钮时如何更改该数字。
是否有一种方法可以调用以重新绘制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);
  }  
 }
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入java.awt.geom.*;
导入javax.swing.border.LineBorder;
公共类几何测验扩展JApplet{
布尔播放=真;
选择的静态int=0;
静态布尔答案=false;
静态布尔更改=false;
公共静态void main(字符串[]args){
JFrame=新JFrame();
frame.setTitle(“几何测验”);
帧设置大小(1024900);
框架.立根背景(颜色.白色);
JApplet applet=new geometryquick();
applet.init();
JButton[]按钮=新JButton[4];
按钮[0]=新的JButton(“三角形”);
按钮[1]=新的按钮(“方形”);
按钮[2]=新的JButton(“五角大楼”);
按钮[3]=新的JButton(“六边形”);
frame.getContentPane().add(小程序);
容器c=frame.getContentPane();
JPanel p=新的JPanel();
p、 setLayout(新的GridLayout(2,2));
c、 setLayout(新的BorderLayout());
对于(int i=0;i<4;i++){
按钮[i].setPreferredSize(新尺寸(70,70));
按钮[i].背景(颜色.粉色);
最终JButton b=按钮[i];
最终内倾角=i;
最终整数n=3;
b、 addActionListener(
新建ActionListener(){
已执行的公共无效操作(操作事件e){
如果(b.isEnabled()){
控制(角度);
}
如果(!b.isEnabled()){
}
}
}
);  
p、 添加(按钮[i]);
}
c、 添加(p,边界布局。南部);
c、 添加(applet,BorderLayout.CENTER);
frame.setVisible(true);
}
公共静态无效控制(int i){
如果((i+3)=选择){
showMessageDialog(null,“Correct”);
}
否则{
showMessageDialog(null,“错误”);
}
改变=正确;
}
公共void init(){
JPanel面板=新的StrokePanel();
getContentPane().add(面板);
}
类StrokePanel扩展了JPanel{
公共中风专家组(){
setPreferredSize(新维度(1024800));
挫折地面(颜色:白色);
}
公共组件(图形f){
图形2d g=(图形2d)f;
Point2D中心=新的Point2D.Float(512250);
浮动半径=1000;
float[]dist={0.1f、0.2f、1.0f};
Color[]colors1={Color.white,Color.white,Color.red};
RadialGradientPaint p=新的RadialGradientPaint(中心、半径、距离、颜色1);
g、 setPaint(p);
g、 fillRect(0,01024800);
int random=(int)(Math.random()*4+3);
选择=随机;
拉深形状(g,随机);
}
公共虚空绘图形状(图形g,int numri_brinjeve)
{
图形2d g2=(图形2d)g;
g2.设定行程(新基本行程(2.0f));
g、 setColor(颜色为粉红色);
g2.翻译(512250);
双肯迪=360.0/numri_brinjeve;
GeneralPath路径=新的GeneralPath();
双a=0;
双c=0;
双x=0;
双y=0;
双r=150;
对于(int i=1;i
每次我按下四个按钮中的一个,我都试图改变这个数字

您需要调用
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

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

基本上,
行程面板
需要保持其当前状态,因此选择的
实际上应该是
行程面板
的一个属性,而且,
选择的
不应该从
paintComponent
中修改,这将导致无休止的问题,就像
paintComponent
可以被修改一样在任何时候,由于任何原因调用,大多数情况下都没有您的参与。因此,您需要一些方法来设置和获取该值

StrokePanel

你有没有放入
System.out.println()
语句来查看代码的分支?我把这段代码放入一个IDE中,首先跳出来的是这些行,这会导致一个错误:
applet=new geometryquick();
Japplet.init();
因为上面没有声明applet。是的,很抱歉我在两个单独的文件中处理它,并且没有检查它。尽管它不是我想要的,但还是谢谢。1)调用
repaint
以触发绘制周期;2)在执行任何自定义绘制之前调用
super.paintComponent
;3)摆脱
extensed JApplet
并围绕
JFrame
构建您的UI,这会导致混乱不已。感谢您所做的一切,但我尝试了该代码,但它不会运行。当我运行代码。根据我发布(和测试)的示例,它对我来说很好
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);
    }
}
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;
    }
}