Draw « Graphics « Java Swing Q&A





1. What component should I use to draw a dendrogram in Java?    stackoverflow.com

Dendrogram is not a regular diagram. Should I use any component from javax.swing.* library, or just draw it with java.awt.Graphics?

2. Splitting up the drawing of a GUI Element    stackoverflow.com

I have to use a GUI Element that draws a picture at a specific screen position. If the user selects this picture there is a border drawn around the Image. Now ...

3. Drawing on Java heavyweight swing components    stackoverflow.com

I'm working with an application in which I add a heavyweight (Canvas) to a JFrame. The Canvas is a 3rd party component, so I am required to keep it heavyweight. I'd ...

4. How to make C++ code draw on Java components    stackoverflow.com

We are designing physics app, and we would like to have it written in Java, and also we want to use root (root is writen in C++). Root has ...

5. Drawing graphics in java (netbeans ide)    stackoverflow.com

I created a new JApplet form in netbeans:

public class UI extends javax.swing.JApplet {
    //generated code...
}
And a jpanel in design mode named panou:
// Variables declaration - do not modify ...

6. What's the fastest way to draw a Hello World in Java    stackoverflow.com

What's the fastest way to draw a Hello World on the screen using a GUI in Java: 1- by using the minimum number of classes. 2- with the least byte code executed 3- the ...

