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 javax.swing.JColorChooser pentru a
seta fundalul unui JPanel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
& & & & &
@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"); &
panel.add(btnColor, BorderLayout.SOUTH);& &*
btnColor.addActionListener(new ActionListener() {
@Override
& &*
public void actionPerformed(ActionEvent evt) {
Color color = JColorChooser.showDialog(AlegeCuloarea.this,
"Alege o culoare", bgColor);
if (color != null) { // culoare noua selectata
bgColor = color;
}
panel.setBackground(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
SwingUtilities.invokeLater(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 java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@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 = Color.BLACK;
public static final Color CANVAS_BACKGROUND = Color.CYAN;
// 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 ");
btnPanel.add(btnLeft);
btnLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x1 -= 10;
x2 -= 10;
canvas.repaint();
requestFocus(); // change the focus to JFrame to receive KeyEvent
}
});
JButton btnRight = new JButton("Move Right");
btnPanel.add(btnRight);
btnRight.addActionListener(new ActionListener() {
4
public void actionPerformed(ActionEvent evt) {
x1 += 10;
x2 += 10;
canvas.repaint();
requestFocus(); // change the focus to JFrame to receive KeyEvent
}
});
// Set up a custom drawing JPanel
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH,
CANVAS_HEIGHT));
// Add both panels to this JFrame's content-pane
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);
// "super" JFrame fires KeyEvent
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
switch(evt.getKeyCode()) {
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) {
super.paintComponent(g);
setbackground(CANVAS_BACKGROUND);
g.setcolor(LINE_COLOR);
g.drawline(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
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new CGMoveALine(); // Let the constructor do the job
}
});
}
}