在一个ArrayList图形 [英] Graphics in an ArrayList

查看:129
本文介绍了在一个ArrayList图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个code,打印10个随机图形(椭圆形,长方形等)。
我跳槽加入我的随机椭圆形等在一个ArrayList要做到这一点,然后让Java的随机挑选的形状10倍,从这个ArrayList和打印这些采摘项目。

现在我不知道如果这甚至可能,如何我会去这个问题。

 进口java.awt中的*。
java.awt.event中导入*。
进口的java.util。*;
进口的javax.swing *。公共类卡拉继承JPanel实现的ActionListener {
    随机随机=新的随机();
    //...s    公共卡拉(){
        集preferredSize(新尺寸(400300)); // 300像素使面板400。
        // ...
        this.setBackground(Color.WHITE);
    }    保护类RandomShapesComponent扩展JComponent的{
    @覆盖    保护无效paintComponent(图形G){        super.paintComponent方法(G); //清除背景
        // ...
    }
    }    / **
     *重新绘制卡拉的JPanel,当按钮为pressed。 *
     * /
    @覆盖
    公共无效的actionPerformed(ActionEvent的五){
        再生();
        重绘();
    }    私人无效再生(){
        //清除形状列表
        // ...        //创建随机的形状
        // ...
    }
    公共静态无效的主要(字串[] ARG){
        最后红肉=新卡拉();
        //创建事件线程上的GUI。
        //这是优于
        SwingUtilities.invokeLater(Runnable的新(){
            @覆盖
            公共无效的run(){
                最终的JFrame帧=新的JFrame(计算机辅助随机艺术家);
                frame.add(卡拉,BorderLayout.CENTER);
                JButton的按钮=的新的JButton(重绘);
                button.addActionListener(卡拉);
                frame.add(按钮,BorderLayout.SOUTH);
                frame.pack();
                cara.regenerate(); //可以在这里做,如果卡拉的大小!
                frame.setVisible(真);
            }
        });
    }
}

和下面由我来画一个三角形(这是其中一个形状我想放入数组,并随机选择)的方式:

 进口的javax.swing *。
进口java.awt中的*。
java.awt.event中导入*。公共类三延伸卡拉{
 公共无效的paintComponent(图形G){
//创建科洛尔和三角形状随机变量
INT X;
诠释Ÿ;
INT×2;
INT Y2;X =(int)的的Math.random()* 100;
Y =(INT)的Math.random()* 100;
X2 =(INT)的Math.random()* 100;
Y2 =(INT)的Math.random()* 100;INT R1;
INT G1;
INT B1;
R1 =(int)的的Math.random()* 255;
G1 =(int)的的Math.random()* 255;
B1 =(INT)的Math.random()* 255;
 色色=新的色彩(R1,G1,B1);
 //绘制三角形 g.setColor(颜色);
 g.drawLine(X,Y,Y2,y)基
 g.setColor(颜色);
 g.drawLine(X,Y,Y2,Y2);
 g.setColor(颜色);
 g.drawLine(Y2,Y,Y2,Y2);
}
}


解决方案

这是绝对可能的。这里有一个想法:


  • 有一个名为形的抽象基类,它有一个名为),画(方法,对图形上下文参数

  • 有形状三角形,圆形等的子类,并相应地填写每一个的Draw()方法

  • 有在FruitPanel一个ArrayList实例变量

  • 在FruitPanel构造函数初始化10形状的子类的对象ArrayList中

  • 从在你的ArrayList FruitPanel电话平局(G)各的事情你的paintComponent()方法

您可以计算出随机化和这样的自己......这是一个基本的结构。祝你好运!

I am trying to create a code that prints 10 random graphics (oval,rectangle etc). I'm hopping to do this by adding my random ovals etc in an ArrayList and then let Java randomly pick a shape 10 times from this ArrayList and print these picked items.

Now I have no idea if this is even possible and how I would go about this.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Cara extends JPanel implements ActionListener {
    Random random = new Random();
    //...s

    public Cara() {
        setPreferredSize(new Dimension(400,300)); // make panel 400 by 300 pixels.
        // ... 
        this.setBackground(Color.WHITE);
    }

    protected class RandomShapesComponent extends JComponent{
    @Override

    protected void paintComponent(Graphics g) { 

        super.paintComponent(g);     // clears the background
        // ...
    }
    }

    /**
     * redraws the Cara JPanel, when the button is pressed. *
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        regenerate();
        repaint();
    }

    private void regenerate() {
        // clear the shapes list
        //...

        // create random shapes 
        // ... 
    }


    public static void main(String[] arg) {
        final Cara cara = new Cara();    
        // create the GUI on the event thread.
        // this is better than 
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Computer Assisted Random Artist");
                frame.add(cara, BorderLayout.CENTER);
                JButton button = new JButton("redraw");
                button.addActionListener(cara);
                frame.add(button, BorderLayout.SOUTH);
                frame.pack();
                cara.regenerate(); // can be done here if cara has a size!
                frame.setVisible(true);                
            }
        });
    }
}

And below is a way for me to draw a triangle (this is one of the shapes i would like to put into the array and be randomly picked):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Tri extends Cara{
 public void paintComponent (Graphics g){
// create random variables for collor and shape of triangle
int x;
int y;
int x2;
int y2;

x = (int) Math.random()*100;
y = (int) Math.random()*100;
x2 = (int) Math.random()*100; 
y2 = (int) Math.random()*100;

int r1;
int g1;
int b1;
r1 = (int) Math.random()*255;
g1 = (int) Math.random()*255;
b1 = (int) Math.random()*255;
 Color color = new Color(r1,g1,b1);
 //draw triangle

 g.setColor(color);
 g.drawLine(x,y,y2,y);
 g.setColor(color);
 g.drawLine(x,y,y2,y2);
 g.setColor(color);
 g.drawLine(y2,y,y2,y2);


} 


}

解决方案

This is definitely possible. Here's an idea:

  • Have an abstract base class called "Shape" and it has a method called "draw()" which has a parameter for a Graphics context
  • Have subclasses of Shape for Triangle, Circle, etc and fill in each's draw() method accordingly
  • Have an ArrayList instance variable in FruitPanel
  • in the FruitPanel constructor initialize the ArrayList with 10 Shape subclass objects
  • From your paintComponent() method in FruitPanel call draw(g) on each of the things in your ArrayList

You can figure out the randomization and such yourself... this is a basic structure. Good luck!

这篇关于在一个ArrayList图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