I have a class MenuScreen on which I paint a lot of images to. (4 at the moment [that may not be a lot to you but it is to me])
only one renders no matter what, sometimes they some half-render, and other times they completely render,
and sometimes they don't render at all (The main one still renders)
Here is my code
public List<BufferedImage> im;
public MenuScreen() {
setTitle("ALevelUp 0.0.1 Alpha");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initImages();
JPanel p = new JPanel(new GridLayout(1, 1));
p.add(new JLabel(new ImageIcon(im.get(0))));
add(p);
setSize(766, 500);
setLocationRelativeTo(null);
setVisible(true);
System.err.println(getHeight() + "," + getWidth());
Graphics2D g = (Graphics2D) im.get(0).getGraphics();
draw(g);
}
public final void initImages() {
im = a.init();
}
public final void draw(Graphics2D g) {
BufferedImage s1 = im.get(1);
Graphics2D s1g = (Graphics2D) s1.getGraphics();
s1g.setFont(scale(a.getFont(), s1g, "Slot 1", s1));
s1g.setColor(Color.black);
s1g.drawString("Slot 1", s1.getWidth() / 2 - 23, s1.getHeight() / 2 + 7);
g.drawImage(s1, (getWidth() / 2) - (s1.getWidth() / 2) - 21,
47, rootPane);
s1g.setColor(new Color(253, 198, 147));
s1g.fillRect(106, 20, 100, 20);
s1g.setColor(Color.black);
s1g.drawString("Slot 2", s1.getWidth() / 2 - 23, s1.getHeight() / 2 + 7);
g.drawImage(s1, getWidth() / 2 - s1.getWidth() / 2 - 21, 179, rootPane);
s1g.setColor(new Color(253, 198, 147));
s1g.fillRect(106, 20, 100, 20);
s1g.setColor(Color.black);
s1g.drawString("Slot 3", s1.getWidth() / 2 - 23, s1.getHeight() / 2 + 7);
g.drawImage(s1, getWidth() / 2 - s1.getWidth() / 2 - 21, 311, rootPane);
}
public Font scale(Font f, Graphics g, String text, BufferedImage img) {
float ntry = 20.0f;
Font font = null;
while (2 < 3) {
font = f.deriveFont(ntry);
FontMetrics fm = g.getFontMetrics(font);
int width = fm.stringWidth(text);
if (width < img.getWidth()) {
return font;
}
}
}
Can anyone help me to understand why this happens and what I can do to fix it?
Edit: Here Are the image resources, if you need them:
The Main Screen
The Slot Panels
How it looks when it works
I had to modify most of the code to get something to work. I'm assuming that this is what you want.
Here are the changes I made.
I added a main method that called SwingUtilities invokeLater to put the Swing components on the Event Dispatch thread.
I split the code into 3 classes, DrawImage, DrawingPanel, and Snippet. DrawImage creates the four images. DrawingPanel draws the four images onto a JPanel. Snippet creates the JFrame and adds the drawing panel to the JFrame.
I defined the size of the drawing panel to hold 4 slots. I packed the JFrame so that
the JFrame would be the correct size to hold the drawing panel.
I overrode the paintComponent method to draw the four images from the image list. These images were already created in the DrawImage class. I called super.paintComponent to make sure all of the Swing children components were drawn correctly.
I created the images before I created the Swing GUI.
I used a method I created, centerString, to center the text in the images. I left the scale method alone.
Here's the modified code. Unlike yours, it's runnable.
package snippet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Snippet implements Runnable {
private JFrame frame;
private List<BufferedImage> imageList;
public Snippet() {
imageList = new ArrayList<BufferedImage>();
new DrawImage().createImages();
}
#Override
public void run() {
frame = new JFrame();
frame.setTitle("ALevelUp 0.0.1 Alpha");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DrawingPanel p = new DrawingPanel();
frame.add(p);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println(frame.getHeight() + "," + frame.getWidth());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Snippet());
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID =
2535522354552193273L;
public DrawingPanel() {
this.setPreferredSize(new Dimension(550, 350));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 50;
int y = 50;
for (int i = 0; i < 2; i++) {
BufferedImage image = null;
for (int j = 0; j < 2; j++) {
image = imageList.get(i * 2 + j);
g.drawImage(image, x, y, this);
x += image.getWidth() + 50;
}
x = 50;
y += image.getHeight() + 50;
}
}
}
public class DrawImage {
public void createImages() {
imageList.add(createImage("Slot 1"));
imageList.add(createImage("Slot 2"));
imageList.add(createImage("Slot 3"));
imageList.add(createImage("Slot 4"));
}
private BufferedImage createImage(String text) {
Rectangle r = new Rectangle(0, 0, 200, 100);
BufferedImage image = new BufferedImage(r.width, r.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) image.getGraphics();
Font font = g.getFont();
g.setFont(scale(font, g, text, image));
g.setColor(Color.BLACK);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.YELLOW);
centerString(g, r, text, font);
g.dispose();
return image;
}
private Font scale(Font f, Graphics g, String text,
BufferedImage img) {
float ntry = 20.0f;
Font font = null;
while (2 < 3) {
font = f.deriveFont(ntry);
FontMetrics fm = g.getFontMetrics(font);
int width = fm.stringWidth(text);
if (width < img.getWidth()) {
return font;
}
}
}
/**
* This method centers a <code>String</code> in
* a bounding <code>Rectangle</code>.
* #param g - The <code>Graphics</code> instance.
* #param r - The bounding <code>Rectangle</code>.
* #param s - The <code>String</code> to center in the
* bounding rectangle.
* #param font - The display font of the <code>String</code>
*
* #see java.awt.Graphics
* #see java.awt.Rectangle
* #see java.lang.String
*/
private void centerString(Graphics g, Rectangle r, String s,
Font font) {
FontRenderContext frc =
new FontRenderContext(null, true, true);
Rectangle2D r2D = font.getStringBounds(s, frc);
int rWidth = (int) Math.round(r2D.getWidth());
int rHeight = (int) Math.round(r2D.getHeight());
int rX = (int) Math.round(r2D.getX());
int rY = (int) Math.round(r2D.getY());
int a = (r.width / 2) - (rWidth / 2) - rX;
int b = (r.height / 2) - (rHeight / 2) - rY;
g.setFont(font);
g.drawString(s, r.x + a, r.y + b);
}
}
}
Related
I've gotten my code to work for the most part except for one last thing. When the number of lines drawn > 20, the lines aren't being drawn to take up the full amount of panel
** Look at the bottom right side of my screenshot. The lines go part way down the full height of the panel. I need it to go the full length of the height down. **
Here's my code for DrawPanelTest
// Creating JPanel to display DrawPanel
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class DrawPanelTest
{
public static void main(String[] args)
{
int nValue = 0; // declare variable to store user input. Default value 0.
Boolean flag = true; // initalize flag to true for argument value of while-loop
// while-loop to prompt user input
while (flag) {
// prompt user for the value of N
nValue = Integer.parseInt(JOptionPane.showInputDialog("Enter number of lines between 2 and 100."));
// user input validation. Valid input is nValue [2, 100]
if (nValue < 2 || nValue > 100) {
nValue = Integer.parseInt(JOptionPane.showInputDialog("Enter number of lines between 2 and 100."));
} else {
flag = false; // if user input is correct, while-loop will end
} // end if-else
} // end while-loop
// displays the value of N to make sure it really is correct; This works
// String message = String.format("The value of n is: %d ", nValue);
// JOptionPane.showMessageDialog(null, message);
// create a panel that contains our drawing
DrawPanel drawPanel = new DrawPanel(nValue);
// create a new frame to hold the panel
JFrame application = new JFrame();
// set the frame to exit when closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(drawPanel); // add panel to the frame
application.setSize(600, 600); // set the size of the frame
application.setVisible(true); // make the frame visible
}
} // end class DrawPanelTest
Here's my code DrawPanel
// Using drawLine() to connect the corners of a panel
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
int numLines;
// constructor initializes DrawPanel and initializes
// numLines with the argument value of n
public DrawPanel(int n)
{
numLines = n;
}
// draws a X from the corners of the panel
public void paintComponent( Graphics g)
{
// call paintComponent to ensure the panel displays correctly
super.paintComponent(g);
int width = getWidth(); // total width
int height = getHeight(); // total height
int x1 = 0; // starting x-coordinate
int y1 = height / 2; // starting y-coordinate
int x2 = width; // initial value for end x-coordinate
int spaceValue = 0; // represents the space between the lines
int stepValue = height/numLines; // represents the increment value starting from top right corner
for (int i = 0; i <= numLines; i++) {
if (numLines == 2) {
g.drawLine(x1, y1, x2, 0);
g.drawLine(x1, y1, x2, height);
break;
} // end numLines == 2
else if (numLines >= 3) {
g.drawLine(x1, y1, x2, spaceValue);
spaceValue += stepValue;
} // end else if
} // end for-loop
} // end paintComponent
} // end class DrawPanel
I think my problem lies in DrawPanel line 41. I don't think I'm calculating the spaces between the lines correctly so that they end up taking the entire height of the panel.
Thanks in advance for your help.
Your calculation of the "stepValue" has two problems:
the stepValue is to small, so the last line will never appear at the bottom
the stepValue is truncated because of integer math so each addition of the truncated value to the "spaceValue" will yield a slightly less accurate value.
Lets say you have a panel with a height of 600 and the number of lines is 3.
Your calculation is:
int stepValue = 600 / 3 = 200
which I would suggest is wrong as you would draw 3 lines with a "spaceValue" of 0, 200, 400, so the last line (at 600) would never be drawn.
In reality I think the calculation should be:
double stepValue = (double)height / (numLine - 1);
which gives:
double stepValue = 600 / (3 - 1) = 300.
which will give lines with a "spaceValue" of 0, 300, 600 which would be a line at the top, middle and bottom.
So your painting loop simply becomes:
for (int i = 0; i < numLines; i++)
{
g.drawLine(x1, y1, x2, spaceValue);
spaceValue += stepValue;
}
I think (one of) the issue you're having is a "integer division" issue.
int stepValue = height / numLines;
is truncating the result. For example, if the height is 400 and the number of lines is 6 the stepValue will be 66 instead of 67 (which would allow 66.6666 to be rounded up)
Now, you could "round" the value up yourself, but I'd prefer to make use of the available APIs to do these things for me.
Line2D.Double supports double precision parameters, which makes it perfect for this job
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (lineCount > 1) {
double gap = getHeight() / (double)(lineCount - 1);
for (int i = 0; i < lineCount; i++) {
Line2D line = new Line2D.Double(0, getHeight() / 2, getWidth(), i * gap);
g2d.draw(line);
}
} else {
g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
}
g2d.dispose();
}
Runnable example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(new EmptyBorder(32, 32, 32, 32));
frame.setContentPane(contentPane);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private int lineCount = 1;
public TestPane() {
setBorder(new LineBorder(Color.RED));
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
lineCount++;
repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 400);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
FontMetrics fm = g2d.getFontMetrics();
g2d.drawString(Integer.toString(lineCount), 10, fm.getAscent());
if (lineCount > 1) {
double gap = getHeight() / (double) (lineCount - 1);
for (int i = 0; i < lineCount; i++) {
Line2D line = new Line2D.Double(0, getHeight() / 2, getWidth(), i * gap);
g2d.draw(line);
}
} else {
g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
}
g2d.dispose();
}
}
}
In the for loop, change
for (int i = 0; i < numLines; i++) {
to
for (int i = 0; i <= numLines; i++) {
to iterate the full height of the component. The i must equal numLines to reach unity.
I wrote a little Java program for a friend of mine, we have the same resolution ( 1920 * 1080 ), but his Screen in general is smaller than mine. To solve the problem I gave the GUI elements percentual coordinates, which works perfectly until I use fonts. The Font will be the same size, like mine which is like the code expects. The Bounds for a JButton for example says where to start where to end, but a font measured in points, so each inch has 72 points, a Font with 30 will always be half an inch. (Compare to the Images)
How can I fix this, I found nothing that could solve the problem in my eyes?
Image Before:
https://ibb.co/HXf0pt4
Image After:
https://ibb.co/1sbKyts
public class DrawMenu extends JLabel{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int sw = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int sh = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
g.setFont(new Font("TimesRoman", Font.PLAIN, (int)Math.round(sw * 0.0078)));
g.drawString("Termine die nächsten 0 bis 7 Tage", (int)Math.round(sw * 0.026), (int)Math.round(sh * 0.176));
g.drawString("Termine die nächsten 8 bis 14 Tage", (int)Math.round(sw * 0.182), (int)Math.round(sh * 0.176));
repaint();
}
}
public class Gui {
static JFrame jfCounter, jfMenu, jfTask;
static DrawCounter drawc;
static DrawMenu drawm;
static DrawTask drawt;
static JButton kEnde, kNC, kA, kL, kEsc1, kEsc2, kEsc3, kEsc4, kEsc5, kLG, kLB, kAS, kEnter, kJFsw1, kJFsw2, kJFsw3,
kJFsw4, ko[] = new JButton[6], kSettings, ktw, ktla, ktle, ktlb, ktnt;
static JTextArea taAusgabe, taAusgabedw, taAusgabenw, taze, tabe, tati;
static JFormattedTextField nfTag, nfMonat, nfJahr, nfTagA, nfMonatA, nfJahrA;
static JTextField tfAnlass, tfkLBestimmt, tfAnlassA, tfAnlassAS, tfKonsole, tfKonsoleT, tfsettings[] = new JTextField[12], tftw,
tftis, tftlbs, tftnct, tftnci;
public Gui() {
int sw = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
int sh = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();
System.out.println(sw + " " + sh);
// JFrame Settings
jfCounter = new JFrame();
jfCounter.setSize(sw, sh);
jfCounter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfCounter.setLocationRelativeTo(null);
jfCounter.setExtendedState(JFrame.MAXIMIZED_BOTH);
jfCounter.setTitle("Tageszähler");
jfMenu = new JFrame();
jfMenu.setSize(sw, sh);
jfMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfMenu.setLocationRelativeTo(null);
jfMenu.setExtendedState(JFrame.MAXIMIZED_BOTH);
jfMenu.setTitle("Menü");
jfTask = new JFrame();
jfTask.setSize(sw, sh);
jfTask.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfTask.setLocationRelativeTo(null);
jfTask.setExtendedState(JFrame.MAXIMIZED_BOTH);
jfTask.setTitle("Task");
// JButtons
Border bor = BorderFactory.createLineBorder(Color.BLACK, 1);
Border boresc = BorderFactory.createLineBorder(Color.RED, 3);
kEnde = new JButton("Ende");
kEnde.addActionListener(new ActionHandler());
kEnde.setBounds((int) Math.round(sw * 0.026), (int) Math.round(sh * 0.509), (int) Math.round(sw * 0.195),
(int) Math.round(sh * 0.028));
kEnde.setBorder(bor);
jfCounter.add(kEnde);
kNC = new JButton("Neuer Counter");
kNC.addActionListener(new ActionHandler());
kNC.setBounds((int) Math.round(sw * 0.026), (int) Math.round(sh * 0.028), (int) Math.round(sw * 0.078),
(int) Math.round(sh * 0.028));
kNC.setBorder(bor);
jfCounter.add(kNC);
// JLabel / Draw
drawc = new DrawCounter();
drawc.setBounds(0, 0, sw, sh);
drawc.setVisible(true);
jfCounter.add(drawc);
drawm = new DrawMenu();
drawm.setBounds(0, 0, sw, sh);
drawm.setVisible(true);
jfMenu.add(drawm);
drawt = new DrawTask();
drawt.setBounds(0, 0, sw, sh);
drawt.setVisible(true);
jfTask.add(drawt);
// JFrame Final Visible
Gui.getJfMenu().setVisible(true);
}
I'm working on a project and the general idea of my work is to do every part of it in a stand alone project, then add it when finished to the main project through libraries.
My problem is - one part I have done was to perform a painting on a panel.
When I add a layer pane which connects it to the main project , there is no drawing actually happening.
Here is my project sample code:
In code sample 1 there is JLayeredPane which contains my panel to perform drawing.
In code sample 2 there is a button. Its actionPerformed is to add that JLayeredPane to a panel. But the problem is that the drawing is not appearing after adding the JLayeredPane.
Code sample 1:
public class GraphGui extends javax.swing.JFrame {
/**
* Creates new form GraphGui
*/
adjacencyMatrix m = new adjacencyMatrix();
Dfs df = new Dfs();
int[] x = new int[df.MAX_VERTS];
int[] y = new int[df.MAX_VERTS];
public Graphics2D d;
public Graphics2D doo;
int i = 0;
public GraphGui() {
setlookAndFeel();
initComponents();
setLocationRelativeTo(null);
DFS.setVisible(true);
adjMatrics.setVisible(false);
//display is the panel that draw over
d = (Graphics2D) display.getGraphics();
doo = (Graphics2D) jPanel2.getGraphics();
}
//crating an initialization of components are done automatically
public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
df.dfs();
doo.setFont(new Font("TimesRoman", Font.BOLD, 19));
doo.drawString("visits: " + df.out, 5, 20);
df.out = "";
}
public void AddE1ActionPerformed(java.awt.event.ActionEvent evt) {
String j = start1.getText();
int k = Integer.parseInt(j) - 1;
String c = end1.getText();
int v = Integer.parseInt(c) - 1;
df.addEdge(k, v);
d.setFont(new Font("TimesRoman", Font.BOLD, 17));
d.drawLine(x[k] + 30, y[k] + 20, x[v], y[v] + 19);
}
public void AddV1ActionPerformed(java.awt.event.ActionEvent evt) {
String f = ver1.getText();
String toUpperCase = f.toUpperCase();
char r = toUpperCase.charAt(0);
df.addVertex(r);
int radius = 30;
int R = (int) (Math.random() * 256);
int G = (int) (Math.random() * 256);
int B = (int) (Math.random() * 256);
x[i] = R % 320;
y[i] = B % 167;
d.setColor(new Color(R, G, B));
d.setFont(new Font("TimesRoman", Font.BOLD, 15));
d.drawOval(x[i], y[i], radius, radius);
d.fillOval(x[i], y[i], radius, radius);
d.setColor(Color.BLACK);
d.drawString(r + "", x[i] + 10, y[i] + 20);
d.drawOval(0, 0, radius, radius);
i++;
}
What code sample 1 is supposed to do is shown at this link:
https://pbs.twimg.com/media/CG8INXZXAAEqthh.png:large
Code Sample 2
{
private void graphBTActionPerformed(java.awt.event.ActionEvent evt) {
GraphGui gr=new GraphGui();
jPanel2.removeAll();
jPanel2.add(gr.DFS);
MainLayer.setVisible(false);
Displaylayer.setVisible(true);
}
}
And at the link below is what I got after adding the panel-
nothing draws.
https://pbs.twimg.com/media/CG8IR7rWoAA3qKG.png:large
There are 2 separate issues here.
(1) Simplest answer is - you need to call 'getGraphics' from within your action method. Not from the constructor. E.g.
public void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Graphics2D doo = (Graphics2D) jPanel2.getGraphics();
...
doo.setFont(...);
doo.drawString(...);
}
(2) This would yield visible drawings, but they'll disappear whenever java decides to repaint - e.g. if you minimize the frame. This can be solved by paintComponent() as mentioned in the remarks. The basic idea is that your component (eg jPanel2) would hold a data structure of eveything it needs to paint - Strings, edges, vertexes etc. In paintComponent you draw them all. In actionPerformed() you change the datastructure and invoke 'repaint'. A sketch of this approach:
class MyPanel extends JPanel{
private String text;
private Point[] vertextes;
public void addVertext(..)
public void paintComponent(Graphics g){
... use g to drawString, drawOval... according to 'text' and 'vertexes'
}
}
// Then in your JFrame:
private MyPanel p;
...
actionPerfomred(...){
p.addVertext(..)
p.repaint();
}
Trying to figure out the best way to do this (And without crossing any specifics DO NOTs that I don't know about).
I'm working on visually displaying a graph (Various nodes, with edges connecting them) with circles and lines to represent such. Each node will be added during runtime and I can't hardcode this. From what I understand, all painting needs to be done in the paint(Graphics g) method - which isn't that helpful, since I can't be change the parameters and it seems this is only called during the initial creation?
Right now I was thinking about having it call various other methods, passing the Graphics object, and depending on other variables - I'll decide whether that's what I even want to call (Since the paint() method is the only one I can call).
Am I going about this completely wrong? Never bothered with this before.
To give you a better idea of what I want to end up with: I want to be able to pass the coordinates of the shape I want to add for the node, and then add it to whatever I have on the graph so far. And then same with the edges, I want to be able to pass the beginning and end point of the line to repaint on top of whatever is existing at that time.
Not exactly what I want right now - but you'll get the idea from what I patched together so far:
import java.awt.*;
import javax.swing.*;
public class MyCanvas extends Canvas
{
public MyCanvas()
{
}
public void paint(Graphics graphics)
{
// Keep this until I figured out if it's painted on load or not.
graphics.drawLine(10, 20, 350, 380);
}
public static void main(String[] args)
{
MyCanvas canvas = new MyCanvas();
JFrame frame = new JFrame();
int vertexes = 0;
// Change this next part later to be dynamic.
vertexes = 10;
int canvasSize = vertexes * vertexes;
frame.setSize(canvasSize, canvasSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.setVisible(true);
}
public void drawNode(int x, int y, Graphics g)
{
// Treat each location as a 10x10 block. If position 1,1 then go to (5,5) - If position 3,5 then go to (25, 45) eg: (x*10)-5, (y*10)-5
int xLoc = (x*10) - 5;
int yLoc = (y*10) - 5;
g.setColor(Color.white);
g.fillOval(xLoc, yLoc, 8, 8);
g.drawOval(xLoc, yLoc, 8, 8);
}
public void drawArc(int x, int y, int xx, int yy, Graphics g)
{
int xLoc = (x*10) - 5;
int yLoc = (y*10) - 5;
int xxLoc = (xx*10) - 5;
int yyLoc = (yy*10) - 5;
g.drawLine(xLoc, yLoc, xxLoc, yyLoc);
}
}
Edit: (Response for Andrew)
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class MyCanvas extends JPanel
{
public MyCanvas() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
public static void main(String[] args)
{
int vertexes = 0;
// Change this next part later to be dynamic.
vertexes = 10;
int canvasSize = vertexes * vertexes;
JFrame frame = new JFrame();
JLabel label = new JLabel();
BufferedImage bImage = new BufferedImage(canvasSize, canvasSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bImage.createGraphics();
g2d.drawLine(50, 50, 300, 300);
ImageIcon iIcon = new ImageIcon(bImage);
label.setIcon(iIcon);
frame.add(label);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g2d = drawNode(1,1,g2d);
label.repaint();
}
public static Graphics2D drawNode(int x, int y,Graphics2D g2d)
{
// Treat each location as a 10x10 block. If position 1,1 then go to (5,5) - If position 3,5 then go to (25, 45) eg: (x*10)-5, (y*10)-5
int xLoc = (x*10) - 5;
int yLoc = (y*10) - 5;
g2d.setColor(Color.white);
g2d.fillOval(xLoc, yLoc, 8, 8);
g2d.drawOval(xLoc, yLoc, 8, 8);
return g2d;
}
public static void drawArc(int x, int y, int xx, int yy)
{
int xLoc = (x*10) - 5;
int yLoc = (y*10) - 5;
int xxLoc = (xx*10) - 5;
int yyLoc = (yy*10) - 5;
// g.drawLine(xLoc, yLoc, xxLoc, yyLoc);
}
}
There are various strategies you might pursue for this.
If the objects are never removed from the drawing once done, use a BufferedImage, put it in a (ImageIcon in a) JLabel. When it comes time to update:
Get the graphics instance of the image and draw the new element.
Dispose of the graphics object.
Call repaint() on the label.
Keep a list of the drawn elements. In the paint method, paint them all. When a new element is added, call repaint() on the rendering component.
Here is an example of the 1st technique:
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class MyCanvas
{
JLabel view;
BufferedImage surface;
Random random = new Random();
public MyCanvas()
{
surface = new BufferedImage(600,400,BufferedImage.TYPE_INT_RGB);
view = new JLabel(new ImageIcon(surface));
Graphics g = surface.getGraphics();
g.setColor(Color.ORANGE);
g.fillRect(0,0,600,400);
g.setColor(Color.BLACK);
// Keep this until I figured out if it's painted on load or not.
g.drawLine(10, 20, 350, 380);
g.dispose();
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
addNewElement();
}
};
Timer timer = new Timer(200, listener);
timer.start();
}
public void addNewElement() {
boolean drawArc = random.nextBoolean();
int x = random.nextInt(60);
int y = random.nextInt(40);
Graphics g = surface.getGraphics();
if (drawArc) {
g.setColor(Color.BLUE);
int xx = random.nextInt(60);
int yy = random.nextInt(40);
drawArc(x,y,xx,yy,g);
} else {
drawNode(x,y,g);
}
g.dispose();
view.repaint();
}
public static void main(String[] args)
{
MyCanvas canvas = new MyCanvas();
JFrame frame = new JFrame();
int vertexes = 0;
// Change this next part later to be dynamic.
vertexes = 10;
int canvasSize = vertexes * vertexes;
frame.setSize(canvasSize, canvasSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(canvas.view);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void drawNode(int x, int y, Graphics g)
{
// Treat each location as a 10x10 block. If position 1,1 then go to (5,5) - If position 3,5 then go to (25, 45) eg: (x*10)-5, (y*10)-5
int xLoc = (x*10) - 5;
int yLoc = (y*10) - 5;
g.setColor(Color.white);
g.fillOval(xLoc, yLoc, 8, 8);
g.drawOval(xLoc, yLoc, 8, 8);
}
public void drawArc(int x, int y, int xx, int yy, Graphics g)
{
int xLoc = (x*10) - 5;
int yLoc = (y*10) - 5;
int xxLoc = (xx*10) - 5;
int yyLoc = (yy*10) - 5;
g.drawLine(xLoc, yLoc, xxLoc, yyLoc);
}
}
Further tip
You might notice that the lines look quite 'jagged' & ugly. Both the BufferedImage or a JComponent has access to the more useful Graphics2D object (for the JComponent it is necessary to cast it in paintComponent()). A Graphics2D instance accepts rendering hints that can be used to smooth (dither) the elements drawn.
I'm trying to make a custom swing control that is a meter.
Swing Meter http://dl.dropbox.com/u/2363305/Programming/Java/swing_meter.gif
The arrow will move up and down. Here is my current code, but I feel I've done it wrong.
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.Polygon;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class meter extends JFrame {
Stroke drawingStroke = new BasicStroke(2);
Rectangle2D rect = new Rectangle2D.Double(105, 50, 40, 200);
Double meterPercent = new Double(0.57);
public meter() {
setTitle("Meter");
setLayout(null);
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void paint(Graphics g) {
// Paint Meter
Graphics2D g1 = (Graphics2D) g;
g1.setStroke(drawingStroke);
g1.draw(rect);
// Set Meter Colors
Point2D start = new Point2D.Float(0, 0);
Point2D end = new Point2D.Float(0, this.getHeight());
float[] dist = { 0.1f, 0.5f, 0.9f };
Color[] colors = { Color.green, Color.yellow, Color.red };
LinearGradientPaint p = new LinearGradientPaint(start, end, dist,
colors);
g1.setPaint(p);
g1.fill(rect);
// Make a triangle - Arrow on Meter
int[] x = new int[3];
int[] y = new int[3];
int n; // count of points
// Set Points for Arrow
Integer meterArrowHypotenuse = (int) rect.getX();
Integer meterArrowTip = (int) rect.getY()
+ (int) (rect.getHeight() * (1 - meterPercent));
x[0] = meterArrowHypotenuse - 25;
x[1] = meterArrowHypotenuse - 25;
x[2] = meterArrowHypotenuse - 5;
y[0] = meterArrowTip - 20; // Top Left
y[1] = meterArrowTip + 20; // Bottom Left
y[2] = meterArrowTip; // Tip of Arrow
n = 3; // Number of points, 3 because its a triangle
// Draw Arrow Border
Polygon myTriShadow = new Polygon(x, y, n); // a triangle
g1.setPaint(Color.black);
g1.fill(myTriShadow);
// Set Points for Arrow Board
x[0] = x[0] + 1;
x[1] = x[1] + 1;
x[2] = x[2] - 2;
y[0] = y[0] + 3;
y[1] = y[1] - 3;
y[2] = y[2];
Robot robot = new Robot();
Color colorMeter = robot.getPixelColor(x[2]+10, y[2]);
// Draw Arrow
Polygon myTri = new Polygon(x, y, n); // a triangle
Color colr = new Color(colorMeter.getRed(), colorMeter.getGreen(), colorMeter.getBlue());
g1.setPaint(colr);
g1.fill(myTri);
}
public static void main(String[] args) {
new meter();
}
}
Thanks for looking.
In addition to #Jonas' example, you might like to look at the article How to Write a Custom Swing Component.
Addendum: On reflection, it looks a little intimidating, but you can extend BasicSliderUI and reuse some of your code in paintThumb() and paintTrack().
JSlider slider = new JSlider();
slider.setUI(new MySliderUI(slider));
...
private static class MySliderUI extends BasicSliderUI {
public MySliderUI(JSlider b) {
super(b);
}
#Override
public void paintTrack(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Rectangle r = trackRect;
g2d.setPaint(new GradientPaint(
r.x, r.y, Color.red, r.x + r.width, r.y + r.height, Color.blue));
g.fillRect(r.x, r.y, r.width, r.height);
}
#Override
public void paintThumb(Graphics g) {
super.paintThumb(g); // replace with your fill()
}
}
You could use a JSlider and use setValue(int n) to set the value whenever you need. You can also change the default appearance, so you get an arrow and a gradient as you want.