當前位置:首頁 » 編程語言 » java圖形

java圖形

發布時間: 2022-01-10 04:13:29

⑴ 用java輸出以下圖形,怎麼做 * ** *** ****

實現思路:就是通過內層循環得到」*「的個數,外層循環控制換行。
for(int i=0;i<4;i++){
for(int j=0;j<=i;j++){
System.out.print("*");
}
System.out.println("");

}
備註:如果不想換行的話,直接去掉外層循環即可。

⑵ 如何用JAVA做出如下圖形 * *** *****

public class Test {
public static void main(String[] args) {
Test.print();
}
public static void print(){
int row = 3;//列印的行數

for(int i = 0; i < row; i++){
//循環列印每行前的空格
for(int j = 0; j < row - (i + 1); j++){
System.out.print(" ");
}
//循環列印每行的*
for(int j = 0; j < i*2+1; j++){
System.out.print("*");
}
//單行列印完成後換行
System.out.print(" ");
}
}
}

⑶ 用JAVA繪制各種函數圖形

import java.awt.*;
import java.applet.*;
import java.math.*;
import java.awt.event.*;

public class dffg extends Applet implements ActionListener
{
Button bb,bn;
TextField tt;
int aa;
public void init()
{
bb=new Button("畫圖");
bn=new Button("清除重畫");
tt=new TextField(5);
add(bb);
add(tt);
add(bn);
bb.addActionListener(this);
bn.addActionListener(this);

}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bb)
{

int aa=Integer.parseInt(tt.getText());
Graphics g=getGraphics();
for(int i=1;i<500;i++)
{
int x=i;
int y=(int)(Math.sin(aa*x*3.14/181)*100+150);
g.drawString("s",x,y);
}
}
if(e.getSource()==bn)
{
repaint();
}

}

}

⑷ 用JAVA寫一個簡單圖形類

public class Test013 {

/**
* 編寫一個圖形類MyGraphic。 1)它有兩個基本屬性:圖形線條的顏色String lineColor和圖形的填充顏色String
* fillColor。 2)設計矩形類CRectangle,有屬性double rLong和寬double rWidth, 使用方法 float
* calCircum()可以返回矩形的周長,使用方法float calSquare()可以返回矩形的面積。
* 編寫方法show(),顯示圖形的線條顏色和填充顏色,輸出面積和方法。 3)設計圓形類CCircle,定義屬性:半徑double
* radius,可以通過同名方法計算周長和面積。 編寫方法show(),顯示圖形的線條顏色和填充顏色,輸出面積和方法。
* 4)編寫出應用程序對CRectangle類和CCircle類進行驗證。 完成上述要求即可
*/
public static void main(String[] args) {
MyGraphic rectangle = new CRectangle(10, 5);
rectangle.setFillColor("紫色"); //設定矩形填充顏色
rectangle.setLineColor("白色"); //設定矩形線條顏色
rectangle.show();
System.out.println("矩形周長 = " + rectangle.calCircum());
System.out.println("矩形面積 = " + rectangle.calSquare());

MyGraphic circle = new CCircle(3);
circle.setFillColor("紅色");
circle.setLineColor("黃色");
circle.show();
System.out.println("園形周長 = " + circle.calCircum());
System.out.println("園形面積 = " + circle.calSquare());
}

}

/**
* 圖形類
*
*/
abstract class MyGraphic {
private String lineColor; // 圖形線條的顏色
private String fillColor; // 圖形的填充顏色

public String getLineColor() {
return lineColor;
}

public void setLineColor(String lineColor) {
this.lineColor = lineColor;
}

public String getFillColor() {
return fillColor;
}

public void setFillColor(String fillColor) {
this.fillColor = fillColor;
}

public MyGraphic(String lineColor, String fillColor) {
this.lineColor = lineColor;
this.fillColor = fillColor;
}

public MyGraphic() {
}

/**
* 顯示圖形的顏色
*/
public abstract void show();

/**
* 計算圖形的周長
*/
public abstract float calCircum();

/**
* 計算圖形的面積
*/
public abstract float calSquare();
}

