如何根据用户输入在paint方法中绘制墙

时间:2015-09-24 22:09:17

标签: java swing for-loop repaint

我对编程很新,我决定参加java类的介绍。我有一个任务,我必须使用for循环创建一个墙,根据用户输入更改墙的高度。我认为我的大部分代码都是正确的,但我似乎无法将用户输入与for循环连接起来。任何帮助将不胜感激。

//Package List
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class Wall extends JApplet implements ActionListener{

//Component declaration
JLabel directions;
JTextField input = new JTextField( 10 );
private JButton go;
//Variable declaration
int userinput;


//Method declaration
public void init() 
{
    getContentPane().setBackground(new Color (128, 128, 128));//Changes backround of JApplet to black
    //Set JButton and JLabel
    setLayout (new FlowLayout( ));
    directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
    go = new JButton( "Go!" );
    go.setBackground( Color.GREEN );
    go.setFocusPainted( false );
    go.addActionListener( this );
    add (directions );
    add (input); 
    add( go );
}

 public void actionPerformed( ActionEvent ae )
{
    String text = input.getText();
    userinput = Integer.parseInt( text );
    repaint();
}

//Method declaration 
public void paint(Graphics g) 
{
    super.paint(g);
    int startX = 50;
    int startY = 650;
    int width = 50;
    int height = 20;
    int spacing = 2;
    int xOffset = 0;
    for (int row = 0; row < userinput; row++) {
        int y = startY + (row * ( height + spacing));
        if ( row % 2 == 0) {
            xOffset = width / 2;
        } else {
            xOffset = 0;
        }
        for (int col = 0; col < 8; col++) {
            int x = xOffset + (startX + (col * (width + spacing)));
            System.out.println(x + "x" + y);
            g.setColor( Color.RED );
            g.fillRect( x, y, width, height);
        }
}
}
}

1 个答案:

答案 0 :(得分:1)

基本上,您的代码有效,但您的<div class="" id="step-id-1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <h4 class="modal-title" id="myModalLabel">Create your password pls.</h4> </div> <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> <div class="modal-body"> <p>You should fill the field with password </p> <p><input type="password" class="form-control input-lg" id="new-password" required placeholder="Yeni Şife.." name="new-password" /></p> <p><input type="password" class="form-control input-lg" id="confirm-password" required placeholder="Repeat you password pls.." name="confirm-password" /></p> <?php if($flag){ ?> <div style="padding:5px 10px; color:#fff; background:#F00">Passwords are unmatched, refill the form please</div> <?php } ?> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> **<button type="submit" class="btn btn-primary update-password">Confirm your password</button>** </div></form> **<a href="#" data-powertour-action="next" style="float:right">Next step</a>** </div> </div> </div> 很大,似乎在屏幕上绘画。

通常,您应该避免覆盖startypaint等顶级容器(为什么使用applet?!),而是使用JApplet之类的组件。

有几个原因,但你会遇到的是JPanel可以在子组件上绘画,但是,由于绘画的工作方式,这些子组件在更新时可以绘制什么你已经画过......这对用户来说简直太奇怪了。

相反,将代码分成逻辑单元,每个单元应负责一个单元(逻辑)工作

paint

所以,基本上,我在这里所做的就是移动'#34;壁画&#34;到它自己的组件/类,并提供了一个简单的setter(和getter)来改变行数