简体   繁体   English

如何使用GUI在Java中创建笑脸图章

[英]How to create a Smiley Face stamp in java using GUI

I'm trying to write a function that will draw the pictured smiley face 我正在尝试编写一个函数,该函数将绘制出如图所示的笑脸

在此处输入图片说明

I need to pass in parameters for location and size of the smiley face. 我需要为笑脸的位置和大小传递参数。 For example, if the location passed in was (0,0) and size 100, then the yellow circle would be a width and height of 100, the eyes are black circles located at (30,30) and (60,30) with a width and height of 5, and the mouth is a black semi-circle inset by 10 pixels. 例如,如果传入的位置是(0,0)且大小为100,则黄色圆圈的宽度和高度为100,眼睛是位于(30,30)和(60,30)的黑眼圈,宽度和高度为5,并且嘴为10像素的黑色半圆。 Lastly I need to call the smiley function from paintComponent then use it as a stamp to draw at least 5 different smileys in different locations and sizes. 最后,我需要从paintComponent调用笑脸函数,然后将其用作图章以在不同位置和大小上绘制至少5个不同的笑脸。

I know I need to create a equation that will do all of this but am unaware as to how to accomplish this because when I change the x and y coordinates the eyes of the smiley face are not in the proper position as they were before the change. 我知道我需要创建一个方程式来完成所有这些工作,但是不知道如何完成此操作,因为当我更改x和y坐标时,笑脸的眼睛不像更改前那样处于适当的位置。 anything helps, thanks. 任何帮助,谢谢。

public class GuiApp extends JFrame
{
    private DrawingPanel panel;
public class DrawingPanel extends JPanel 
{
    public DrawingPanel()
    {
        this.setBackground(Color.RED);
    }
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g); 
       // drawFlower(g, 20, 10, 10);
       drawSmiley(g, 25, 25, 100);
    }
}
public GuiApp()
{
    setBounds(100, 100, 450, 300);//x,y,w,h of window
    panel = new DrawingPanel();
    this.setContentPane(panel);
}
public static void main(String [] args)
{
    GuiApp f = new GuiApp();
    f.setTitle("Smiley");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}
public void drawFlower(Graphics g,int x,int y,int s)
{
     g.drawOval(60, 60, 200, 200);
     g.fillOval(90, 120, 50, 20);
     g.fillOval(190, 120, 50, 20);
     g.drawArc(110, 130, 95, 95, 0, -180);     
}
public void drawSmiley(Graphics g, int x, int y, int s)
{
    g.setColor((Color.YELLOW));
    g.fillOval(x-s/2, y-s/2, s, s);
    g.setColor((Color.blue));
    g.fillOval((int)(1+(x-s/2)+(x-s/2)*.3), (int)(1+(y-s/2)+(y-s/2)*.3),(int)(s*.10),(int)(s*.10));
    g.fillOval((int)(1+(x-s/2)+(x-s/2)*.9), (int)(1+(y-s/2)+(y-s/2)*.3),(int)(s*.10),(int)(s*.10));
    g.drawArc((int)(1+(x-s/2)+(x-s/2)*.1), (int)(1+(y-s/2)-(y-s/2)*.15), (int)(s*.9), (int)(s), 0, -180);
}

} }

Remember, most graphical operations occur from the top/left corner, so when drawing something like an oval, the x/y is the top/left corner of the oval and it will expand right/down. 请记住,大多数图形操作都是从上/左角开始的,因此当绘制椭圆形时, x/y是椭圆的上/左角,它将向右/向下扩展。

So, this means that when drawing the right eye, for example, you will not only need to calculate the x/y position as a factor of the diameter of the circle, but you may need to subtract the width of the eye itself from the horizontal to make it "look" right 因此,这意味着,例如,在绘制右眼时,您不仅需要将x / y位置计算为圆直径的因数,而且还可能需要从中减去眼睛本身的宽度。水平使其“看起来”正确

Now, you can make your life easier by using Graphics#translate to move the origin/starting point of all graphical operations to that new location, this will reduce the amount of calculations you need to make. 现在,通过使用Graphics#translate将所有图形操作的起点/起点移动到该新位置,可以使您的生活更轻松,这将减少您需要进行的计算量。 This also means that you could, technically, write two methods, one which did the actual painting of the smiley face starting from position 0x0 and one which translated the position (based on the parameters) and then called the first, but that's just an example idea ;) 从技术上讲,这也意味着您可以编写两种方法,一种方法是从位置0x0开始实际绘画笑脸,另一种方法是(根据参数)转换位置,然后调用第一种方法,但这仅是一个示例。想法;)

微笑

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Smile {

    public static void main(String[] args) {
        new Smile();
    }

    public Smile() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            drawSmiley(g, 25, 25, 100);
        }

        public void drawSmiley(Graphics g, int x, int y, int s) {
            Graphics copy = g.create();
            copy.translate(x, y);
            copy.setColor((Color.YELLOW));
            copy.fillOval(0, 0, s, s);
            copy.setColor((Color.blue));
            copy.fillOval((int) (1 + s * .3), (int) (1 + s * .3), (int) (s * .10), (int) (s * .10));
            copy.fillOval((int) ((1 + s * .7) - (s * .10)), (int) (1 + s * .3), (int) (s * .10), (int) (s * .10));

            double width = s * 0.8;
            double height = s * 0.8;

            copy.drawArc((int)((s - width) / 2d), (int)((s - height) / 2d), (int)width, (int)height, 0, -180);
            copy.dispose();
        }
    }

}

输出的图像

package guimodule;

import processing.core.PApplet;

public class MyDisplay extends PApplet {

  public void setup() {
    size(400, 400);
    background(200, 200, 200);

  }

  public void draw() {
    fill(255, 255, 0);
    ellipse(200, 200, 390, 390);
    fill(0, 0, 0);
    ellipse(120, 130, 50, 70);
    ellipse(280, 130, 50, 70);
    noFill();
    fill(0, 0, 0);
    arc(200, 280, 145, 120, 0, PI);
  }
}

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

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