「java完美拼出矩形」java画一个矩形

博主:adminadmin 2022-12-20 20:27:09 66

今天给各位分享java完美拼出矩形的知识,其中也会对java画一个矩形进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA怎么画出一个任意大小的圆形和矩形

package test.xxl;

import java.awt.Button;

import java.awt.Color;

import java.awt.Cursor;

import java.awt.Graphics;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class Demo0617 extends JFrame implements MouseListener,ActionListener{

private static int x = 0 ; 

private static int y = 0 ;

private static int w = 0 ;

private static int h = 0 ;

private static Color c ;

// 真为圆,假为方

private boolean flag = false ;

private static final long serialVersionUID = 1L;

public Demo0617(){

this.setSize(440, 500) ;

this.setVisible(true) ;

this.setLayout(null) ;

this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)) ;

this.setResizable(false);//不能改变窗体大小

this.setBackground(Color.WHITE) ;

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

this.addMouseListener(this) ;

this.getContentPane().setBackground(Color.WHITE) ;

Button b1,b2,b3,b4,b5,b6,b7,b8,b9 ;

b1 = new Button("红色") ;

b1.setBounds(0, 0, 100, 30) ;

b1.setBackground(Color.RED) ;

b1.addActionListener(this) ;

this.add(b1) ;

b2 = new Button("黑色") ;

b2.setBounds(110, 0, 100, 30) ;

b2.setBackground(Color.BLACK) ;

b2.addActionListener(this) ;

this.add(b2) ;

b3 = new Button("黄色") ;

b3.setBounds(220, 0, 100, 30) ;

b3.setBackground(Color.YELLOW) ;

b3.addActionListener(this) ;

this.add(b3) ;

b4 = new Button("蓝色") ;

b4.setBackground(Color.BLUE) ;

b4.setBounds(330, 0, 100, 30) ;

b4.addActionListener(this) ;

this.add(b4) ;

b5 = new Button("橡皮擦") ;

b5.setBounds(0, 40, 100, 30) ;

b5.addActionListener(this) ;

this.add(b5) ;

b6 = new Button("撤销") ;

b6.setBounds(110, 40, 100, 30) ;

b6.addActionListener(this) ;

this.add(b6) ;

b7 = new Button("全部删除") ;

b7.setBounds(220, 40, 100, 30) ;

b7.addActionListener(this) ;

this.add(b7) ;

b8 = new Button("圆形") ;

b8.setBounds(0, 80, 100, 30) ;

b8.addActionListener(this) ;

this.add(b8) ;

b9 = new Button("矩形") ;

b9.setBounds(110, 80, 100, 30) ;

b9.addActionListener(this) ;

this.add(b9) ;

}

/**

 * @param args

 */

public static void main(String[] args) {

new Demo0617() ;

}

@Override

public void paint(Graphics g) {

if(c == null)

c = g.getColor();

g.setColor(c);

if(flag){

g.fillOval(x, y, w, h);

} else {

g.fillRect(x, y, w, h) ;

}

}

public void clear(Graphics g){

g.setColor(Color.WHITE) ;

g.clearRect(0, 0, 440, 500) ;

}

/**

 * 单击

 */

@Override

public void mouseClicked(MouseEvent e) {

}

/**

 * 按下

 */

@Override

public void mousePressed(MouseEvent e) {

x = e.getX() ;

y = e.getY() ;

}

/**

 * 松开

 */

@Override

public void mouseReleased(MouseEvent e) {

int x = e.getX() ;

int y = e.getY() ;

if(x  this.x){

w = x - this.x ;

} else {

w = this.x - x ;

}

if(y  this.y){

h = y - this.y ;

} else {

h = this.y - y ;

}

paint(getGraphics()) ;

}

/**

 * 鼠标进入事件

 */

@Override

public void mouseEntered(MouseEvent e) {

}

/**

 * 鼠标移除事件

 */

@Override

public void mouseExited(MouseEvent e) {

}

@Override

public void actionPerformed(ActionEvent e) {

switch (e.getActionCommand().hashCode()) {

case 1038352:

// 红色

c = Color.RED ;

break;

case 1293761:

// 黑色

c = Color.BLACK ;

break;

case 1293358:

// 黄色

c = Color.YELLOW ;

break;

case 1087797:

// 蓝色

c = Color.BLUE ;

break;

case 27138585:

// 橡皮擦

c = Color.WHITE ;

break ;

case 836828:

Graphics graphics = getGraphics() ;

graphics.setColor(Color.WHITE) ;

if(flag){

graphics.fillOval(x, y, w, h) ;

} else {

graphics.fillRect(x, y, w, h) ;

}

break;

case 657183940:

// 全部删除

clear(getGraphics()) ;

break;

case 715036:

// 圆形

flag = true ;

break;

case 976025:

// 矩形

flag = false ;

break;

default:

System.out.println(e.getActionCommand().hashCode());

break ;

}

}

}

用Java如何在屏幕上(桌面上)画出一个矩形

在java.awt.Graphics类中有绘制矩形的方法.用drawRect(int x, int y, int width, int height)方法画出,,也可以用fillRect()画出以当前颜色填充的矩形.

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编一个矩形!

画矩形:

public void drawRect(int x, int y, int width, int height)

x - 要绘制矩形的 x 坐标。y - 要绘制矩形的 y 坐标。

width - 要绘制矩形的宽度。height - 要绘制矩形的高度。

drawLine(x,y,m,z)

x - 第一个点的 x 坐标。y - 第一个点的 y 坐标。

m - 第二个点的 x 坐标。z - 第二个点的 y 坐标。

Java编写一个矩形类,至少包含以下方法:

import java.awt.Point;

public class Rectangle {

private int widthT = 0;

private int heightT = 0;

Point point = new Point();

public Rectangle(int width, int height, int centerX, int centerY) {

widthT = width;

heightT = height;

point.x = centerX;

point.y = centerY;

}

public int width() {

return widthT;

}

public int height() {

return heightT;

}

public int centerX() {

return point.x;

}

public int centerY() {

return point.y;

}

}

麻烦采纳呦~~~亲

关于java完美拼出矩形和java画一个矩形的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-20,除非注明,否则均为首码项目网原创文章,转载请注明出处。