Ritprogram1.java

snottybugbearSoftware and s/w Development

Nov 3, 2013 (3 years and 10 months ago)

81 views

Ritprogram1.java
package prog1;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Ritprogram1 extends JFrame implements WindowListener,
MouseListener, MouseMotionListener {
private int last_x, last_y;

public Ritprogram1(String s) {
super(s);
this.init();
}

public void init() {
this.addWindowListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setSize(300, 300);
}

public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
}

public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}

public void mouseDragged(MouseEvent e) {
Graphics g = this.getGraphics();
int x = e.getX();
int y = e.getY();
g.drawLine(last_x, last_y, x, y);
last_x = x;
last_y = y;
}

public void windowOpened(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}

public void windowClosing(WindowEvent e) {
System.exit(0);
}

public static void main(String[] args) {
Ritprogram1 r = new Ritprogram1("Enkelt ritprogram");
r.setVisible(true);
}
}
Ritprogram2.java
package prog2;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class MinRityta extends JComponent {
protected int last_x, last_y;
public MinRityta() {
super();
this.init();
}

public void init() {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics();
int x = e.getX();
int y = e.getY();
g.drawLine(last_x, last_y, x, y);
last_x = x;
last_y = y;
}
});
}
}

public class Ritprogram2 {
public static void main(String[] args) {
JFrame r = new JFrame("Ritprogram 2");
r.getContentPane().add(new MinRityta());
r.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
r.setSize(300, 300);
r.setVisible(true);
}
}
Ritprogram3.java
package prog3;

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

class MinRityta extends JComponent {
Image image = null;
Graphics imG = null;
protected int last_x, last_y;

public MinRityta() {
super();
this.init();
}

protected Graphics getImGraphics() {
if (image == null) {
Dimension dim = getSize();
image = createImage(dim.width, dim.height);
imG = image.getGraphics();
}
return imG;
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, this);
}
}

public void init() {
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Graphics g = getImGraphics();
int x = e.getX();
int y = e.getY();
g.drawLine(last_x, last_y, x, y);
repaint();
last_x = x;
last_y = y;
}
});
}
}






public class Ritprogram3 {
public static void main(String[] args) {
JFrame r = new JFrame("Ritprogram 3");
r.getContentPane().add(new MinRityta());
r.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
r.setSize(300, 300);
r.setVisible(true);
}

}
SwingComponents.java
package prog4;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingComponents {
public static void main(String[] args) {
final JTextField textf1;
final JTextArea text1;
JButton b;
JFrame f = new JFrame("SwingComponents");
Container c = f.getContentPane();
textf1 = new JTextField();
c.add(textf1, BorderLayout.NORTH);
text1= new JTextArea();
c.add(text1, BorderLayout.CENTER);
b = new JButton("Flytta");
c.add(b, BorderLayout.SOUTH);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text1.append(textf1.getText());
}
});
f.setSize(300, 300);
f.setLocation(100, 100);
f.setVisible(true);
}
}