本文目錄一覽:
- 1、java java.awt.shape
- 2、問一個問題,JAVA上可不可以做出來一個圓形的按鈕?
- 3、java設計圖形(Shape)類及其子類(Circle、Rectangle)
- 4、用java怎樣畫橢圓?
java java.awt.shape
畫三角形為啥不用drawline呢 – -!Graphics
這是shape的描述
Shape 介面提供了表示一些幾何形狀的對象的定義。Shape 是由 PathIterator 對象描述的,它可以表示 Shape 的輪廓以及確定該輪廓如何將 2D 平面劃分成內點和外點的規則。每個 Shape 對象都提供回調,以獲取幾何形狀的邊框,確定點或矩形是部分還是全部位於 Shape 內部,並檢索一個描述 Shape 輪廓的軌跡路徑的 PathIterator 對象。
內部定義:當且僅當以下條件成立時,才認為某個點位於 Shape 內:
該點完全位於 Shape 邊界內,或者
該點恰好位於 Shape 邊界上,並且 X 軸正方向上緊鄰該點的空間完全處於邊界之內,或者
該點恰好在水平邊界分段上,並且 Y 軸正方向上緊鄰該點的空間完全處於邊界之內。
contains 和 intersects 方法將 Shape 內部視為可以填充的封閉區域。這意味著為了確定某個 shape 是否包含矩形或與矩形相交,或者確定某個 shape 是否包含一個點,這些方法將隱式地認為未閉合的 shape 是閉合的。
問一個問題,JAVA上可不可以做出來一個圓形的按鈕?
可以自定義的
貼代碼給你看下
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JFrame;
public class CircleButton extends JButton {
Shape shape;
Color bgColor = SystemColor.control;
public CircleButton() {
this(“未命名”, null);
}
public CircleButton(String label) {
this(label, null);
}
public CircleButton(String label, Color bgColor) {
super(label); // 調用父類構造方法
if (bgColor != null) {
this.bgColor = bgColor;
}
Dimension size = this.getPreferredSize();
size.width = size.height = Math.max(size.width, size.height);
this.setPreferredSize(size); // 設置寬高等距
this.setContentAreaFilled(false); // 不繪製內容區域
this.setBorderPainted(false); // 不繪製邊框
this.setFocusPainted(false); // 不繪製焦點狀態
}
protected void paintComponent(Graphics g) {
// 如果滑鼠按下,isArmed()方法返回true
if (this.getModel().isArmed()) {
g.setColor(java.awt.SystemColor.controlHighlight);
} else {
g.setColor(java.awt.SystemColor.controlShadow);
g.setColor(this.bgColor); // 設置背景顏色
}
g.fillOval(0, 0, this.getSize().width – 1, this.getSize().height – 1); // 繪製圓形背景區域
g.setColor(java.awt.SystemColor.controlShadow); // 設置邊框顏色
g.drawOval(0, 0, this.getSize().width – 1, this.getSize().height – 1); // 繪製邊框線
super.paintComponent(g);
}
public boolean contains(int x, int y) {
if ((shape == null) || (!shape.getBounds().equals(this.getBounds()))) {
this.shape = new Ellipse2D.Float(0, 0, this.getWidth(), this
.getHeight());
}
return shape.contains(x, y);
}
}
class CircleButtonTest {
public static void main(String[] args) {
JFrame jf = new JFrame(“自定義按鈕”);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(350, 280);
jf.setLocationRelativeTo(null);
jf.setLayout(new FlowLayout());
Color arrColor[] = new Color[] { Color.blue, Color.black, Color.red,
Color.yellow, Color.green };
for (int i = 0; i 5; i++) {
CircleButton cb = new CircleButton(“圓形按鈕” + (i+1),arrColor[i]);
jf.getContentPane().add(cb);
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(“按鈕”);
}
});
}
jf.setVisible(true);
}
}
java設計圖形(Shape)類及其子類(Circle、Rectangle)
你好,剛好閑著幫你寫一個:
Shape類:
public class Shape {
protected Point location;
public Shape(){
}
public double area(){
return 0.0;
}
}
Circle類:
public class Circle extends Shape{
private int r;
public Circle() {
}
public Circle(Point center,int r) {
super.location=center;
this.r = r;
}
public double area() {
return Math.PI*r*r ;
}
}
Rectangle類:
public class Rectangle extends Shape{
private int width;
private int height;
public Rectangle() {
}
public Rectangle(Point o,int width, int height) {
location=o;
this.width = width;
this.height = height;
}
public double area() {
return width*height;
}
}
我這裡圖方便,在創建圓的時候直接用圓心和半徑創建,還有矩形也是用一個點位置和長寬創建,所以還要加一個點類:
public class Point {
public int x;
public int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
用java怎樣畫橢圓?
你的問題是是使用java畫橢圓,可以使用awt和swing類庫實現
畫橢圓可以通過畫矩形及其內切橢圓實現,示例代碼如下
如果只需要橢圓,則無需g2.draw(rect);
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// 畫矩形
double leftX = 100;
double topY = 100;
double width = 200;
double height = 150;
Rectangle2D rect = new Rectangle2D.Double(leftX, topY, width, height);
g2.draw(rect);
// 畫rect的內切橢圓
Ellipse2D ellipse = new Ellipse2D.Double();
ellipse.setFrame(rect);
g2.draw(ellipse);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/179994.html