当前位置:首页 > 代码 > 正文

java颜色代码(java颜色代码对照表图片)

admin 发布:2022-12-19 10:27 96


本篇文章给大家谈谈java颜色代码,以及java颜色代码对照表图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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关键字用什么颜色

红色。在eclipse中,关键字有特定的颜色区分,为红色。红色字体是java语法的关键字。 蓝色字体是代码里面定义的常量,或者字符串值。 黑色字体就是代码了。

用java声明一个颜色类Color

import java.awt.*;

import java.awt.event.*;

public class adjustcolor implements AdjustmentListener, WindowListener {

Frame f=new Frame("调整颜色");

Label l1=new Label("调整滚动条,会改变初始颜色",Label.CENTER);

Label l2=new Label("此处显示颜色值",Label.CENTER);

Label l3=new Label("红",Label.CENTER);

Label l4=new Label("绿",Label.CENTER);

Label l5=new Label("蓝",Label.CENTER);

Scrollbar scr1=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);

Scrollbar scr2=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);

Scrollbar scr3=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);

public adjustcolor(){

f.add(l1);

f.add(l2);

f.add(l3);

f.add(l4);

f.add(l5);

f.add(scr1);

f.add(scr2);

f.add(scr3);

f.setSize(400,350);

f.setVisible(true);

f.addWindowListener(this);

f.setResizable(false);

l1.setBackground(Color.GREEN);

scr1.setBounds(35,225,360,25);

scr2.setBounds(35,255,360,25);

scr3.setBounds(35,285,360,25);

l1.setBounds(0,0,400,200);

l2.setBounds(0,310,400,30);

l3.setBounds(0,225,30,30);

l4.setBounds(0,255,30,30);

l5.setBounds(0,285,30,30);

scr1.addAdjustmentListener(this);

scr2.addAdjustmentListener(this);

scr3.addAdjustmentListener(this);

l1.setBackground(Color.GREEN);

scr1.setBackground(Color.RED);

scr2.setBackground(Color.GREEN);

scr3.setBackground(Color.blue);

}

public void adjustmentValueChanged(AdjustmentEvent e){

int a=scr1.getValue();

int b=scr2.getValue();

int c=scr3.getValue();

l1.setBackground(new Color(a,b,c)) ;

l2.setText("红"+" "+"绿"+" "+"蓝"+" "+a+" "+b+" "+c);

l1.setText(null);

}

public static void main(String[] args){

new adjustcolor();

}

public void windowActivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowClosed(WindowEvent arg0) {

}

public void windowClosing(WindowEvent arg0) {

System.exit(0);

}

public void windowDeactivated(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {

// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {

// TODO Auto-generated method stub

}

}

这是源代码 应该是你想要的

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颜色代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java颜色代码对照表图片、java颜色代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/6672.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载