/**
* 矩形類
*
*/
class CRectangle extends MyGraphic {

private double rLong; // 長
private double rWidth; // 寬

/**
* 通過構造函數為圖形的屬性賦值
*
* @param rLong
* @param rWidth
*/
public CRectangle(double rLong, double rWidth) {
this.rLong = rLong;
this.rWidth = rWidth;
}

/**
* @return 矩形的周長
*/
@Override
public float calCircum() {
return (float) (2 * (rLong + rWidth));
}

/**
* @return 矩形的面積
*/
@Override
public float calSquare() {
return (float) (rLong * rWidth);
}

@Override
public void show() {
System.out.println("矩形線條的顏色: " + super.getLineColor());
System.out.println("矩形填充顏色: " + super.getFillColor());
}

public double getrLong() {
return rLong;
}

public void setrLong(double rLong) {
this.rLong = rLong;
}

public double getrWidth() {
return rWidth;
}

public void setrWidth(double rWidth) {
this.rWidth = rWidth;
}
}

/**
* 圓形類
*
*/
class CCircle extends MyGraphic {
private double radius; // 圓形半徑

public CCircle(double radius) {
this.radius = radius;
}

/**
* @return 圓形的周長
*/
@Override
public float calCircum() {
return (float) (2 * Math.PI * radius);
}

/**
* @return 圓形的面積
*/
@Override
public float calSquare() {
return (float) (Math.PI * radius * radius);
}

@Override
public void show() {
System.out.println("圓形線條的顏色: " + super.getLineColor());
System.out.println("圓形填充顏色: " + super.getFillColor());
}

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;
}
}

⑸ Java編寫一個圖形界面

rcp(SWT/JFace)開發的要不要,同樣是java的

⑹ Java圖形

我試一下,你的direction只有一個方向,用一個變數記住2個參數是不合適的。

需要同時跟蹤x和y上的方向,因為對於小球來說,實際上有4方向,而不是2個。或者你用記住一個角度也可以,以後碰壁之後,用三角形計算出另一個角度。每次移動時用三角函數sin(a)和cos(a)計算出x和y的步長。當在一個位置碰壁時只應該改變其中一個坐標的加減方向,而你改變direction就相當於同時改變了x和y的加減方向。


下面部分代碼修改了。

	privateintdirectionX;
privateintdirectionY;

publicMovingComponent(){
x=0;
y=0;
directionX=1;
directionY=1;
}

publicvoidpaintComponent(Graphicsg){
g.setColor(Color.GREEN);
g.fillOval(x,y,WIDTH,HEIGHT);
}

publicvoidmove(){
intheight=getHeight();
intwidth=getWidth();

//先試探性移動,記住兩個方向的坐標變化本身互不相關的。
intnextX=x+2*directionX;
intnextY=y+2*directionY;

if(nextX<0||nextX>width-WIDTH){
//碰壁後調整方向,然後重新計算位置x。
directionX=-1*directionX;
nextX=x+2*directionX;
}

if(nextY<0||nextY>height-HEIGHT){
//碰壁後調整方向,然後重新計算位置y.
directionY=-1*directionY;
nextY=y+2*directionY;
}

x=nextX;
y=nextY;

repaint();
}

⑺ 關於java中畫圖形的paint方法

代碼如下:

/**分析下例:我們只是new了一個對象並沒有調用Paint()方法那為什麼會畫出圖呢?
* Graphics這個類的對象就是一隻畫筆,當某容器調用paint()時就會在該容器中畫圖。
* 當窗口產生時本身就存在一隻畫筆,我們只需要拿到畫筆重寫Paint()便可以隨心作畫。
*每次需要重畫的時候就會自動調用paint(Graphics g)(什麼時候需要重畫呢?如當窗口被覆蓋又重新置頂時,當窗口最小化又最大化時等等)
*/

