gpt4 book ai didi

java - 用户选择要在Java中绘制的颜色和形状后尝试绘制矩形/椭圆形

转载 作者:行者123 更新时间:2023-12-02 05:54:09 29 4
gpt4 key购买 nike

刚开始摆弄 GUI。我希望这个程序绘制矩形/圆形(取决于用户单击的位置),并继续在前一个形状内绘制相同的形状,直到达到用户在文本字段中输入的数量。当我运行它时,它似乎永远无法进入paintComponent。感谢您的帮助。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class ShapePanel extends JPanel implements ActionListener {

JTextField numOfShapes;
JButton square, circle, black, red, blue;
boolean isSquare = true;
boolean isCircle = false;
boolean isBlack = true;
boolean isRed = false;
boolean isBlue = false;
int num = 0;

ShapePanel() {

setLayout( new BorderLayout());

//Panel 1
JPanel p1 = new JPanel();
square = new JButton("Squares");
p1.add(square);
square.addActionListener( this );

circle = new JButton("Circles");
p1.add(circle);
circle.addActionListener( this );

numOfShapes = new JTextField(15);
p1.add(numOfShapes);
//numOfShapes.addKeyListener( this );
add(p1, BorderLayout.NORTH);



//Panel 3
JPanel p3 = new JPanel();
black = new JButton("Black");
p3.add(black);
black.addActionListener( this );

red = new JButton("Red");
p3.add(red);
red.addActionListener( this );

blue = new JButton("Blue");
p3.add(blue);
blue.addActionListener( this );
add(p3, BorderLayout.SOUTH);
}

@Override
public void actionPerformed(ActionEvent ae) {
String nOfS = numOfShapes.getText();
System.out.println("made it to action performed");

if(ae.getSource() == square){
isSquare = true;
}
if(ae.getSource() == circle){
isCircle = true;
}
if(ae.getSource() == black){
isBlack = true;
}
if(ae.getSource() == red){
isRed = true;
}
if(ae.getSource() == blue){
isBlue = true;
}
}


class Paint extends JPanel {

public void paintComponent(Graphics g){
super.paintComponent(g);

JPanel p2 = new JPanel();
int c = 0;//change in size
int x1 = 20;
int x2 = 200;
int y1 = 20;
int y2 = 200;
String nOfS = numOfShapes.getText(); //number of shapes to be drawn
num = Integer.parseInt(nOfS);

System.out.println("Made it to paint component");

if (isBlack){
g.setColor(Color.BLACK);
isBlack = false;
}
if (isRed){
g.setColor(Color.RED);
isRed = false;
}
if (isBlue){
g.setColor(Color.BLUE);
isBlue = false;
}

if (isSquare){
for(int i = 0; i < num; i++){
x1+=c;
y1+=c;
x2-=c;
y2-=c;
g.drawRect(x1, y1, x2, y2);
c+=10;
}
isSquare = false;
}
if (isCircle){
for(int i = 0; i < num; i++){
x1+=c;
y1+=c;
x2-=c;
y2-=c;
g.drawOval(x1, y1, x2, y2);
c+=10;
}
isCircle = false;
}

add(p2, BorderLayout.CENTER);
}
}
}


public class lab14 {
public static void main(String[] args) {
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setSize(400, 400);
application.setLocationRelativeTo(null);
application.setTitle("Shapes");
application.add(new ShapePanel());
application.setVisible(true);
}
}

最佳答案

建议

  • 我会将 Paint JPanel 重命名为其他名称,因为 Paint 类名已用作 Java 核心类之一。将其重命名为其他名称,例如 DrawPanel。
  • 我将重写 DrawPanel 类中的 paintComponent(...) 并调用 super 的方法,就像您所做的那样。
  • 我会删除任何更改对象状态或从 paintComponet 中向 GUI 添加组件的代码。它应该只用于绘画和绘画。
  • 我将一个 DrawPanel 实例(例如名为“drawPanel”)添加到主 JPanel BorderLayout.CENTER。
  • 我确信只向 JButton 添加一次 ActionListener。
  • 在 ActionListener 中,我会在更改其状态后对其调用 repaint()

关于java - 用户选择要在Java中绘制的颜色和形状后尝试绘制矩形/椭圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23257181/

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