Tatar gr. 22_1
Ćwiczenie A:
1)Kod programu:
import java.awt.*;
import java.util.*;
public class Classlab3 extends javax.swing.JApplet{
private String biezaca_data="";
public void init(){
setBackground(Color.yellow);
}
public void paint(Graphics g){
Graphics2D g2=(Graphics2D) g;
GregorianCalendar day=new GregorianCalendar();
String time=day.getTime().toString();
g2.drawString(biezaca_data, 5,25);
g2.drawString(time,5,25);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
repaint();
}
biezaca_data=time;
}
}
Opis działania i użytych funkcji:
Tworzymy nowy obiekt klasy GregorianCalendar i jego pole Time przekazujemy obiektowi o nazwie time. Czas zostaje wyświetlony na ekranie jako wartość obiektu biezaca_data o tej samej zawartości co time.
Ćwiczenie B: (adres strony: http://student.agh.edu.pl/~tatara/)
1) Kod programu:
<html> | |
---|---|
<head> | |
<title></title> | |
</head> | |
<body> | |
<p> | |
</p> | |
<h1 style="text-align: center;"> | |
<span style="color: rgb(128, 0, 128);"><span style="font-size: 72px;"><span style="font-family: 'comic sans ms', cursive;">Michał Tatara</span></span></span></h1> | |
<h2 style="text-align: center;"> | |
<span style="color: rgb(0, 255, 0);"><span style="font-family: 'comic sans ms', cursive;"><span style="font-size: 36px;">Rok II</span></span></span></h2> | |
<h3 style="text-align: center;"> | |
<span style="color: rgb(255, 215, 0);"> Automatyka i Robotyka</span></h3> | |
<p style="text-align: center;"> | |
<span style="color: rgb(255, 0, 0);">grupa 22</span></p> | |
<div align="center"><object | |
id="ap" | |
type="application/x-java-applet" | |
code="Classlab3.class" | |
width="200" | |
height="60"> | |
</object></div> | |
</body> | |
</html> |
Opis działania i użytych funkcji:
Jest to kod html, który pozwala na użycie apletu z ćwiczenia A.
Ćwiczenie C:
1) Kod programu:
package PackMT51; import java.awt.*; import java.applet.*; import java.awt.event.*; public class Zdarzenia_myszy extends Applet implements MouseListener, MouseMotionListener, KeyListener{ char l; int x; int y; int wyborfigury; Color kolor=new Color(111,55,28); Font a = new Font ("SERIF", Font.BOLD, 20); Font b = new Font ("Monospaced", Font.BOLD, 30); Font c = new Font ("Monospaced", Font.ITALIC, 20); public void init(){ addMouseListener(this); addMouseMotionListener(this); addKeyListener(this); setSize(1300,700); } public void paint (Graphics g){ Graphics2D g2= (Graphics2D) g; g2.setStroke(new BasicStroke(4)); g.setColor(Color.green); g.fillRect(0, 0, 1300, 100); g.setColor(Color.black); g.drawLine(0,100,1300,100); g.drawLine(0,0,0,700); g.drawLine(0,0,1300,0); g.drawLine(100,100,100,700); g.drawLine(1300,0,1300,700); g.drawLine(0, 200, 100, 200); g.drawLine(0, 300, 100, 300); g.drawLine(0, 400, 100, 400); g.drawLine(0, 500, 100, 500); g.drawLine(0, 600, 100, 600); g2.setStroke(new BasicStroke(2)); g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); x=50; y=350; rysujsniezynke(); x=50; y=450; rysujdzwonek(); x=50; y=650; rysujswiateczne(); g.setFont(a); g.setColor(Color.red); g.drawString("USUN", 20, 555); g.setFont(b); g.setColor(Color.yellow); g.drawString("PROSTY PROGRAM GRAFICZNY", 200, 60); } public void rysujsniezynke(){ Graphics g=getGraphics(); Graphics2D g2= (Graphics2D) g; g2.setStroke(new BasicStroke(3)); g.setColor(kolor); g.drawLine(x-10, y-10, x+10, y+10); g.drawLine(x-10, y+10, x+10, y-10); g.drawLine(x, y+14, x, y-14); g.drawLine(x-14, y, x+14, y); } public void rysujdzwonek(){ Graphics g=getGraphics(); Graphics2D g2= (Graphics2D) g; g2.setStroke(new BasicStroke(3)); g.setColor(kolor); g.drawLine(x-14, y+14, x-7, y+4); g.drawLine(x-7, y+4, x-7, y-10); g.drawLine(x-7, y-10, x-2, y-14); g.drawLine(x-2, y-14, x+2, y-14); g.drawLine(x+2, y-14, x+7, y-10); g.drawLine(x+7, y-10, x+7, y+4); g.drawLine(x+7, y+4, x+14, y+14); g.drawLine(x-14, y+14, x+14, y+14); g.drawOval(x, y+14, 7, 7); g.drawOval(x-2, y-18, 4, 4); } public void rysujcos(){ Graphics g=getGraphics(); Graphics2D g2= (Graphics2D) g; g2.setStroke(new BasicStroke(2)); g.setColor(kolor); g.drawLine(x-13, y, x+13, y); g.drawLine(x, y-13, x, y+13); g.drawOval(x-10, y-10, 20, 20); } public void rysujswiateczne(){ Graphics g=getGraphics(); Graphics2D g2= (Graphics2D) g; g2.setStroke(new BasicStroke(2)); g.setColor(Color.green); g.drawLine(x, y-20, x+6, y-10); g.drawLine(x, y-20, x-6, y-10); g.drawLine(x-2, y-10, x-6, y-10); g.drawLine(x+2, y-10, x+6, y-10); g.drawLine(x+2, y-10, x+12, y+5); g.drawLine(x-2, y-10, x-12, y+5); g.drawLine(x-5, y+5, x-12, y+5); g.drawLine(x+5, y+5, x+12, y+5); g.drawLine(x+5, y+5, x+20, y+27); g.drawLine(x-5, y+5, x-20, y+27); g.drawLine(x-20, y+27, x+20, y+27); g.setColor(Color.black); g.drawRect(x-7,y+27,14,8); g.fillRect(x-7,y+27,14,8); } @Override public void mouseClicked(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub Graphics g=getGraphics(); g.setFont(c); g.setColor(Color.yellow); g.drawString("kursor w obrebie okna programu", 200, 80); } @Override public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub repaint(); // przywraca stan grafiki okreslony w met. paint } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub Graphics g=getGraphics(); x=e.getX(); y=e.getY(); if(x<100){ if(y>100){ if(y<200){ wyborfigury=1; } else{ if(y<300){ wyborfigury=2; } else{ if(y<400){ wyborfigury=3; } else{ if(y<500){ wyborfigury=4; } else{ if(y<600){ wyborfigury=5; } else{ if(y<700){ wyborfigury=6; } } } } } } } } else{ if(wyborfigury==1){ g.setColor(kolor); g.drawLine(x-10,y+10,x+10,y-10); } else{ if(wyborfigury==2){ g.setColor(kolor); g.drawOval(x,y,40,40); } else{ if(wyborfigury==3){ rysujsniezynke(); } else{ if(wyborfigury==4){ rysujdzwonek(); } else{ if(wyborfigury==5){ g.drawRect(x,y,30,30); } else{ if(wyborfigury==6){ rysujswiateczne(); } } } } } } } } @Override public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseDragged(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub Graphics g=getGraphics(); x=e.getX(); y=e.getY(); if(x<100){ if(y>100){ if(y<200){ g.clearRect(5,5,42,42); g.setColor(kolor); g.drawLine(15,35,35,15); } else{ if(y<300){ g.clearRect(5, 5, 42, 42); g.setColor(kolor); g.drawOval(15, 15, 20, 20); } else{ if(y<400){ g.clearRect(5,5,42,42); x=25; y=25; rysujsniezynke(); } else{ if(y<500){ g.clearRect(5,5,42,42); x=25; y=25; rysujdzwonek(); } else{ if(y<600){ g.clearRect(5,5,42,42); x=25; y=25; rysujswiateczne(); } } } } } } } } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub Graphics g=getGraphics(); g.setFont(b); l=e.getKeyChar(); int s=1000; int w=100; int xx=160; int yy=160; switch(l){ case 'w': g.clearRect(xx, yy, s, w); kolor=Color.yellow; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("w", 200, 200); break; case 'q': g.clearRect(xx, yy, s, w); kolor=Color.blue; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("q", 200, 200); break; case 'e': g.clearRect(xx, yy, s, w); kolor=Color.red; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("e", 200, 200); break; case 'r': g.clearRect(xx, yy, s, w); kolor=Color.black; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("r", 200, 200); break; case 't': g.clearRect(xx, yy, s, w); kolor=Color.cyan; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("t", 200, 200); break; case 'y': g.clearRect(xx, yy, s, w); kolor=Color.orange; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("y", 200, 200); break; default: g.clearRect(xx, yy, s, w); kolor=Color.green; g.setColor(kolor); g.drawLine(20, 180, 80, 120); g.drawOval(20, 220, 60, 60); g.drawString("Wcisnales niezdefiniowany klawisz", 200, 200); break; } } @Override public void keyReleased(KeyEvent arg0) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent arg0) { // TODO Auto-generated method stub } } |
---|
Opis działania i użytych funkcji:
Program prezentuje sposób użycia interfejsu KeyListener. Program nasłuchuje jaki klawisz został wciśnięty z klawiatury i dla danego klawisza wykonuje zadaną operację.