7. Why cant i draw an ellipse with this code?    stackoverflow.com

 package test;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class test_bmp extends JPanel implements MouseListener,MouseMotionListener,ActionListener
{
 static BufferedImage image;
 Color color;
 Point start=new Point();
 Point end =new Point();
 JButton elipse=new JButton("Elipse");
 JButton rectangle=new ...

8. Drawing A Piano    stackoverflow.com

I have started working on a software synthesizer (or keyboard). I have decided to use Java because of the available Jfugue API. I am trying to figure out how ...

9. Draw nodes in e.g. a Chord ring    stackoverflow.com

I have a set of nodes that I would like to put into a ring. They all have a numeric property which I would like to use a reference when putting ...





10. What is the simplest way to draw in Java?    stackoverflow.com

What is the simplest way to draw in Java?

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

public class Canvas
{
    private JFrame frame;    
    private Graphics2D graphic;
   ...

11. Swing components drawing inside each other    stackoverflow.com

This is my fourth question here in about two weeks... I think I have a lot to learn. Anyway, I'm hoping the sample I've prepared below will explain the problem better than ...

12. Is there a Java library for writing a drawing program?    stackoverflow.com

I need to write a simple Java GUI application that, basically, allows the user to mark regions in a CT lung image. To make the marking process easier, it is important that ...

13. java: how to implement drawing freehand using a inner class    stackoverflow.com

Hi I am attempting a paint application and trying to figure how an inner class can get access to the main graphic2D function to implement a freehand draw choice? ...

14. How can I ensure correct drawing order in an OverlayLayout?    stackoverflow.com

I am using a JPanel with an OverlayLayout to draw two unrelated components on top of each other. Specifically, I have a transparent component which contains my own line drawings, and ...

15. Drawing curve bit by bit    stackoverflow.com

I need to draw a curve, knowing that I receive points every x milliseconds or x seconds, and that the curve is moving to the left by one pixel each time ...

16. Painting in Swing, blinking issue    stackoverflow.com

I have the following problem in swing.
I'm implementing basic drawing operations (lines, shapes). When I'm moving mouse with pressed left button, I need to repaint current shape. So I clear the ...





17. Repainting Artifacts in Swing    stackoverflow.com

I have a program with one panel on the right containing a BufferedImage that is very frequently updated and reapainted (using paintImediately()) and on the left are some buttons and JTextAreas. ...

18. Mac OS X Java doesn't draw initially    stackoverflow.com

Trying to work with basic swing on Java keeps giving me issues. When the JFrame is created by the runtime, none of the components are drawn initially, you have to resize the ...

19. please help!!! free format drawing.    coderanch.com

20. free-hand drawing and swing    coderanch.com

21. Drawing in XORMode or Layers?    coderanch.com

23. How to draw US map ?    coderanch.com

I know how to use the Image maps, but once the US map is drawn and when a user clicks on a particular state I need to identify the portion where he has clicked. I am stuck on this part. Any help in this matter is highly appreciated. Bhaskar [ December 30, 2002: Message edited by: Bhaskar Gunturi ]

24. Iterative Drawing    coderanch.com

25. Incremental drawing with swing?    coderanch.com

26. [Java] Getting stuff drawn on a swing component    coderanch.com

What is the end result you are trying to achieve with this? There are several ways you could multiple layers in a component - Add the component you are currently painting on to another container that has an overridden paint() method that inserts drawing operations after the paintChildren() method. Make each layer of the final product a separate image. Draw to ...

27. how to draw Gage/Protector    coderanch.com

28. Drawing Graphics in Layers?    coderanch.com

Hello, I have a tricky question, where I am not sure if java would supprot what I need. I am drawing grahps using Graphics in a class that exdends Applet. I would like to be able to draw two different things on the same 'canvas' without overwriting one another. Something that comes to mind as analogy is Layers but I couldn't ...

29. direct drawing    coderanch.com

30. Drawing to a backbuffer    coderanch.com

I've written a simple app which shows some graphics on the screen and does some very basic sprite movement. I don't know/understand the best way to redraw each frame though. What I'm doing now is approximately this - paintComponent(Graphics g) { // fill my bufferedimage with black // Draw some stuff directly on the g context // pass the graphics context ...

31. Drawing Bitmaps    coderanch.com

32. write a program to Draw a GanttChart using Java Swing    coderanch.com

Hi all, I am satheesh currently doing a swing project in a company.I want to draw a GanttChart using Java Swing.The Gantt Chart should have the functionality of cutting,copying,etc... for the tasks.The assignment of tasks in a Date and changing it (By means of dragging and dropping it in another date) should also be there. We cant buy any third party ...

33. Custom drawing    coderanch.com

I'm trying to create an application that renders custom components like rectangles, ovals, ... I'm now trying to subclass Jcomponent and overriding paintComponent. The code is public class SubProcess extends JPanel { private int posX; private int posY; private int aWidth; private int aHeight; /** * @param posX * @param posY * @param width * @param height */ public SubProcess(int posX, ...

34. Notice any problems with Java 1.5 drawing?    coderanch.com

Since I installed 1.5 on my two computers (NT4 and 98SE) all Java gui apps have problems with screen drawing; even the Java plugin control panel itself. Buttons partially drawn and overlapping; backgrounds not completely filled, drawing stuff in the wrong places (sometimes even outside of the app's frame window). I have searched but can't find any mentions of problems like ...

35. drawing only upper half of word    coderanch.com

Rendering that you do with the getGraphics method does not persist,ie, if and when swing decides to repaint your gui/component it will do so and your getGraphics code will not be called. Better to work with swing and subclass JEditorPane and override its paint method (careful, see api) or its paintComponent method. JEditorPane editorPane = new JEditorPane() { protected void paintComponent(Graphics ...

36. Drawing    coderanch.com

import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImagePanelApplet extends JApplet { public void init() { getContentPane().setLayout(new BorderLayout()); getContentPane().add(new JScrollPane(new ImagePanel())); } public static void main(String[] args) { JApplet applet = new ImagePanelApplet(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(applet); f.setSize(400,400); f.setLocation(200,200); applet.init(); f.setVisible(true); } } class ImagePanel extends JPanel { BufferedImage image; public ImagePanel() ...

37. creating an 'eraser' function for a drawing app...    coderanch.com

I'm a little stuck and thought I would see if anyone out there has any suggestions for this... I have a simple panel that I'm allowing the user to draw a line while dragging the mouse inside the 'drawable' area. I'm using the mousePressed, mouseReleased, and MouseDragged methods to capture a series of points and drawing lines between those points during ...

38. Saving of freehand drawing coordination...    coderanch.com

Hi! Do anyone of you have any idea how can i save my X and Y value coordination in a text file? I am currently doing a freehand drawing. i need to know how to save the coordination when i draw. For example when i draw from point A to Point B, the moment i click on my mouse, it should ...

39. drawing    coderanch.com

There are two general approaches to this: 1 component approach. You can make a GridLayout and add JLabels that can be used to show color (they must be 'setOpaque' to do this) and information. 2 graphics approach. Here you do all the rendering in the 'paintComponent' method (assuming you are using Swing components like JPanel). Draw a grid and ...

40. drawing table not using jtabel    coderanch.com

import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class GraphicTable extends JPanel { String[] msgs; final int ROWS = 3, COLS = 2, PAD = 10; public GraphicTable() { msgs = new String[] { "hello world", "graphic table" }; setBorder(BorderFactory.createLoweredBevelBorder()); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int w = getWidth(); int h = ...

41. Custom-Drawn Component    coderanch.com

So I have this chessboard component that is a 512x512 subclass of a JComponent that handles all its drawing itself (squares and pieces). I lay it out on a null layout manager as follows: JFrame f = ((JFrame)_screen.getWindow()); // configure controls f.getContentPane().removeAll(); f.getContentPane().setLayout(null); f.getContentPane().add(_boardRenderer); // position _boardRenderer.setSize(512, 512); _boardRenderer.setLocation(100, 100); The problem I have is, though the component is drawn perfectly, ...

42. Problem with simple drawing tool    coderanch.com

43. how to draw ?    coderanch.com

44. Drawing on desktopPane    coderanch.com

Hello everyone! This is my first time posting here at JavaRanch Big Moose Saloon, I hope I am doing it correctly. I have a problem with my following code. I have created a background image witht the desktopPane and now I would like to draw some shapes over it, however I have no idea how to! Can someone please give me ...

45. scrolls and pics and drawing    coderanch.com

The JFrame paint method is a Container method and is called to render the JFrame and all of its child components (see api). Using this requires some care and might be better left until you have had more practice. An easy way to get started is to use a separate JComponent and do your rendering in its paintComponent method. There are ...

46. Drawing Problem    coderanch.com

Is the component that is doing the drawing *storing* the shapes you want to draw, so that it can repaint them when needed, for example, after iconifying and deiconifying? Or are you doing your shape drawing in mouse events, which is usually a bad idea. My rule of thumb is: never call Component's getGraphics(), since what you then render will not ...

47. What to draw on?    coderanch.com

48. advice on drawing a square wave pulse train    coderanch.com

import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import javax.swing.*; public class SquareWave { public static void main(String[] args) { SquareWave squareWave = new SquareWave(); SquareWavePanel squareWavePanel = new SquareWavePanel(); WaveMaker waveMaker = new WaveMaker(squareWavePanel); squareWavePanel.addMouseListener(waveMaker); JFrame f = new JFrame("click to make wave"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(squareWavePanel); f.getContentPane().add(squareWave.getControls(waveMaker), "South"); f.setSize(500,300); f.setLocation(200,200); f.setVisible(true); } private JPanel getControls(final WaveMaker waveMaker) { final JButton start = new ...

49. draw method    coderanch.com

Maybe this will give you some ideas. import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.*; public class DrawMethod { private Point[][] generateRandomPoints(JFrame f) { Random seed = new Random(); Container cp = f.getContentPane(); int w = cp.getWidth(); int h = cp.getHeight(); int numberOfPointSets = 5 + seed.nextInt(5); Point[][] pts = new Point[numberOfPointSets][3]; for(int j = 0; j < numberOfPointSets; ...

50. drawing in graphics    coderanch.com

I am trying to draw a number of circles and a line. The circles are drawn by clicking the mouse button whereever it needs to be placed. Once the circles are drawn, I can right click the mouse to disable circle drawing and enable line drawing. But the circles disappear. When i draw the circles, it comes without any problem. Same ...

51. board not Drawing...    coderanch.com

for some reason, when I am calling "InitializeBoard(g)" and reDrawBoard(g), It's not painting to the JPanel... is there something I am missing? code is below... import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Board extends MouseAdapter { private Square[] squares = new Square [100]; private int count,W,L,Xoriginal,Yoriginal,X,Y; BattleShip object = new BattleShip(); public Board(int x, int y) { int count = ...

53. Swing drawing    coderanch.com

import java.awt.*; import javax.swing.*; public class test extends JPanel { private JFrame frame; private JPanel panel; private Container c; public void init() { JFrame frame = new JFrame("Test Swing Draw"); JPanel panel = new JPanel(); c = frame.getContentPane(); c.setLayout(new FlowLayout()); c.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,600); frame.setVisible(true); } public void paintComponent(Graphics g) { System.out.println("Paint ok."); super.paintComponent(g); g.setColor(Color.red); g.drawString("Hello!", 50, 50); } public static void ...

54. removing drawn objects    coderanch.com

Hi there, How do you remove drawn objects on a JPanel? Note my example project below which draws a rectangle on a JPanel named WhiteBoard. I include a button that, when pressed, is supposed to remove the rectangle from the WhiteBoard by calling removeAll() and then repainting. I tried calling both repaint() and revalidate(), but neither seemed to show the removal ...

55. Simple drawing    coderanch.com

Hi! I can't get this simple drawing thing to work. I'm drawing a Line. But I want it to look like a rectangle. ( Rotated rectangle ) I can't get this to work. My math isn't what It should I guess because I can't get the rectangle to draw correctly. From two points I want to draw a rotated rectangle. I ...

56. Drawing/moving problem: how to define destination? (code included)    coderanch.com

Hi, (sorry if my english is not understandable enough) I have created a program where user can define start point and destination point with a mouse. After doing this line is drawn between these points(coordinates using drawLine). Then I have this Run button and when it is pressed letter X starts to move to a direction which is "hard coded" to ...

57. Drawing at nodes    coderanch.com

I want to drawline against the parent node when its expanded and rectangle at all chil node....But its not showing any line... How can i draw for each node? Please help me.. /* * Reg.java * * Created on October 4, 2007, 3:12 PM * * To change this template, choose Tools | Template Manager * and open the template in ...

58. Drawing on JPane    coderanch.com

59. Drawing Axes?    coderanch.com

I want to plot some data and I would like to draw dates on the horizontal axis and price on the vertical using Swing. I have already implemented pan and zoom. Can anyone point me to some code that contains the logic to select tick marks at asthetically pleasing intervals and put the labels for each tick mark on so they ...

60. Help showing drawing items    coderanch.com

Hi all, I am trying to show a rectangle "flying" across a panel, but it just sits there Any suggestions? public void runProj(){ double t = 0; myPanel.setCurrentX((int)(40)); // The 40 moves it right to the start of the plot area myPanel.setCurrentY((int)(326) ); //This moves it down to the origin and subtracts to move up System.out.print("runProj thinks myPanel.getCurrentX = " + ...

61. can help me solve this drawing ?    coderanch.com

62. draw item    coderanch.com

63. Saving drawing on simple paint application problem    coderanch.com

Hi, I'm trying to create a simple paint application. A JPanel is extended and the drawing is repainted as the user moves the mouse around. I'm trying to Save the drawing by saving the extended JPanel class (Panel) but am not having much luck. When opening two saved files (one blank drawing and one with some drawing) I don't see much ...

64. Drawing Problem    coderanch.com

Hi, I am trying to solve a graphics problem. I need an algorithm for drawing an image made up of a large circle (100 pixel diameter) in the middle and around this circle, there should be smaller (25 pixel diameter) circles. there should be a 5 pixel separation between the large circle and the orbiting circles. The problem I am grappling ...

65. draw a polygone    coderanch.com

66. how to draw directly to the monitor without a While loop    coderanch.com

sorry for not explaining myself better. ill do it more explicitly- ill write down the the methods i know so far to draw an image: 1.- using paint method from inside the component(passive rendering): class mycomponent extends Component{ override paint(Graphics g) super.paint(); //do drawing here g.drawSomething } 2.-using active rendereing(anywhere inside a while loop) static main(){ Graphics g = mycomponet.GetGraphics(); while(true) ...

67. Redrawing the UI    coderanch.com

Hi All, I have a requirement in which I want to redraw a gridbaglayout panel based on a radio button selection. Can someone suggest me how to achieve this? Initially, the screen will look like this - On selection of radio button 2, I should show -

68. start/stop drawing    coderanch.com

Hi - Am curious about something. Suppose I wish to first draw a background, then gradually draw more objects on a JPanel, one by one. Does this all have to be done via a single paintComponent() call? In otherwords, write the code that does the entire draw in one pass in paintComponent()? In that case, I'm thinking I'll have to try ...

69. can't understand draw()    coderanch.com

70. save drawing    coderanch.com

71. Drawing a map    java-forums.org

72. how to draw in Java    java-forums.org

73. Finding where to draw    java-forums.org

import java.awt.*; import java.awt.geom.Rectangle2D; public class test4 extends Frame { public test4() { /* retrieve max window size */ GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); GraphicsConfiguration [] gc = gs[0].getConfigurations(); Rectangle r = gc[0].getBounds(); setSize(r.width, r.height); setVisible(true); Rectangle useable = getBounds(); System.out.println("Size: " + useable.width + "x" + useable.height); System.out.println("Offset: " + useable.x + "," + useable.y); } public ...

74. Draw Graphics on top of another    java-forums.org

I am currently writing a program to display a movable object on top of a background image. However, the frame displays nothing :( Could anyone help me to have a look of the following program... thanks Java Code: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MyProject extends JFrame{ private ImageIcon bg, img_player; boolean key_left=false, key_right=false, key_up=false, key_down=false; int x = ...

75. Drawing simple graphics in NetBeans    java-forums.org

Hi, I am trying to draw some simple graphics in NetBeans (6.9.1). Say, I have a jFrame, and a jPanel inside the jFrame... the usual code as generated by a simple NetBeans project. Now, I need to 'draw' some rectangles side-by-side (much like a checker or chess board) in jFrame. The total number of rectangles should be modifiable (e.g. in separate ...

76. how to draw oval using Graphics object    java-forums.org

Hi, I have one requirement.I have drawn one oval on panel using Graphics2D object. But i want to display the oval on panel immediately after executing the class with out performing any action(mouse click event). I had written the code in constructor.Its not displaying. This one working perfectly when i place the code on any event of the panel. please help ...

77. How to draw a dendrogram?    java-forums.org

78. Need help drawing an overview    java-forums.org

Hi, I am studying software delelopment and we are currently learning Java. I have been given an assignment to draw an overview over cars booked in a car booking system. There is a list of cars eg. (Golf, Mustang, Civic, Fiesta, Picasso) on the y axis and the coming 14 days on the x axis. Then there has to be some ...

79. Drawing on aJPanel    java-forums.org

import java.awt.*; import javax.swing.*; public class Test { public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(getContentPane()); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } private static JPanel createContentPane() { JPanel redPanel = new JPanel(); redPanel.setBackground(Color.red); redPanel.setLocation(10, 10); redPanel.setSize(50,50); return redPanel; } private static JPanel getContentPane(){ // GridLayout tends to ignore the size hints, // creates a grid of the available ...

80. Java Drawing PUZZLE    java-forums.org

@Override public void paintComponent(Graphics gpxGraphics) { super.paintComponent(gpxGraphics); Graphics2D g2dGraphics = (Graphics2D)gpxGraphics; g2dGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); //Find the size of the panel Dimension dmnSize = getSize(); //Set pen to black g2dGraphics.setColor(Color.BLACK); //Draw left and right curved edge g2dGraphics.fill(new Arc2D.Double(5, 5, 35, 35, 180, -90, Arc2D.PIE)); g2dGraphics.fill(new Arc2D.Double(dmnSize.width-40, 5, 35, 35, 90, -90, Arc2D.PIE)); //Draw title block g2dGraphics.fill(new Rectangle(22, 5, dmnSize.width-44, 17)); //Draw left and right ...

81. Drawing on a JComponents    forums.oracle.com

Hi All, Have been playing with Graphics/2D I have noticed a slight bug though, I have asked around and people say that it is a problem with WinXP and not java or my code. The Problem: When I draw a line on a JPanel the line is visible, When I drag the window around the screen the line is still visible ...

82. Drawing outside the GUI    forums.oracle.com

83. Owner Draw JComponent Problem    forums.oracle.com

84. Newbie - GUI drawing    forums.oracle.com

In my program of battleships for example i have drawn the grid using class Drawgrid extends JPanel { public void paint(Graphics g) { etc.... The user then types in the X and Y coordinates into 2 textfields then presses submit Now how do i draw the image of say a 'O' meaning miss into that location on the grid, atm it ...

85. Application is not initially drawing swing widgets    forums.oracle.com

Hello, I have a strange component drawing problem with an application. The application extends WindowAdaptor and starts by using an extended JFrame as a base to add components. Adding swing based components to the JFrame works fine and there are no exceptions generated when I run/debug the application. The problem occurs during the start-up of the application. When the app first ...

86. Redrawing in Swing    forums.oracle.com

Hi guys, I am coding a tetris game board using Swing Graphics2D. The way I have it set up is the board is a JPanel, and then there is a 2d boolean array (boolean[ ][ ]) that will hold a true value if a block is contained in it. finally, there is a draw method that scans the 2d array for ...

87. Help on drawing in GUI    forums.oracle.com