Java 从JFrame中删除圆
我正在创建一个河内塔游戏,并成功地在一个时间延迟打印形状(这部分代码已被删除,而我试图获得所有的椭圆形,他们应该是)我想知道我将如何着手删除圆。如您所见,我已经尝试了Java 从JFrame中删除圆,java,swing,user-interface,geometry,awt,Java,Swing,User Interface,Geometry,Awt,我正在创建一个河内塔游戏,并成功地在一个时间延迟打印形状(这部分代码已被删除,而我试图获得所有的椭圆形,他们应该是)我想知道我将如何着手删除圆。如您所见,我已经尝试了clearRect()方法,但这不起作用。由于每个圆都有自己的方法,因此有没有办法设置该方法在JPanel上是否可见?我认为这是最简单的方法,但如果有人有更好的想法,那就去做吧!谢谢你的帮助,我已经附上了我的代码 package towersofhanoi; import javax.swing.*; import java.awt
clearRect()代码>方法,但这不起作用。由于每个圆都有自己的方法,因此有没有办法设置该方法在JPanel上是否可见?我认为这是最简单的方法,但如果有人有更好的想法,那就去做吧!谢谢你的帮助,我已经附上了我的代码
package towersofhanoi;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.Object;
/*g.fillOval(60 = horizontal distance , 540= vertical distance, 400 = width, 60 = height) */
public class TowersOfHanoi extends JPanel {
private int clock = 0;
private Color circles = new Color(176, 56, 251);
public static void main(String[] args) {
// Print the shapes and frame
TowersOfHanoi drawRectangle = new TowersOfHanoi();
JFrame frame = new JFrame("Towers of Hanoi");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(drawRectangle);
frame.setSize(1250, 800);
frame.setVisible(true);
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
drawRectangle.nextFrame();
drawRectangle.repaint();
}
});
timer.setRepeats(true);
timer.start();
}
public void nextFrame() {
clock++;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
frame1(g);
frame2(g);
frame3(g);
frame4(g);
frame5(g);
frame6(g);
frame7(g);
frame8(g);
frame9(g);
}
private Color frame1(Graphics g) {
Color pegs = new Color(251, 129, 56);
g.setColor(pegs);
// peg 1
g.fillRect(250, 300, 25, 450);
// peg 2
g.fillRect(600, 300, 25, 450);
// peg 3
g.fillRect(950, 300, 25, 450);
// bottom
g.fillRect(200, 700, 825, 50);
// create a color for circles
// cirle 7 (Labeled from bottom to top)
g.setColor(circles);
g.fillOval(60, 640, 400, 60);
g.setColor(Color.BLACK);
g.drawOval(60, 640, 400, 60);
return circles;
}
private void frame2(Graphics g) {
// circle 6
g.setColor(circles);
g.fillOval(85, 580, 350, 60);
g.setColor(Color.BLACK);
g.drawOval(85, 580, 350, 60);
}
private void frame3(Graphics g) {
// circle 5
g.setColor(circles);
g.fillOval(110, 520, 300, 60);
g.setColor(Color.BLACK);
g.drawOval(110, 520, 300, 60);
}
private void frame4(Graphics g) {
// circle 4
g.setColor(circles);
g.fillOval(135, 465, 250, 55);
g.setColor(Color.BLACK);
g.drawOval(135, 465, 250, 55);
}
private void frame5(Graphics g) {
// circle 3
g.setColor(circles);
g.fillOval(160, 420, 200, 45);
g.setColor(Color.BLACK);
g.drawOval(160, 420, 200, 45);
}
private void frame6(Graphics g) {
// circle 2
g.setColor(circles);
g.fillOval(185, 380, 150, 40);
g.setColor(Color.BLACK);
g.drawOval(185, 380, 150, 40);
}
private void frame7(Graphics g) {
// circle 1
g.setColor(circles);
g.fillOval(210, 345, 100, 35);
g.setColor(Color.BLACK);
g.drawOval(210, 345, 100, 35);
}
public void frame8(Graphics g) {
g.clearRect(210, 345, 100, 35);
g.setColor(circles);
g.fillOval(560, 665, 100, 35);
g.setColor(Color.BLACK);
g.drawOval(560, 665, 100, 35);
}
public void frame9(Graphics g) {
g.clearRect(185, 380, 150, 40);
g.setColor(circles);
g.fillOval(890, 660, 150, 40);
g.setColor(Color.BLACK);
g.drawOval(890, 660, 150, 40);
}
}
我想知道我该如何去掉这个圆圈
super.paintComponent(…)将清除面板上的绘画,因此这就是所需的全部内容
正如你所看到的,我已经尝试了clearRect()
它是不需要的(见我上面的评论),但是您再次调用fillOval(…)和drawOval(…)方法,以便重新绘制。因此,代码完全按照您的要求执行
如果该方法在JPanel上可见,有什么方法可以设置吗
您需要一个布尔指示器来告诉绘制方法要做什么。比如:
if (paintOval1)
frame1(g);
if (paintOval2)
frame2(g);
ArrayList<CustomClass> circles = new ArrayList<CustomClass>();
circles.add( new CustomClass(60, 640, 400, 60, true) );
circles.add( new CustomClass(85, 580, 350, 60, true) );
pubic void setCirclePainted(int circle, Boolean isPainted)
{
CustomClass circle = circles.get(circle);
circle.setPainted( isPainted );
}
当然,这是一种非常野蛮的方法,如果你有100个圆圈要画,这不是一种很好的方法。代码变得太大了
因此,您应该创建一个包含四个属性(x、y、width、height、ispaint)的自定义类。然后为每个圆创建该类的实例,并将该类添加到ArrayList中。比如:
if (paintOval1)
frame1(g);
if (paintOval2)
frame2(g);
ArrayList<CustomClass> circles = new ArrayList<CustomClass>();
circles.add( new CustomClass(60, 640, 400, 60, true) );
circles.add( new CustomClass(85, 580, 350, 60, true) );
pubic void setCirclePainted(int circle, Boolean isPainted)
{
CustomClass circle = circles.get(circle);
circle.setPainted( isPainted );
}
最后,您需要一种方法来更改绘制圆的状态。比如:
if (paintOval1)
frame1(g);
if (paintOval2)
frame2(g);
ArrayList<CustomClass> circles = new ArrayList<CustomClass>();
circles.add( new CustomClass(60, 640, 400, 60, true) );
circles.add( new CustomClass(85, 580, 350, 60, true) );
pubic void setCirclePainted(int circle, Boolean isPainted)
{
CustomClass circle = circles.get(circle);
circle.setPainted( isPainted );
}
因此,您的关键是创建“CustomClass”,并为其命名。然后您需要实现类的所有getter/setter方法,以便访问类的属性。我实际上发现,我理解和执行的最简单方法是使用else
和else if
语句根据时间显示形状。谢谢你的其他建议
public void paintComponent(Graphics g) {
super.paintComponent(g);
frame1(g);
frame2(g);
frame3(g);
frame4(g);
if (clock<= 5) {
frame5(g);
}
else if(clock >= 6) {
frame9(g);
}
frame6(g);
frame7(g);
frame8(g);
}
公共组件(图形g){
超级组件(g);
框架1(g);
框架2(g);
框架3(g);
框架4(g);
如果(时钟=6){
框架9(g);
}
框架6(g);
框架7(g);
框架8(g);
}