總結:我們想要在容器中畫圖時只需要做的就是:在該容器中重寫Paint()系統會自動傳給我們畫筆,自動調用paint方法按照我們的意願進行作畫。
public class TestGraphics extends Frame. {
public static void main(String []args) {
new TestGraphics("畫圖",100,100,200,200,Color.white);
}
public TestGraphics(String s,int x,int y,int w,int h,Color c) {
super(s);
this.setBounds(x, y, w, h);
this.setBackground(c);
this.setVisible(true);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.magenta);
g.fillOval(100, 100, 50, 50);
g.setColor(Color.green);
g.fill3DRect(60, 100, 50, 50, false);
g.setColor(c);
}
}

import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
原理是:在Frame中增加成員變數-容器ArrayList,用它來容納點,每次點擊滑鼠就觸發了事件:往容器中添加一個點,然後立即調用repaint方法強制畫出容器中所有的點來
所以我們利用容器來"裝"點,然後通過iterator來遍歷畫出所有的點。

適配器類:使用適配器類可以只重寫我們需要的方法

因為這些適配器類都已經實現了相應的介面即把所有的方法都空實現了一遍 我們只需要重寫我們需要的方法即可

repaint -調用- update() - 調用- paint();

*/

public class MyFrame. extends Frame. {
ArrayList<Point>al ;//泛型指定容器中只能放入Point
public MyFrame(String s) {
super(s);
al =new ArrayList<Point>();
this.setBounds(100, 100, 200, 200);
this.setBackground(Color.darkGray);
this.setVisible(true);
this.addMouseListener(new MouseAdapter(){//匿名類
@Override
public void mousePressed(MouseEvent e) {
MyFrame. f = (MyFrame)e.getSource();//e是事件,e.getSource()是獲取事件源即窗口 f
f.al.add(new Point(e.getX(),e.getY())); //而e.getX(),e.getY()則是獲取事件發生的x,y坐標
repaint();//每次點擊滑鼠觸發事件時都有了新的點,所以強制要求重畫,才能立刻顯示出該點否則只有窗口被最小化又最大化後才能看到剛才的點
}
});

//匿名類:在參數處我們傳遞的是new WindowAdapter() {匿名類體} );他沒有名字,我們把它當成WindowAdapter來用,為什麼可以這樣呢?因為語法上規定了匿名類要麼是實現了前面的介面,要麼是從前面的類繼承,就著前面父類的名字來寫類體。當此類與其他類關系不大時可以用匿名類
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void paint(Graphics g) {
Iterator <Point>it= al.iterator();//泛型指定取出元素時只能是point
while(it.hasNext()) {
Point p = it.next();//由於使用泛型這時候就不用強制轉換了
Color c = g.getColor();//保護原有顏色

g.setColor(Color.pink);
g.fillOval(p.x-6, p.y-6, 12, 12);
g.setColor(c);
}
}
public static void main(String []args) {
new MyFrame("點擊");
}

}

⑻ java輸出圖形

public static void main(String[] args) {
版int n=10; //可以通過調整n值,調整輸出菱形的大小。n=行數權+1
for(int i=1;i<n;i++){
for(int j=1;j<=(i<n/2?n/2-i:i-n/2);j++){
System.out.print(" ");
}
for(int j=1;j<(i<n/2?2*i:2*(n-i));j++){
System.out.print("*");
}
for(int j=1;j<=(i<n/2?n/2-i:i-n/2);j++){
System.out.print(" ");
}
System.out.println();
}
}

⑼ java圖形界面

落伍了嗎,
db2 的客戶端窗口,
oracle 的安裝 ,客戶端窗口 不都是java做的?
實際上oracle資料庫本身就是java做的,從這一點你就可以看出java語言的強大功能。
java 可以開發 控制台程序,窗口程序,web伺服器端程序,移動設備嵌入式程序等,別的語言能做的,java都能做。

⑽ java畫圖形

當s = 0 ,畫出填滿的圓形
s = 1 ,畫出線
s = 2 ,畫出菱形
s = 3 ,畫出三角形
s = 4 ,畫出正方形
s = 5 ,畫出五邊形
s = 6, 畫出六邊形

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:765
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:664
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:311
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:289
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:817
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:162
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:94
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:507
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:658
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:481