導航:首頁 > 編程語言 > 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畫個矩形類代碼相關的資料

熱點內容
pc桌面壁紙文件夾 瀏覽:473
微信怎麼添加群 瀏覽:781
40歲男人適合的微信名 瀏覽:925
編程里比例怎麼打 瀏覽:215
蘋果12兩個app如何分屏 瀏覽:592
ps下載完不是壓縮文件 瀏覽:362
電腦中的個人文件包括什麼 瀏覽:572
網路連接一般什麼密碼 瀏覽:199
java定時器quartz實例 瀏覽:259
稻殼excel文件太大怎麼弄 瀏覽:901
文件里的視頻如何保存到相冊 瀏覽:428
手機百度雲文件電腦 瀏覽:957
編程怎麼做到時鍾精準 瀏覽:912
錘子用過的壁紙在哪個文件里 瀏覽:468
qq網站安全性未知訪問不了怎麼辦 瀏覽:270
燕秀怎麼修改編程人名字 瀏覽:789
2012年天之眼導航升級 瀏覽:595
如何安裝視頻文件 瀏覽:315
紅米2A升級miui9 瀏覽:927
如何在表格中加入一行資料庫 瀏覽:421

友情鏈接