简体   繁体   English

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

[英]How to draw a wall in paint method based on user input

I fairly new to programming and I have decided to take an intro to java class. 我对编程很陌生,因此决定对Java类进行介绍。 I have an assignment where I have to create a wall using a for loop that changes height of the wall based on user input. 我有一个任务,我必须使用一个for循环来创建墙,该环会根据用户输入来更改墙的高度。 I think I got most of the code right but I can't seem to connect the user input with the for loop. 我认为我的大多数代码都正确,但似乎无法将用户输入与for循环连接。 Any help would be appreciated. 任何帮助,将不胜感激。

//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);
        }
}
}
}

Basically, your code works, but your starty is to large and seems to be painting off the screen. 基本上,您的代码可以工作,但是您的starty了,似乎在屏幕上画了画。

Generally, you should avoid overriding paint of top level containers like JApplet (why are using applets?!) and instead, use a component like JPanel instead. 通常,您应该避免覆盖诸如JApplet类的顶级容器的paint (为什么要使用Applet ?!),而应改用诸如JPanel类的组件。

A few reasons for this, but the one you will run into is that paint can paint over the child components, BUT, because of the way painting works, those child components, when updated, can paint over what you've painted ... which altogether, is just weird for the user. 造成这种情况的原因有很多,但您会遇到的一个原因是, paint可以在子组件上进行绘画,但是由于绘画的工作方式,这些子组件在更新后可以在您所绘画的内容上进行绘画...总而言之,这对用户来说很奇怪。

Instead, separate you code into logic units, each unit should be responsible for a single unit (of logic) work 相反,将您的代码分成逻辑单元,每个单元应负责一个(逻辑)单元的工作

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Wall extends JApplet implements ActionListener {

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

    private WallPanel wallPanel;

//Method declaration
    public void init() {
        getContentPane().setBackground(new Color(128, 128, 128));//Changes backround of JApplet to black
        //Set JButton and JLabel
        setLayout(new BorderLayout());

        JPanel controls = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        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);
        controls.add(directions, gbc);
        controls.add(input, gbc);
        controls.add(go, gbc);

        wallPanel = new WallPanel();

        add(controls, BorderLayout.NORTH);
        add(wallPanel);
    }

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

    public class WallPanel extends JPanel {

        private int rowCount;

        public void setRowCount(int rowCount) {
            this.rowCount = rowCount;
            repaint();
        }

        public int getRowCount() {
            return rowCount;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            int startX = 50;
            int startY = 0;
            int width = 50;
            int height = 20;
            int spacing = 2;
            int xOffset = 0;
            for (int row = 0; row < getRowCount(); 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)));
                    g.setColor(Color.RED);
                    g.fillRect(x, y, width, height);
                }
            }
        }

    }
}

So, basically, all I've done here is move the "wall painting" to it's own component/class and provided a simple setter (and getter) for changing the number of rows 因此,基本上,我在这里所做的就是将“壁画”移至它自己的组件/类,并提供了一个简单的setter(和getter)来更改行数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM