java修改颜色的代码 java怎么换颜色

java设定背景颜色

本来是在drawcomponent这个里边使用setBackground,你想啊drawcomponent是继承JComponent的所以它是一个容器,所以它同样有setBackground这个方法来设置它的背景颜色

成都创新互联公司服务项目包括庆城网站建设、庆城网站制作、庆城网页制作以及庆城网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,庆城网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到庆城省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

但是因为你在设置它本身为一个画布,因为你用了paintComponent(Graphics g)

这个方法,所以setBackground这个方法即使你用了也看不到很大的效果。但是有一种取代的方法就是在paintComponent(Graphics g)方法中首先就用Graphics 所含有的方法g.setColor(Color.black);来设置背景颜色再用g.fillRect(0, 0, this.getWidth(), this.getHeight());来填满整个容器,这就达到了设置背景目的。然后你再g.setColor(其他颜色);来绘制其它图形.

具体代码:(在你以上的代码上修改了点)

public void paintComponent(Graphics g)

{

Graphics2D g2=(Graphics2D)g;

g.setColor(Color.black);//这里设置背景颜色

g.fillRect(0, 0, this.getWidth(), this.getHeight());//这里填充背景颜色

double x=100;

double y=100;

double w=200;

double h=150;

Rectangle2D rect=new Rectangle2D.Double(x,y,w,h);

g2.setPaint(Color.white);//这里是你设置其他笔触颜色

g2.draw(rect);

Ellipse2D ellipse=new Ellipse2D.Double();

ellipse.setFrame(rect);

g2.draw(ellipse);

Point2D p1=new Point2D.Double(x-40,y-30);

Point2D p2=new Point2D.Double(x+w+40,y+h+30);

g2.draw(new Line2D.Double(p1,p2));

double centerx=rect.getCenterX();

double centery=rect.getCenterY();

double radius=150;

Ellipse2D circle=new Ellipse2D.Double();

circle.setFrameFromCenter(centerx,centery,centerx+125,centery+125);

g2.draw(circle);

}

测试结果图

java 编程 背景颜色的改变

**************************************************************

新建一个类ChangeColor.java,代码如下:

**************************************************************

import java.awt.Color;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

/**

* @author Godwin

* @version 2010-05-16

*/

public class ChangeColor extends JFrame implements MouseMotionListener {

public ChangeColor() {

this.setTitle("Change Color");

this.setBounds(300, 200, 400, 300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

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

this.addMouseMotionListener(this);

}

public void mouseMoved(MouseEvent e) {

if (e.getX()  (this.getWidth() / 2)) {

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

} else {

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

}

}

public void mouseDragged(MouseEvent e) {

}

public static void main(String[] args) {

new ChangeColor();

}

}

**************************************************************

运行结果如下:

**************************************************************

写一个java程序,使其画一个正方形并改变它的颜色(每秒钟改变一次)。求完整代码谢谢

按照你的要求,编写的Java程序如下

import java.awt.Color;

import java.awt.Graphics;

import javax.swing.JFrame;

public class F extends JFrame implements Runnable{

Color[] colors = {Color.red,Color.orange,Color.yellow,Color.green,Color.cyan,Color.blue,Color.magenta,Color.black};  

int i=0;

F(){

setTitle("变色正方形");

setSize(400, 400);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setVisible(true);

}

public void paint(Graphics g){

g.setColor(colors[i]);

g.drawRect(100,100,200,200);

}

@Override

public void run() {

while(true){

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

if(i7)

i++;

else

i=0;

repaint();

}

}

public static void main(String[] args) {

F f=new F();

Thread t=new Thread(f);

t.start();

}

}

运行结果

java的小应用程序中的设置颜色怎么编写,麻烦您了

import java.awt.*;

import java.awt.event.*;

public class Test extends Frame implements ActionListener {

Button button1, button2;

Panel panel1,panel2;

public static void main(String[] args) {

new Test();

}

Test(){

init();

setBounds(100,100,400,300);

setVisible(true);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

void init(){

button1 = new Button("红颜色");

button2 = new Button("绿颜色");

panel1 = new Panel();

panel2 = new Panel();

add(BorderLayout.CENTER,panel1);

panel2.add(button1);

panel2.add(button2);

add(BorderLayout.SOUTH,panel2);

button1.addActionListener(this);

button2.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

if(e.getSource() == button1) {

panel1.setBackground(Color.red);

} else if(e.getSource() == button2) {

panel1.setBackground(Color.green);

}

}

}

名称栏目:java修改颜色的代码 java怎么换颜色
地址分享:https://www.cdcxhl.com/article34/hicsse.html

成都网站建设公司_创新互联,为您提供定制网站微信小程序外贸网站建设做网站手机网站建设云服务器

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网站建设公司