導航:首頁 > 編程語言 > java畫個矩形類代碼

java畫個矩形類代碼

發布時間:2023-05-21 21:05:49

java中做一個按鈕,點擊按鈕後畫一個矩形的代碼怎麼寫

兄弟幫你寫了一個:

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

public class Print {
public static void main(String[] args) {
new Te();
}
}

class Te extends Frame implements ActionListener {

Color cc = Color.red;
int x = -20, y = -50;
Random r = new Random();

public Te() {

this.setLayout(null);
Button b = new Button("畫圓");
this.add(b);
b.setBounds(30,30,50,50);
b.addActionListener(this);
this.addWindowListener(new WindowAdapter () {

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
this.setBounds(200,200,500,400);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
this.cc = Color.red;
this.x = r.nextInt(400);
do {
int x1 = r.nextInt(300);
this.y = x1;
} while (this.y < 50);

this.repaint();
}

@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(cc);
g.drawRect(x,y,50,50);
g.setColor(c);
}
}

㈡ java畫矩形

直接說event是簡單,不過總要試一試才敢拿上來講,所以就全寫上來了。。。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;

public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new PaintingPanel());
frame.setBounds(100, 100, 400, 400);
frame.setVisible(true);
}
}

class PaintingPanel extends JPanel {

ArrayList<Rectangle> list;
Rectangle current;

public PaintingPanel() {
list = new ArrayList<Rectangle>();
addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
}

MouseInputListener mouseHandler = new MouseInputAdapter() {
Point startPoint;

public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
current = new Rectangle();
}

public void mouseReleased(MouseEvent e) {
makeRectangle(startPoint, e.getPoint());
if (current.width > 0 && current.height > 0) {
list.add(current);
current = null;
repaint();
}
}

public void mouseDragged(MouseEvent e) {
if (current != null) {
makeRectangle(startPoint, e.getPoint());
repaint();
}
}

private void makeRectangle(Point p1, Point p2) {
int x = Math.min(p1.x, p2.x);
int y = Math.min(p1.y, p2.y);
int w = Math.abs(p1.x - p2.x);
int h = Math.abs(p1.y - p2.y);
current.setBounds(x, y, w, h);
}
};

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);

for (Rectangle rect : list) {
g.fillRect(rect.x, rect.y, rect.width, rect.height);
}

if (current != null) {
g.drawRect(current.x, current.y, current.width, current.height);
}
}

}

㈢ 用java編寫矩形類

class Rectangle{
double x1, y1, x2, y2;
Rectangle(){
this(0,0,0,0);
}
Rectangle(double x1, double y1, double x2, double y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
double getArea(){
return Math.abs((x1-x2)*(y1-y2))/2;
}
double getPemi(){
return Math.abs((x1-x2)+(y1-y2))*2;
}
}
public class Test{
public static void main(String[] args) {
Rectangle a = new Rectangle();
a.x1=2.1;
a.y1=3.2;
a.x2=5.2;
a.y2=6.3;
System.out.println("面積是"+a.getArea()+" 周長是"+a.getPemi());
Rectangle b = new Rectangle(1, 2, 6.8, 10.5);
System.out.println("面積是"+b.getArea()+" 周長是"+b.getPemi());
}
}

閱讀全文

與java畫個矩形類代碼相關的資料

熱點內容
讀取數據要哪個文件夾 瀏覽:461
微信怎樣接收電腦文件夾 瀏覽:574
蘋果鎖屏後時間沒有了 瀏覽:105
ios描述文件路徑 瀏覽:914
安卓手機音量dong聲 瀏覽:3
南昌哪裡有孩子學編程的 瀏覽:422
無法打開要寫入的文件 瀏覽:282
城信app有什麼用 瀏覽:645
fanuc程序傳輸軟體 瀏覽:211
s博士點評是什麼app 瀏覽:290
app怎麼封裝防紅 瀏覽:577
南通辦公文件櫃有哪些 瀏覽:894
蘋果分享錄音文件到微信 瀏覽:548
win10逐個選擇文件 瀏覽:312
少兒編程適合什麼樣的學生 瀏覽:655
微信怎麼製作語音文件 瀏覽:274
哪些病毒造成文件夾刪不掉 瀏覽:583
vr全景視頻文件怎麼打開 瀏覽:121
eclipse幾個版本 瀏覽:316
系統啟動密碼怎麼設 瀏覽:223

友情鏈接