当前位置:首页 » 编程语言 » java画矩形

java画矩形

发布时间: 2022-06-17 12:45:29

java 用字符"*"画矩形

for (i = 0; i < n; ++i)
System.out.print("*");
System.out.println();
for (i = 2; i < n; ++i) {
System.out.print("*");

for (j = 2; j < n; ++j)
System.out.print(" ");
System.out.print("*");
System.out.println();
}
for (i = 0; i < n; ++i)
System.out.print("*");
不需要用到判断语句,像我这个样子就能得到,这是双重循环的训练

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

packagetest.xxl;

importjava.awt.Button;
importjava.awt.Color;
importjava.awt.Cursor;
importjava.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;

importjavax.swing.JFrame;

,ActionListener{

privatestaticintx=0;
privatestaticinty=0;
privatestaticintw=0;
privatestaticinth=0;

privatestaticColorc;

//真为圆,假为方
privatebooleanflag=false;

=1L;

publicDemo0617(){
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);

Buttonb1,b2,b3,b4,b5,b6,b7,b8,b9;
b1=newButton("红色");
b1.setBounds(0,0,100,30);
b1.setBackground(Color.RED);
b1.addActionListener(this);
this.add(b1);

b2=newButton("黑色");
b2.setBounds(110,0,100,30);
b2.setBackground(Color.BLACK);
b2.addActionListener(this);
this.add(b2);

b3=newButton("黄色");
b3.setBounds(220,0,100,30);
b3.setBackground(Color.YELLOW);
b3.addActionListener(this);
this.add(b3);

b4=newButton("蓝色");
b4.setBackground(Color.BLUE);
b4.setBounds(330,0,100,30);
b4.addActionListener(this);
this.add(b4);

b5=newButton("橡皮擦");
b5.setBounds(0,40,100,30);
b5.addActionListener(this);
this.add(b5);

b6=newButton("撤销");
b6.setBounds(110,40,100,30);
b6.addActionListener(this);
this.add(b6);

b7=newButton("全部删除");
b7.setBounds(220,40,100,30);
b7.addActionListener(this);
this.add(b7);

b8=newButton("圆形");
b8.setBounds(0,80,100,30);
b8.addActionListener(this);
this.add(b8);

b9=newButton("矩形");
b9.setBounds(110,80,100,30);
b9.addActionListener(this);
this.add(b9);


}

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
newDemo0617();
}

@Override
publicvoidpaint(Graphicsg){
if(c==null)
c=g.getColor();
g.setColor(c);
if(flag){
g.fillOval(x,y,w,h);
}else{
g.fillRect(x,y,w,h);
}

}

publicvoidclear(Graphicsg){
g.setColor(Color.WHITE);
g.clearRect(0,0,440,500);
}

/**
*单击
*/
@Override
publicvoidmouseClicked(MouseEvente){


}

/**
*按下
*/
@Override
publicvoidmousePressed(MouseEvente){

x=e.getX();
y=e.getY();

}

/**
*松开
*/
@Override
publicvoidmouseReleased(MouseEvente){
intx=e.getX();
inty=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
publicvoidmouseEntered(MouseEvente){
}

/**
*鼠标移除事件
*/
@Override
publicvoidmouseExited(MouseEvente){
}

@Override
publicvoidactionPerformed(ActionEvente){
switch(e.getActionCommand().hashCode()){
case1038352:
//红色
c=Color.RED;
break;
case1293761:
//黑色
c=Color.BLACK;
break;
case1293358:
//黄色
c=Color.YELLOW;
break;
case1087797:
//蓝色
c=Color.BLUE;
break;
case27138585:
//橡皮擦
c=Color.WHITE;
break;
case836828:
Graphicsgraphics=getGraphics();
graphics.setColor(Color.WHITE);

if(flag){
graphics.fillOval(x,y,w,h);
}else{
graphics.fillRect(x,y,w,h);
}
break;
case657183940:
//全部删除
clear(getGraphics());
break;
case715036:
//圆形
flag=true;
break;
case976025:
//矩形
flag=false;
break;
default:
System.out.println(e.getActionCommand().hashCode());
break;
}
}

}

③ java Jpanel上画矩形

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 像素高的区域。使用图形上下文的当前颜色填充该矩形。

参数:
x - 要填充矩形的 x 坐标。
y - 要填充矩形的 y 坐标。
width - 要填充矩形的宽度。

颜色g.setColor(参数);
画g.fillRect(参数);

④ java中如何用画布绘制矩形图形

importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
classOvalcanvasextendsCanvas
{
intN=10;
intx[]=newint[N];
inty[]=newint[N];
Ovalcanvas()
{
setSize(300,200);
setBackground(Color.cyan);
}
publicvoidsetOval(int[]x,int[]y,intN)
{
this.N=N;
for(inti=0;i<N;i++)
{
this.x[i]=x[i];
this.y[i]=y[i];
}
}
publicvoidpaint(Graphicsg)
{
g.drawPolygon(x,y,N);
}
}
publicclassExample6_
{
Ovalcanvascanvas;
TextFieldin_N;
Buttonbtn;
Example6_7()
{
super("画布上绘制多边形");
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
in_N=newTextField(6);
setLayout(newFlowLayout());
add(newLabel("请输入变数:"));
add(in_N);
btn=newButton("确定");
btn.addActionListener(this);
add(btn);
canvas=newOvalcanvas();
add(canvas);
validate();
}
publicvoidactionPerformed(ActionEvente)
{
intN=Integer.parseInt(in_N.getText());
intx[]=newint[N];
inty[]=newint[N];
for(inti=0;i<N;i++)
{
x[i]=(int)(Math.random()*200);
y[i]=(int)(Math.random()*200);
}
canvas.setOval(x,y,N);
canvas.repaint();
}
publicstaticvoidmain(String[]args)
{
newExample6_7();
}
}

⑤ java 绘图时如何用特定画笔字符绘画一个矩形

publicclassYugi{
publicstaticvoidmain(String[]args){
intc=10;
intk=5;
for(inti=0;i<k;i++){
for(intj=0;j<c;j++){
System.out.print("*");
}
System.out.println();
}
}
}

⑥ java 用字符"*"&输入语句画矩形

import java.util.*;
public class tuxing {
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
int w=sc.nextInt();
int h=sc.nextInt();
for(int i=1;i<=w;i++)
System.out.print("*");

System.out.println();

for(int i=1;i<=h-2;i++)
{
System.out.print("*");
for(int j=1;j<=w-2;j++)
System.out.print(" ");
System.out.println("*");
}
for(int i=1;i<=w;i++)
System.out.print("*");

System.out.println();
}
}

已经给楼主写出来楼主想要的了..

呵呵...祝楼主早日成功哈!!

输入的数据是
10
7

呵呵....

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

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

⑧ java 画个矩形框。。。

你运行看看,不知道是不是你想要的效果......

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class ChangeTest extends JFrame {
Canva can = null;
Graphics g = null;
public ChangeTest () {
init ();
}
public void init () {
this.setTitle("Simple Display");
this.setLayout(null);
can = new Canva();
can.setBounds(60, 100, 50, 50);

can.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e) {
super.mouseEntered(e);
g = can.getGraphics();
g.setColor(Color.GREEN);
can.update(g);
}
public void mouseExited(MouseEvent e) {
super.mouseExited(e);
g = can.getGraphics();
g.setColor(Color.RED);
can.update(g);
}
});
this.add(can);

Toolkit kit = Toolkit.getDefaultToolkit();
this.setLocation(kit.getScreenSize().width / 3,
kit.getScreenSize().height / 3);
this.setSize(new Dimension(432, 500));
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

class Canva extends Canvas {
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillRect(0, 0, 50, 50);
}
public void update(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 50, 50);
}
}
public static void main(String args[]){
new ChangeTest();
}
}

⑨ java 画一个矩形 为何报错

你能发父类Shape2
来看看吗,这是因为java.awt.Rectangle类没有这个构造函数,所以匹配了所有的构造函数都没有匹配到,才提示这个找不到构造器的错误,还有就是Rectangle构造出来的事要int类型的,不是double类型的

⑩ 如何用java画一个矩形,要求左上坐标为20.30.宽为120.高为30.

import java.awt.*;
import javax.swing.*;

public class Rectangle extends JFrame {

public void CreateJFrame(String title) {
JFrame frame = new JFrame();
Container container = frame.getContentPane();
JLabel jl = new JLabel("窗体");
container.add(jl);
jl.setHorizontalAlignment(SwingConstants.CENTER);
frame.setBounds(20, 30, 120, 30);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
new Rectangle().CreateJFrame("创建窗体");
}
}

热点内容
编程函数总结 发布:2025-02-06 20:09:11 浏览:314
编程obj 发布:2025-02-06 19:59:52 浏览:842
津贴脚本 发布:2025-02-06 19:44:10 浏览:739
好分数里如何修改密码 发布:2025-02-06 19:42:30 浏览:155
mysql存储过程判断 发布:2025-02-06 19:40:15 浏览:853
bat编译器的作用 发布:2025-02-06 19:26:54 浏览:344
phpajaxsession 发布:2025-02-06 19:20:56 浏览:623
西安java学习 发布:2025-02-06 19:15:44 浏览:623
微信电影源码网站 发布:2025-02-06 18:55:21 浏览:934
本地建mysql数据库 发布:2025-02-06 18:54:23 浏览:762