Java 如何使用用户输入更改绘制对象的位置
我正在尝试绘制多条鱼(非常简单的鱼),用户可以指定多少条。但是,当用户指定要绘制多条鱼时,圆不会绘制在正确的位置。所有的卵形都被画出来了,它们只是不在正确的位置,所以它们不再像鱼了Java 如何使用用户输入更改绘制对象的位置,java,swing,jpanel,paint,draw,Java,Swing,Jpanel,Paint,Draw,我正在尝试绘制多条鱼(非常简单的鱼),用户可以指定多少条。但是,当用户指定要绘制多条鱼时,圆不会绘制在正确的位置。所有的卵形都被画出来了,它们只是不在正确的位置,所以它们不再像鱼了 public class FishList extends JPanel { static int fn = Integer.parseInt(JOptionPane.showInputDialog(null, "How many fish would you like to draw? ")); static i
public class FishList extends JPanel {
static int fn = Integer.parseInt(JOptionPane.showInputDialog(null, "How many fish would you like to draw? "));
static int w = 200;
static int h = 100;
static int x;
static int y;
static int a = x + 20;
static int b = y + 30;
static int d = 50;
static int c = x + 195;
public FishList() {
setPreferredSize(
new Dimension(400,400));
}
public void paint(Graphics g) {
g.setColor(Color.GREEN);
g.fillOval(x, y, w, h);
g.fillOval(c, y, d, h);
g.setColor(Color.BLACK);
g.fillOval(a, b, 25, 25);
}
public static void main(String[] args) {
MyFrame frame1 = new MyFrame("Drawing Fish");
JPanel outer = new JPanel();
for(int i=0; i<fn; i++){
x = 0 + (i*(w+d+1));
y = 0;
FishList sPanel1 = new FishList();
outer.add(sPanel1);
}
frame1.add(outer);
frame1.pack();
frame1.setVisible(true);
}
}
公共类FishList扩展了JPanel{
static int fn=Integer.parseInt(JOptionPane.showInputDialog(null,“您想画多少条鱼?”);
静态int w=200;
静态int h=100;
静态int x;
静态智力;
静态整数a=x+20;
静态整数b=y+30;
静态int d=50;
静态整数c=x+195;
公众捕鱼名单(){
设置首选大小(
新维度(400400));
}
公共空间涂料(图g){
g、 setColor(Color.GREEN);
g、 椭圆形(x,y,w,h);
g、 圆形(c,y,d,h);
g、 设置颜色(颜色为黑色);
g、 圆形(a、b、25、25);
}
公共静态void main(字符串[]args){
MyFrame frame1=新的MyFrame(“画鱼”);
JPanel outer=新的JPanel();
对于(int i=0;iJPanel
,默认情况下,使用FlowLayout
FlowLayout
还使用组件的首选大小来确定如何在容器中布局每个组件
未能调用super.paint
将导致严重问题。事实上,您应该使用paintComponent
而不是paint
,并确保您正在调用super.paintComponent
查看和了解更多详细信息
组件已经有了位置和大小的感觉,而您正在忽略。在此上下文中使用static
变量不会有任何帮助,因为基本上,鱼的每个实例都将绘制在完全相同的位置,因为它们将共享每个static
变量的相同值
更好的解决方案是生成一个能够开始绘制的类,然后绘制“鱼”
这些将包含在能够绘制它们的组件中,然后您可以将其添加到使用边界布局的容器中
例如
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Fishies {
public static void main(String[] args) {
new Fishies();
}
public Fishies() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new FishBowel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FishBowel extends JPanel {
private List<Fish> fishes = new ArrayList<>(25);
public FishBowel() {
for (int index = 0; index < 10; index++) {
int width = random(20);
int height = width;
int x = random(200 - 20);
int y = random(200 - 20);
fishes.add(new Fish(new Rectangle(x, y, width, height)));
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Fish fish : fishes) {
fish.paint(g2d);
}
g2d.dispose();
}
}
public static int random(int max) {
return (int)(Math.round(Math.random() * max));
}
public class Fish {
private Color color;
private Ellipse2D fish;
public Fish(Rectangle bounds) {
this(new Color(random(255), random(255), random(255)), bounds);
}
public Fish(Color color, Rectangle bounds) {
this.color = color;
fish = new Ellipse2D.Float(bounds.x, bounds.y, bounds.width, bounds.height);
}
public Ellipse2D getFish() {
return fish;
}
public Color getColor() {
return color;
}
public void paint(Graphics2D g2d) {
g2d.setColor(getColor());
g2d.fill(fish);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入java.awt.geom.Ellipse2D;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公营鱼类{
公共静态void main(字符串[]args){
新鱼类();
}
公共鱼类(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的fishboil());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类fishjanel{
私有列表=新的ArrayList(25);
公共鱼肠(){
对于(int-index=0;index<10;index++){
整数宽度=随机(20);
整数高度=宽度;
int x=随机(200-20);
int y=随机(200-20);
添加(新的Fish(新的矩形(x,y,宽度,高度));
}
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
用于(鱼:鱼){
鱼漆(g2d);
}
g2d.dispose();
}
}
公共静态int-random(int-max){
返回值(int)(Math.round(Math.random()*max));
}
公鱼{
私人色彩;
私人椭圆鱼;
公共鱼(矩形边界){
这(新颜色(随机(255)、随机(255)、随机(255))、边界);
}
公共鱼(颜色、矩形边框){
这个颜色=颜色;
fish=新的Ellipse2D.Float(bounds.x、bounds.y、bounds.width、bounds.height);
}
公共Ellipse2D getFish(){
还鱼;
}
公共颜色getColor(){
返回颜色;
}
公共空隙涂料(Graphics2D g2d){
setColor(getColor());
g2d.填充物(鱼);
}
}
}
我并不想忘恩负义,但我不太明白这一点。我只上了第二节计算机科学课,所以我不明白这一点。有没有更简单的解决办法?@user2877117作为一名初学者很好,但你必须折衷一下;你对答案有什么困难吗?@user2877117要实现你想要的,这是最简单的解决方案。从其他方面来说,你需要深入研究创建自己的布局管理器,如果你认为这个例子是压倒性的,那么自定义布局管理更有趣。。。