[英]How to create a Smiley Face stamp in java using GUI
我正在嘗試編寫一個函數,該函數將繪制出如圖所示的笑臉
我需要為笑臉的位置和大小傳遞參數。 例如,如果傳入的位置是(0,0)且大小為100,則黃色圓圈的寬度和高度為100,眼睛是位於(30,30)和(60,30)的黑眼圈,寬度和高度為5,並且嘴為10像素的黑色半圓。 最后,我需要從paintComponent調用笑臉函數,然后將其用作圖章以在不同位置和大小上繪制至少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);
}
}
請記住,大多數圖形操作都是從上/左角開始的,因此當繪制橢圓形時, x/y
是橢圓的上/左角,它將向右/向下擴展。
因此,這意味着,例如,在繪制右眼時,您不僅需要將x / y位置計算為圓直徑的因數,而且還可能需要從中減去眼睛本身的寬度。水平使其“看起來”正確
現在,通過使用Graphics#translate
將所有圖形操作的起點/起點移動到該新位置,可以使您的生活更輕松,這將減少您需要進行的計算量。 從技術上講,這也意味着您可以編寫兩種方法,一種方法是從位置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.