package DevoirObservateurPrj.src;
/******************************************************
Nom du fichier : JDé.java
Date crée : Jun 24, 2004
Date dern. modif. aaaa-mm-jj
*******************************************************
Historique des modifications
*******************************************************
Jun 24, 2004 Version initiale
*******************************************************/
//package devoir;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.JComponent;
/**
* @author Cris Fuhrman
* Code based on
* http://java.sun.com/docs/books/tutorial/uiswing/QandE/example-1dot4/XMarksTheSpot.java
*/
public class JDé extends JComponent implements Observateur {
Dimension preferredSize = null;
int Val_dé=0;
//Dé dé;
public JDé(Object o) {
((Dé)o).attacher(this);
setOpaque(true);
//Set the border using either a MatteBorder (standard, used in
//Exercise 1) or a StripeBorder (custom, used in Exercise 3).
//You can find the exercises here:
// http://java.sun.com/docs/books/tutorial/uiswing/QandE/questions-ch6.html
//setBorder(BorderFactory.createMatteBorder(5, 5, 5, 5, this.getForeground()));
//setBorder(new StripeBorder());
}
/**
* <code>paintComponent</code> contient le code pour le dessin
* "fait sur commande"
*
* @param g a <code>Graphics</code> value
*/
@Override
public void notifier(Object o) {
// TODO Auto-generated method stub
if (o!=null){
Dé dé_obj = (Dé)o;
Val_dé= dé_obj.getValeurActuelle();
}
}
public void paintComponent(Graphics g) {
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(getForeground());
}
/*
* Si la forme (attribut de la classe principale ici) n'est pas
* nulle, on la dessine
*/
if (Val_dé != 0) {
// faire un cast (transtypage) en Graphics2D depour avoir plus de fonctionnalité
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// theColor = une variable de la classe principale
/*
* On dessine la valeur du dé simplement
*/
//g2d.drawString(String.valueOf(dé.getValeurActuelle()), 50, 50);
drawDé(g2d);
}
}
/**
* Code réutilisé de
* http://www.java-help.com/Applet01.txt
* @param g2d
*/
private void drawDé(Graphics2D g) {
g.setColor(this.getForeground());
Insets insets = getInsets();
Dimension dieSize = this.getSize();
int spotWidth = dieSize.width / 10;
int spotHeight = dieSize.height / 10;
switch (Val_dé) {
// Draw the spot pattern for a one.
case 1 :
g.fillOval(
(dieSize.width / 2) - spotWidth,
(dieSize.height / 2) - spotHeight,
spotWidth * 2,
spotHeight * 2);
break;
// Draw the spot pattern for a two.
case 2 :
g.fillOval(
(dieSize.width / 4) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
break;
// Draw the spot pattern for a three.
case 3 :
g.fillOval(
(dieSize.width / 4) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
(dieSize.width / 2) - spotWidth,
(dieSize.height / 2) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
break;
// Draw the spot pattern for a four.
case 4 :
g.fillOval(
(dieSize.width / 4) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
(dieSize.width / 4) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
break;
// Draw the spot pattern for a five.
case 5 :
g.fillOval(
(dieSize.width / 4) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
(dieSize.width / 2) - spotWidth,
(dieSize.height / 2) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
(dieSize.width / 4) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
break;
// Draw the spot pattern for a six.
case 6 :
g.fillOval(
(dieSize.width / 4) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
(dieSize.height / 4) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
(dieSize.width / 4) - spotWidth,
(dieSize.height / 2) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
(dieSize.height / 2) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
(dieSize.width / 4) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
g.fillOval(
((dieSize.width / 4) * 3) - spotWidth,
((dieSize.height / 4) * 3) - spotHeight,
spotWidth * 2,
spotHeight * 2);
break;
}
}
public Dimension getPreferredSize() {
if (preferredSize == null) {
return new Dimension(100, 100);
} else {
return super.getPreferredSize();
}
}
public void setPreferredSize(Dimension newPrefSize) {
preferredSize = newPrefSize;
super.setPreferredSize(newPrefSize);
}
public static void main(String[] args) {
}
}
devoir_m.zip_games application_zip
版权申诉
128 浏览量
2022-09-22
20:57:52
上传
评论
收藏 30KB ZIP 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- projectData
- SQL Server 性能监视器,它旨在提供开箱即用的全面监控,并作为您自己的项目或应用程序的监控框架 它在本地数据库中收集性能
- Python 程序语言设计模式思路-并发模式:线程池模式:管理线程池,优化线程创建和销毁
- 股事汇投资工具-实时新闻、财经日历、市场快讯、持仓查询、外汇兑换、换算工具、大盘云图、江恩工具、指标检测等
- webrtc-streamer
- html+css+'青春献礼二十大 红色旅游助乡村'为主题的网页设计 2022年参与学校网页设计比赛时完成的
- Unity 模型布尔运算插件
- eCANtools工具,CAN资料包
- 129335283047061xiazaigongjuxiang(去重软件).apk
- Android环境检测工具,检测ksu,lsp,magisk等
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