mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-09 10:40:34 -05:00
174 lines
5.5 KiB
HTML
174 lines
5.5 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="author" content="Brandon Rozek">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="robots" content="noindex" />
|
|
<title>Brandon Rozek</title>
|
|
<link rel="stylesheet" href="themes/bitsandpieces/styles/main.css" type="text/css" />
|
|
<link rel="stylesheet" href="themes/bitsandpieces/styles/highlightjs-github.css" type="text/css" />
|
|
</head>
|
|
<body>
|
|
|
|
<aside class="main-nav">
|
|
<nav>
|
|
<ul>
|
|
<li class="menuitem ">
|
|
<a href="index.html%3Findex.html" data-shortcut="">
|
|
Home
|
|
</a>
|
|
</li>
|
|
<li class="menuitem ">
|
|
<a href="index.html%3Fcourses.html" data-shortcut="">
|
|
Courses
|
|
</a>
|
|
</li>
|
|
<li class="menuitem ">
|
|
<a href="index.html%3Flabaide.html" data-shortcut="">
|
|
Lab Aide
|
|
</a>
|
|
</li>
|
|
<li class="menuitem ">
|
|
<a href="index.html%3Fpresentations.html" data-shortcut="">
|
|
Presentations
|
|
</a>
|
|
</li>
|
|
<li class="menuitem ">
|
|
<a href="index.html%3Fresearch.html" data-shortcut="">
|
|
Research
|
|
</a>
|
|
</li>
|
|
<li class="menuitem ">
|
|
<a href="index.html%3Ftranscript.html" data-shortcut="">
|
|
Transcript
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
</aside>
|
|
<main class="main-content">
|
|
<article class="article">
|
|
<h1>Lecture for November 20</h1>
|
|
<h2>Adding a drawing panel to the GUI</h2>
|
|
<p>You can't put swing and graphics together, therefore you need to make a seperate JPanels for swing and graphics.</p>
|
|
<p>Add necessary libraries</p>
|
|
<pre><code class="language-java">import java.awt.*;
|
|
import java.awt.Graphics;
|
|
import java.awt.event.*;
|
|
import javax.swing.*;</code></pre>
|
|
<pre><code class="language-java">public class drawingWindow extends JPanel {
|
|
JTextField field;
|
|
JButton draw;
|
|
DrawingPanel drawingPanel;
|
|
|
|
public drawingWindow() {
|
|
// Each new component would be vertically stacked upon each other
|
|
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
|
|
|
JPanel swingSTuff = new JPanel();
|
|
|
|
// Add things to the screen
|
|
draw = new JButton("Draw");
|
|
field = new JTextField();
|
|
swingStuff.add(field);
|
|
swingStuff.add(draw)
|
|
|
|
// Add the drawing panel onto the screen
|
|
drawingPanel = new DrawingPanel(200, 400);
|
|
add(drawingPanel);
|
|
|
|
// Activate the listener if the button was pressed
|
|
draw.addActionListener(new Listener());
|
|
}
|
|
|
|
// Add the listener to respond to events
|
|
private class listener implements ActionListener {
|
|
public void actionPerformed(ActionEvent event) {
|
|
if (event.getSource() == draw) {
|
|
drawingPanel.setFlag(1);
|
|
// Repaints the screen so the oval can appear
|
|
drawingPanel.repaint();
|
|
}
|
|
}
|
|
}
|
|
|
|
// Create the draw panel so we can add it to the screen
|
|
private class DrawingPanel extends JPanel {
|
|
private int width, height;
|
|
DrawingPanel(int width, int height) {
|
|
this.width = width;
|
|
this.height = height;
|
|
setPreferredSize(new Dimension(width, height));
|
|
}
|
|
public void setFlag(int flag) {
|
|
this.flag = flag;
|
|
}
|
|
public void paintComponent(Graphics g) {
|
|
super.paintComponent(g);
|
|
|
|
// Every time the flag is set, draw an oval at a random location and color
|
|
if (flag == 1) {
|
|
Random rand = new Random();
|
|
int x = rand.nextInt(width);
|
|
int y = rand.nextInt(height);
|
|
g.setColor(Color.RED);
|
|
g.fillOval(x, y, 20, 30);
|
|
}
|
|
}
|
|
}
|
|
}</code></pre>
|
|
<p>There are a myriad of different methods you can use. </p>
|
|
<pre><code class="language-java">// Assume width, height, y, x, etc are defined above
|
|
public void paintComponent(Graphics g) {
|
|
//....
|
|
g.dispose(); // Flushes the graphics buffer
|
|
}</code></pre>
|
|
<p>You have the traditional fill and draw methods. Fill creates the shape shaded in with a color. Draw creates an outline of the shape.</p>
|
|
<pre><code class="language-java">
|
|
// ...
|
|
g.fillRect(x ,y, width, height);
|
|
g.drawRect(x, y, width, height);
|
|
g.fillOval(x, y, width, height);
|
|
g.drawOval(x, y, width, height);
|
|
//g.drawPoly(parematers...);
|
|
//g.fillPoly(parameters...);
|
|
g.drawArc(x, y, width, height, startingAngle, sweepingAngle);
|
|
g.fillArc(x, y, width, height, startingAngle, sweepingAngle);</code></pre>
|
|
<p>You can also create complex shapes like a polygon. When adding points, you need to make sure you add them Clockwise or Counterclockwise (but NOT both)</p>
|
|
<pre><code class="language-java"> Polygon tri = new Polygon();
|
|
tri.addPoint(150, 10);
|
|
tri.addPoint(175, 100);
|
|
tri.addPoint(125, 100);
|
|
// Add points clockwise or counterclockwise (NOT BOTH)</code></pre>
|
|
</article>
|
|
</main>
|
|
|
|
<script src="themes/bitsandpieces/scripts/highlight.js"></script>
|
|
<script src="themes/bitsandpieces/scripts/mousetrap.min.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
tex2jax: {
|
|
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
|
|
processEscapes: true
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<script type="text/javascript"
|
|
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
|
|
</script>
|
|
<script>
|
|
hljs.initHighlightingOnLoad();
|
|
|
|
document.querySelectorAll('.menuitem a').forEach(function(el) {
|
|
if (el.getAttribute('data-shortcut').length > 0) {
|
|
Mousetrap.bind(el.getAttribute('data-shortcut'), function() {
|
|
location.assign(el.getAttribute('href'));
|
|
});
|
|
}
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|