g.drawRect(100, 100, 80, 80);這個有問題。貌似應該是這樣g.drawRect(x,y,x+width,y+height),所以你畫的圖長寬都為負數,看不到圖形了。
創(chuàng)新互聯是一家網站設計公司,集創(chuàng)意、互聯網應用、軟件技術為一體的創(chuàng)意網站建設服務商,主營產品:響應式網站設計、品牌網站設計、營銷型網站建設。我們專注企業(yè)品牌在網站中的整體樹立,網絡互動的體驗,以及在手機等移動端的優(yōu)質呈現。網站設計制作、成都做網站、移動互聯產品、網絡運營、VI設計、云產品.運維為核心業(yè)務。為用戶提供一站式解決方案,我們深知市場的競爭激烈,認真對待每位客戶,為客戶提供賞析悅目的作品,網站的價值服務。
public void drawRect(int x,
int y,
int width,
int height)
類 Graphics 中的 drawRect
參數:
x - 要繪制矩形的 x 坐標。
y - 要繪制矩形的 y 坐標。
width - 要繪制矩形的寬度。
height - 要繪制矩形的高度。
實心的如下
fillRect
public abstract void fillRect(int x,
int y,
int width,
int height)填充指定的矩形。該矩形左邊緣和右邊緣分別位于 x 和 x + width - 1。上邊緣和下邊緣分別位于 y 和 y + height - 1。得到的矩形覆蓋 width 像素寬乘以 height 像素高的區(qū)域。使用圖形上下文的當前顏色填充該矩形。
參數:
x - 要填充矩形的 x 坐標。
y - 要填充矩形的 y 坐標。
width - 要填充矩形的寬度。
顏色g.setColor(參數);
畫g.fillRect(參數);
兄弟幫你寫了一個:
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);
}
}
直接說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 {
ArrayListRectangle list;
Rectangle current;
public PaintingPanel() {
list = new ArrayListRectangle();
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);
}
}
}
你的代碼有問題,你的類本身是frame,你也在類中繪制,但是你卻沒有顯示,而是另外定義了一個frame來顯示,你修改一下:
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
public class FrameTest extends Frame {
/**
* @param args
*/
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(100, 100, 30, 30);
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
//repaint();
}
FrameTest()
{
super("title");
setLocation(100,100);
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameTest ft=new FrameTest();
}
}
這樣應該沒問題了。
網頁標題:Java代碼畫矩形 在JavaScript中繪制矩形
網頁URL:http://jinyejixie.com/article0/docppoo.html
成都網站建設公司_創(chuàng)新互聯,為您提供軟件開發(fā)、微信小程序、小程序開發(fā)、云服務器、響應式網站、靜態(tài)網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