[英]How to repaint() a StrokePanel in 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);
}
}
}
我试图每次按下四个按钮之一时改变身材
您需要调用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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.