如何根据用户输入在 paint 方法中绘制墙
How to draw a wall in paint method based on user input
我对编程还很陌生,所以我决定介绍一下 java class。我有一个任务,我必须使用 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);
}
}
}
}
基本上,您的代码可以工作,但是您的 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);
}
}
}
}
}
所以,基本上,我在这里所做的就是将 "wall painting" 移动到它自己的 component/class 并提供一个简单的 setter(和 getter)用于更改行数
我对编程还很陌生,所以我决定介绍一下 java class。我有一个任务,我必须使用 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);
}
}
}
}
基本上,您的代码可以工作,但是您的 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);
}
}
}
}
}
所以,基本上,我在这里所做的就是将 "wall painting" 移动到它自己的 component/class 并提供一个简单的 setter(和 getter)用于更改行数