如何使用GUI

时间:2015-12-06 21:43:48

标签: java eclipse logic algebra

我正在尝试编写一个可以绘制图像笑脸的功能

enter image description here

我需要传递笑脸的位置和大小的参数。例如,如果传入的位置是(0,0)和大小100,则黄色圆圈的宽度和高度为100,眼睛是位于(30,30)和(60,30)的黑色圆圈。宽度和高度均为5,嘴部为黑色半圆形,内嵌10个像素。最后,我需要从paintComponent调用smiley函数,然后将其用作图章,以在不同的位置和大小绘制至少5个不同的表情。

我知道我需要创建一个方程式来完成所有这些,但我不知道如何实现这一点,因为当我改变x和y坐标时,笑脸的眼睛不在正确的位置,因为它们是在改变之前。什么都有帮助,谢谢。

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

}

2 个答案:

答案 0 :(得分:1)

请记住,大多数图形操作都是从左上角开始的,因此在绘制像椭圆形的东西时,x/y是椭圆的左上角,它会向右/向下扩展。

因此,这意味着在绘制右眼时,您不仅需要计算x / y位置作为圆直径的因子,还可能需要减去眼睛的宽度从水平方向本身使它“看起来”正确

现在,您可以使用Graphics#translate将所有图形操作的原点/起点移动到新位置,从而简化您的生活,这将减少您需要进行的计算量。这也意味着你可以在技术上编写两种方法,一种是从位置0x0开始实际绘制笑脸,另一种是平移位置(基于参数),然后调用第一种,但是这只是一个例子;)

Smile

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

}

答案 1 :(得分:-1)

An image of the output

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