java畫矩形
① 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("創建窗體");
}
}