javarect
❶ Graphics.drawRect() 怎麼用 請舉個例給我把. 是java編程
drawRect
public void drawRect(int x,
int y,
int width,
int height)繪制指定矩形制的邊框。矩形的左邊和右邊位於 x 和 x + width。頂邊和底邊位於 y 和 y + height。使用圖形上下文的當前顏色繪制該矩形。
參數:
x - 要繪制矩形的 x 坐標。
y - 要繪制矩形的 y 坐標。
width - 要繪制矩形的寬度。
height - 要繪制矩形的高度。
❷ 用java定義一個「矩形」(Rect)類。要求如下: 可以生成具有特定長和寬
/**
*Java技術交流:
*Java技術指導:201856584
*@authorGRcoder
*/
publicclassRect{
privatelongrectLong;
privatelongrectWide;
publicRect(){
super();
}
publicRect(longrectLong,longrectWide){
super();
this.rectLong=rectLong;
this.rectWide=rectWide;
}
//面積
publiclonggetArea(){
returnthis.rectLong*this.rectWide;
}
//周長
publiclonggetPerimeter(){
return(this.rectLong+this.rectWide)*2;
}
publiclonggetRectLong(){
returnrectLong;
}
publicvoidsetRectLong(longrectLong){
this.rectLong=rectLong;
}
publiclonggetRectWide(){
returnrectWide;
}
publicvoidsetRectWide(longrectWide){
this.rectWide=rectWide;
}
@Override
publicStringtoString(){
return"Rect[rectLong="+rectLong+",rectWide="+rectWide+"]";
}
}
❸ java中Rectangle 的用法
Rectangle 指定坐標空間中的一個區域,通過坐標空間中 Rectangle 對象左上方的點 (x,y)、寬度和高度可以定義這個區域。
其構造函數Rectangle(int x, int y, int width, int height)
height
Rectangle 的高度。
width
Rectangle 的寬度。
x
Rectangle 左上角的 X 坐標。
y
Rectangle 左上角的 Y 坐標。
❹ 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編寫一個Rect類
public class Rect{
private int length;
private int width;
public Rect(int length, int width) {
this.length = length;
this.width = width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public void countArea(){
int area = this.length * this.width;
System.out.println("矩形的面積為 : "+area);
}
public void countGirth(){
int girth = (this.length + this.width) * 2;
System.out.println("矩形的周長為 : "+girth);
}
public static void main(String[] args) {
Rect rect = new Rect(20, 30);
rect.countArea();
rect.countGirth();
}
}
❻ java drawRect怎麼定位
你所謂的「吃掉」是因為描繪的起點(矩形左上角那點)是相對Frame的左上角開始的,自然包括了標題欄和Frame邊框的像素。我將你的代碼完善了下,使用JPanel來實現你想要的效果,貼出代碼和效果圖。有什麼問題可以問我。Email:[email protected]
Code:
packagecn.gengjie.swing.test;
importjava.awt.BasicStroke;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
{
=1L;
privateJPanelpanel;
publicRectTest(){
this.setTitle("DrawRectTest");
this.panel=newJPanel();
this.add(panel);
this.setSize(450,420);
panel.setBounds(this.getBounds());
this.setVisible(true);
}
publicvoiddrawRect(){
Graphics2Dg=(Graphics2D)panel.getGraphics().create();
g.setColor(Color.RED);
g.setStroke(newBasicStroke(1f));
g.drawRect(0,0,200,250);
g.setColor(Color.GRAY);
g.setStroke(newBasicStroke(2f));
g.drawLine(0,0,200,250);
g.drawString("Heyguy!Thisisjustthe"+"rightoneyouwanna!",0,265);
}
publicvoidpaint(Graphicsg){
super.paint(g);
drawRect();
}
publicstaticvoidmain(Stringargs[]){
newRectTest().drawRect();
}
}
❼ java 畫矩形
你的代碼有問題,你的類本身是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 Rect.java輸出問題
使用Java,double 進行運算時,經抄常出現精度丟失的問題,總是在一個正確的結果左右偏0.0000**1。 特別在實際項目中,通過一個公式校驗該值是否大於0,如果大於0我們會做一件事情,小於0我們又處理其他事情。 這樣的情況通過double計算出來的結果去和0比較大小,尤其是有小數點的時候,經常會因為精度丟失而導致程序處理流程出錯。
這里有篇文章,你可以去看看怎麼處理
http://zhaow-381002134.javaeye.com/blog/420369
❾ java矩形類TRectangleangle
/**
* 已知PPoint 類表示一個點。矩形類TRectangle的成員變數包括:
* 左上角點p,X軸邊長和Y軸邊長b ;
* TRectangle有一個求對角線長度的成員方法。
* 下列程序實例化了一個左上角在(10,10) ,
* a為30,b 為40的TRectangle對象,並求出了它的對角線長度的和它右下角的X坐標。
*/
public class Test {
public static void main(String[] args) {
TRectangle tRectangle = new TRectangle(10, 10, 30, 40);
System.out.println("對角線長度: " + tRectangle.getDiagonal());
PPoint rightBottomPoint = tRectangle.getRightBottomPoint();
System.out.println("對角線點: x=" + rightBottomPoint.getX() + ", y=" + rightBottomPoint.getY());
}
}
class PPoint {
private double x;
private double y;
public PPoint(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
class TRectangle {
private PPoint leftTopPoint;
private double a;
private double b;
public TRectangle(double x, double y, double a, double b) {
this.leftTopPoint = new PPoint(x, y);
this.a = a;
this.b = b;
}
public double getDiagonal() {
return Math.sqrt(a * a + b * b);
}
public PPoint getRightBottomPoint() {
double x = leftTopPoint.getX() + a;
double y = leftTopPoint.getY() - b;
return new PPoint(x, y);
}
}
❿ Java定義一個Rectangle類
classRectangle{
privatedoublewidth;
privatedoubleheight;
publicRectangle(doublewidth,doubleheight){
.width=width;
this.height=height;
}
publicdoublegetC(){
return(width+height)*2;
}
publicdoublegetS(){
returnwidth*height;
}
publicdoublegetWidth(){
returnwidth;
}
publicvoidsetWidth(doublewidth){
this.width=width;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
}
publicclassTest{
publicstaticvoidmain(String[]args){
Rectanglerect=newRectangle(4,5);
System.out.println("周長="+rect.getC()+" 面積="+rect.getS());
}
}