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());
}
}