【问题标题】:How to draw a wall in paint method based on user input如何根据用户输入在paint方法中绘制墙壁
【发布时间】:2015-09-24 22:09:17
【问题描述】:

我对编程很陌生,我决定介绍一下 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);
        }
}
}
}

【问题讨论】:

  • 您的代码有效,但您的 y 偏移量太大

标签: java swing for-loop repaint


【解决方案1】:

基本上,您的代码可以正常工作,但您的 starty 太大并且似乎在屏幕上显示。

通常,您应该避免覆盖 paint 之类的顶级容器,例如 JApplet(为什么要使用小程序?!),而应改用 JPanel 之类的组件。

这有几个原因,但您会遇到的一个原因是paint 可以在子组件上进行绘制,但是,由于绘制的工作方式,这些子组件在更新时可以在您的内容上绘制'已经画了……这对用户来说很奇怪。

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

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

    }
}

所以,基本上,我在这里所做的只是将“壁画”移动到它自己的组件/类中,并提供了一个简单的设置器(和获取器)来更改行数

【讨论】:

  • 非常感谢!我没有意识到开始 y 是如此之大。也感谢您提供的其他提示,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 2014-11-15
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多