PROIECT SEMESTRIAL
LA DISCIPLINA
PROGRAMARE VIZUALA IN JAVA
Tema : Java AWT
student: Aprofirei Dumitru anul 3 grupa 5678
Tema1. S se realizez un exmplu utilizind [Link] pentru a
seta fundalul unui JPanel.
import [Link].*;
import [Link].*;
import [Link].*;
& & & & &
@SuppressWarnings("serial") &
public class AlegeCuloarea extends JFrame { &
JPanel panel; &
Color bgColor = Color.LIGHT_GRAY; // culoare fundal Panel &
// Constructor pentru UI componente si evenimente &
public AlegeCuloarea() { &
panel = new JPanel(new BorderLayout()); &
& & &*
JButton btnColor = new JButton("Schimba culoarea"); &
[Link](btnColor, [Link]);& &*
[Link](new ActionListener() {
@Override
& &*
public void actionPerformed(ActionEvent evt) {
Color color = [Link]([Link],
"Alege o culoare", bgColor);
if (color != null) { // culoare noua selectata
bgColor = color;
}
[Link](bgColor); // se schimba fundalul
}
});
setContentPane(panel);
2
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Sa schimbam culoarea");
setSize(400, 300);
setLocationRelativeTo(null); // centrarea ferestrei
setVisible(true);
// sa o arate
}
& &*3
// Metoda main() o definim abia aici
public static void main(String[] args) {
// Se ruleaza codurile GUI pentru rularea impecabila
[Link](new Runnable() {
@Override
public void run() {
new AlegeCuloarea(& ); / Acum constructorul isi face treaba
}
});
}& &*}
Tema2. Mutarea unui Obiect prin Key/Button Action. Descriere n engleza
import [Link].*;
import [Link].*;
import [Link].*;
@SuppressWarnings("serial")
public class CGMoveALine extends JFrame {
/ / defining size
public static final int CANVAS_WIDTH = 500;
public static final int CANVAS_HEIGHT = 140;
3
public static final Color LINE_COLOR = [Link];
public static final Color CANVAS_BACKGROUND = [Link];
// The moving line from (x1, y1) to (x2, y2), initially position at the center
private int x1 = CANVAS_WIDTH / 2;
private int y1 = CANVAS_HEIGHT / 8;
private int x2 = x1;
private int y2 = CANVAS_HEIGHT / 8 * 7;
private DrawCanvas canvas; // The custom drawing canvas (an innder class extends
JPanel)
// Constructor to set up the GUI components and event handlers
public CGMoveALine() {
// Set up a panel for the buttons
JPanel btnPanel = new JPanel(new FlowLayout());
JButton btnLeft = new JButton("Move Left ");
[Link](btnLeft);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x1 -= 10;
x2 -= 10;
[Link]();
requestFocus(); // change the focus to JFrame to receive KeyEvent
}
});
JButton btnRight = new JButton("Move Right");
[Link](btnRight);
[Link](new ActionListener() {
4
public void actionPerformed(ActionEvent evt) {
x1 += 10;
x2 += 10;
[Link]();
requestFocus(); // change the focus to JFrame to receive KeyEvent
}
});
// Set up a custom drawing JPanel
canvas = new DrawCanvas();
[Link](new Dimension(CANVAS_WIDTH,
CANVAS_HEIGHT));
// Add both panels to this JFrame's content-pane
Container cp = getContentPane();
[Link](new BorderLayout());
[Link](canvas, [Link]);
[Link](btnPanel, [Link]);
// "super" JFrame fires KeyEvent
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
switch([Link]()) {
case KeyEvent.VK_LEFT:
x1 -= 10;
x2 -= 10;
repaint();
break;
5
case KeyEvent.VK_RIGHT:
x1 += 10;
x2 += 10;
repaint();
break;
}
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Handle the CLOSE
button
setTitle("Move a Line");
pack();
// pack all the components in the JFrame
setVisible(true); // show it
requestFocus(); // set the focus to JFrame to receive KeyEvent
}
class DrawCanvas extends JPanel {
@Override
public void paintComponent(Graphics g) {
[Link](g);
setbackground(CANVAS_BACKGROUND);
[Link](LINE_COLOR);
[Link](x1, y1, x2, y2); // Draw line
}
}
// The entry main() method
6
public static void main(String[] arg s) {
// Run GUI codes on the Event-Dispatcher Thread for thread safety
[Link](new Runnable() {
@Override
public void run() {
new CGMoveALine(); // Let the constructor do the job
}
});
}
}